473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to create query ??????????

i have a table with following data
qty start_no end_no
1 1 100
1 101 200
1 201 300
1 5001 5100
1 7001 7100
1 7101 7200

i used query like
select sum(qty),min(start_no),max(end_no) from <table_name>;

it show
6 1 7200

BUT I WANT
3 1 300
1 5001 5100
2 7001 7200

PLZ HELP ME????????????????

THANKS A LOT
kuljeet pal singh
Jul 19 '05 #1
6 3681
"KULJEET" <ku***********@hotmail.com> wrote...
i have a table with following data
qty start_no end_no
1 1 100
1 101 200
1 201 300
1 5001 5100
1 7001 7100
1 7101 7200

i used query like
select sum(qty),min(start_no),max(end_no) from <table_name>;

it show
6 1 7200

BUT I WANT
3 1 300
1 5001 5100
2 7001 7200

PLZ HELP ME????????????????

THANKS A LOT
kuljeet pal singh


Eat this:
select sum(qty),min(start_no),max(end_no)
from <table_name>
where end_no < 301
union all
select sum(qty),min(start_no),max(end_no)
from <table_name>
where start_no >= 5001 and end_no <= 7000
union all
select sum(qty),min(start_no),max(end_no)
from <table_name>
where start_no >= 7001 and end_no <= 7200;

If that doesn't fit your needs, start thinking.
Jul 19 '05 #2
AK
it's very easy to accomplish using recursion
Jul 19 '05 #3
ku***********@hotmail.com (KULJEET) wrote in message news:<fe**************************@posting.google. com>...
[newbie sql question snipped]

Hint: look up group commands.
Socks
Jul 19 '05 #4
its a example only
i have thousand of rows in my table
AND I WANT TO GROUP ACCORDING TO PARTICULER SEQUENCE
if start_no and end_no in particule sequence then group it
else
create another group of another series
start and end no means:-
start_no-1=last_no of previous record;
IF EQUAL THEN IT IS A SERIES

****LIKE THIS *******
particuler sequence is like
start_no last_no
1 100
101 200
201 300

(this is a sequence from 1 to 300) NO BREAK IN SEQUENCE
and
another
5001 5100
5101 5200
this sequence is start from 5001 to 5200

if i insert new values in table
insert into <table name> values(1,5501,5600);

so now 3 group is created
3 1 300
2 5001 5100
1 5501 5600 <----because it will not in series
if start_no is 5101 in place of 5501
then it will group in series of 5001
and 5001 series return qty 3 in place of 2

thanks
kuljeet pal singh
Jul 19 '05 #5
AK
Assuming that the intervals do not overlap,
we can also use ROW_NUMBER() and avoid recursion:

1. Create 2 table expressions:
(SELECT START_NO, (ROW_NUMBER() OVER(ORDER BY START_NO)) AS SERIES_NUM
FROM INTERVALS I WHERE NOT EXISTS(SELECT END_NO FROM INTERVALS I1
WHERE I1.END_NO+1=I.START_NO)) AS LOWER_ENDS

(SELECT END_NO, (ROW_NUMBER() OVER(ORDER BY END_NO)) AS SERIES_NUM
FROM INTERVALS I WHERE NOT EXISTS(SELECT START_NO FROM INTERVALS I1
WHERE I.END_NO+1=I1.START_NO)) AS HIGHER_ENDS

2. Join them on SERIES_NUM

SELECT START_NO, END_NO
FROM LOWER_ENDS JOIN HIGHER_ENDS
ON LOWER_ENDS.SERIES_NUM = HIGHER_ENDS.SERIES_NUM
Jul 19 '05 #6
Well see the magic of "analytic function" ...

select * from t_analytic;

QTY START_ID END_ID
---------- ---------- ----------
1 1 100
1 1 100
2 101 200
1 101 200
3 201 300

select distinct sum(qty) over(partition by start_id,end_id) sqty,
min(start_id) over(partition by start_id,end_id) mstart_id,
max(end_id) over(partition by start_id,end_id) xend_id
from t_analytic
/

SQTY MSTART_ID XEND_ID
---------- ---------- ----------
2 1 100
3 101 200
3 201 300
Regards,
Siva
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lkrubner | last post by:
I have a webserver through Rackspace. I create a domain. I create an FTP user. I upload some files. I create a database called testOfSetupScript and then I create a database user named setup. I...
8
by: Donna Sabol | last post by:
First, I should start by saying I am creating a database to be used by some very impatient, non-computer literate people. It needs to be seameless in it's operation from their point of view. I...
6
by: sheree | last post by:
I would like to create a query where one of the columns of the queries comes from a combo list box on a form. For example, if my table has the following fields: id name interest1 interest2...
4
by: Apple | last post by:
1. I want to create an autonumber, my requirement is : 2005/0001 (Year/autonumber), which year & autonumber no. both can auto run. 2. I had create a query by making relation to a table & query,...
18
by: PC Datasheet | last post by:
An Access user saw my name in a newsgroup and sent me a request for help on a project. As part of the project, a list of the dates in a month was needed. For anyone needing a list of dates in a...
13
by: forbes | last post by:
Hi, I have a user that used the Query Wizard to create a query in Access. Now she claims that her master table is missing all the data that was excluded from the query. Can you create anything...
2
by: angie | last post by:
I need to figure out how to create a user interface to search a query, but here's the bad part...I need to account for criteria on at least 7 of the fields. Here's what I'm thinking I need to do:...
8
by: chrisdavis | last post by:
I'm trying to filter by query or put those values in a distinct query in a where clause in some sort of list that it goes through but NOT at the same time. Example: ROW1 ROW2 ROW3 ROW4 ,...
15
by: harvey | last post by:
How do I make PHP create a database for mysql please? I can see how to make tables and I have read all the documents I can find but I don't understand how to make the database itself. All...
3
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.