sql server programming:
Hi, I have table like this CODE Name DateFrom 1 d american 2004/03/06 15:41:28 1 c american 2004/03/06 15:39:23 1 b american 2004/03/06 15:39:23 2 burt walker 2004/03/01 12:46:20 3 x john 2003/01/01 15:10:30 3 z john 2004/01/01 10:10:20 I want to make a query that returns this information grouped by code and sorted by name and datefrom (desc) I mean, each group (grouped by code) has to be sorted by datefrom (desc) and name (asc), but the final query will give me everything sorted by name (using the first of each group to find out which one goes first) This should be the result: 2 burt walker 2004/03/01 12:46:20 1 d american 2004/03/06 15:41:28 1 b american 2004/03/06 15:39:23 1 c american 2004/03/06 15:39:23 3 z john 2004/01/01 10:10:20 3 x john 2003/01/01 15:10:30 I tried this: select * from Table order by code, datefrom desc, name and it sorts each individual subgroup, but it also sorts by code... Sorry If I didn't explain it too clearly. I hope you guys can understand the problem and give me some hints. Thanks!
Thanks for your answer, Vishal. Here is another example 2 s 2004/03/01 12:46:20 3 a 2003/01/01 15:10:30 1 j 2004/03/06 15:39:23 3 u 2004/01/01 10:10:20 1 g 2004/03/06 15:41:28 1 a 2004/03/06 15:39:23 First, we group by codes: 1 j 2004/03/06 15:39:23 1 a 2004/03/06 15:39:23 1 g 2004/03/06 15:41:28 3 a 2003/01/01 15:10:30 3 u 2004/01/01 10:10:20 2 s 2004/03/01 12:46:20 Now, for each group, we sort by Date 1 g 2004/03/06 15:41:28 1 j 2004/03/06 15:39:23 1 a 2004/03/06 15:39:23 3 u 2004/01/01 10:10:20 3 a 2003/01/01 15:10:30 2 s 2004/03/01 12:46:20 Now, for each group with the same date, we sort by name 1 g 2004/03/06 15:41:28 1 a 2004/03/06 15:39:23 1 j 2004/03/06 15:39:23 3 u 2004/01/01 10:10:20 3 a 2003/01/01 15:10:30 2 s 2004/03/01 12:46:20 And now, we sort the group by name, using the first record of each group (The order is g,s,u) 1 g 2004/03/06 15:41:28 1 a 2004/03/06 15:39:23 1 j 2004/03/06 15:39:23 2 s 2004/03/01 12:46:20 3 u 2004/01/01 10:10:20 3 a 2003/01/01 15:10:30 That's should be the result of the query. Maybe it's not as simple as I thought at the beginning. I hope somebody can help me. Thanks!
hi star, [quoted text, click to view] >> but the final query will give me everything sorted by name
(using the first of each group to find out which one goes first)<< can you elaborate your above statement more. preferably with the help of some relevent sample records and expected result set. -- Vishal Parkar vgparkar@yahoo.co.in
select * from tb order by code asc, datefrom desc, name asc -- -oj http://www.rac4sql.net [quoted text, click to view] "Star" <----> wrote in message news:Oho%23tueBEHA.1964@TK2MSFTNGP11.phx.gbl... > Thanks for your answer, Vishal. > > Here is another example > > 2 s 2004/03/01 12:46:20 > 3 a 2003/01/01 15:10:30 > 1 j 2004/03/06 15:39:23 > 3 u 2004/01/01 10:10:20 > 1 g 2004/03/06 15:41:28 > 1 a 2004/03/06 15:39:23 > > > > First, we group by codes: > > 1 j 2004/03/06 15:39:23 > 1 a 2004/03/06 15:39:23 > 1 g 2004/03/06 15:41:28 > > 3 a 2003/01/01 15:10:30 > 3 u 2004/01/01 10:10:20 > > 2 s 2004/03/01 12:46:20 > > Now, for each group, we sort by Date > > 1 g 2004/03/06 15:41:28 > 1 j 2004/03/06 15:39:23 > 1 a 2004/03/06 15:39:23 > > 3 u 2004/01/01 10:10:20 > 3 a 2003/01/01 15:10:30 > > 2 s 2004/03/01 12:46:20 > > Now, for each group with the same date, we sort by name > > 1 g 2004/03/06 15:41:28 > 1 a 2004/03/06 15:39:23 > 1 j 2004/03/06 15:39:23 > > 3 u 2004/01/01 10:10:20 > 3 a 2003/01/01 15:10:30 > > 2 s 2004/03/01 12:46:20 > > And now, we sort the group by name, using the first record of each group > (The order is g,s,u) > > 1 g 2004/03/06 15:41:28 > 1 a 2004/03/06 15:39:23 > 1 j 2004/03/06 15:39:23 > > 2 s 2004/03/01 12:46:20 > > 3 u 2004/01/01 10:10:20 > 3 a 2003/01/01 15:10:30 > > > > That's should be the result of the query. Maybe it's not as simple as I > thought at the beginning. > I hope somebody can help me. Thanks! > > > >
Now toss in the tricky part. ;-) select code, name, datefrom from tb tboo order by ( select top 1 name from tb tbii where tbii.code = tboo.code and tbii.datefrom = tbii.datefrom -- Consider ignoring the time. order by code asc, -- Not really needed. datefrom desc, -- Not really needed. name asc -- Is really needed. ) asc, code asc, datefrom desc, name asc Bye, Delbert Glass [quoted text, click to view] "oj" <nospam_ojngo@home.com> wrote in message news:OrGCoYgBEHA.392@TK2MSFTNGP12.phx.gbl... > select * > from tb > order by code asc, datefrom desc, name asc > > -- > -oj > http://www.rac4sql.net > > > "Star" <----> wrote in message > news:Oho%23tueBEHA.1964@TK2MSFTNGP11.phx.gbl... > > Thanks for your answer, Vishal. > > > > Here is another example > > > > 2 s 2004/03/01 12:46:20 > > 3 a 2003/01/01 15:10:30 > > 1 j 2004/03/06 15:39:23 > > 3 u 2004/01/01 10:10:20 > > 1 g 2004/03/06 15:41:28 > > 1 a 2004/03/06 15:39:23 > > > > > > > > First, we group by codes: > > > > 1 j 2004/03/06 15:39:23 > > 1 a 2004/03/06 15:39:23 > > 1 g 2004/03/06 15:41:28 > > > > 3 a 2003/01/01 15:10:30 > > 3 u 2004/01/01 10:10:20 > > > > 2 s 2004/03/01 12:46:20 > > > > Now, for each group, we sort by Date > > > > 1 g 2004/03/06 15:41:28 > > 1 j 2004/03/06 15:39:23 > > 1 a 2004/03/06 15:39:23 > > > > 3 u 2004/01/01 10:10:20 > > 3 a 2003/01/01 15:10:30 > > > > 2 s 2004/03/01 12:46:20 > > > > Now, for each group with the same date, we sort by name > > > > 1 g 2004/03/06 15:41:28 > > 1 a 2004/03/06 15:39:23 > > 1 j 2004/03/06 15:39:23 > > > > 3 u 2004/01/01 10:10:20 > > 3 a 2003/01/01 15:10:30 > > > > 2 s 2004/03/01 12:46:20 > > > > And now, we sort the group by name, using the first record of each group > > (The order is g,s,u) > > > > 1 g 2004/03/06 15:41:28 > > 1 a 2004/03/06 15:39:23 > > 1 j 2004/03/06 15:39:23 > > > > 2 s 2004/03/01 12:46:20 > > > > 3 u 2004/01/01 10:10:20 > > 3 a 2003/01/01 15:10:30 > > > > > > > > That's should be the result of the query. Maybe it's not as simple as I > > thought at the beginning. > > I hope somebody can help me. Thanks! > > > > > > > > > >
Opps, change this and tbii.datefrom = tbii.datefrom -- Consider ignoring the time. to this and tbii.datefrom = tboo.datefrom -- Consider ignoring the time. Also you do want to ignore the time, so further change it to this: and cast(cast(tbii.datefrom as integer) as datetime) = cast(cast(tboo.datefrom as integer) as datetime) Also you'll want to ignore the time in the ORDER BY clauses. cast(cast(tbii.datefrom as integer) as datetime) desc, Of course use whatever you favorite expression is for ignoring the time. If you need it to run faster, create a calculated column of the date with the time ignored and use the calculated column in multi-column index (code asc, datefromWOtime desc, name asc) .. Bye, Delbert Glass [quoted text, click to view] "Delbert Glass" <delbert@noincoming.com> wrote in message news:u6e3QEhBEHA.2888@TK2MSFTNGP09.phx.gbl... > Now toss in the tricky part. ;-) > > select > code, > name, > datefrom > from tb tboo > order by > ( > select top 1 name > from tb tbii > where tbii.code = tboo.code > and tbii.datefrom = tbii.datefrom -- Consider ignoring the time. > order by > code asc, -- Not really needed. > datefrom desc, -- Not really needed. > name asc -- Is really needed. > ) asc, > code asc, > datefrom desc, > name asc > > Bye, > Delbert Glass > > "oj" <nospam_ojngo@home.com> wrote in message > news:OrGCoYgBEHA.392@TK2MSFTNGP12.phx.gbl... > > select * > > from tb > > order by code asc, datefrom desc, name asc > > > > -- > > -oj > > http://www.rac4sql.net > > > > > > "Star" <----> wrote in message > > news:Oho%23tueBEHA.1964@TK2MSFTNGP11.phx.gbl... > > > Thanks for your answer, Vishal. > > > > > > Here is another example > > > > > > 2 s 2004/03/01 12:46:20 > > > 3 a 2003/01/01 15:10:30 > > > 1 j 2004/03/06 15:39:23 > > > 3 u 2004/01/01 10:10:20 > > > 1 g 2004/03/06 15:41:28 > > > 1 a 2004/03/06 15:39:23 > > > > > > > > > > > > First, we group by codes: > > > > > > 1 j 2004/03/06 15:39:23 > > > 1 a 2004/03/06 15:39:23 > > > 1 g 2004/03/06 15:41:28 > > > > > > 3 a 2003/01/01 15:10:30 > > > 3 u 2004/01/01 10:10:20 > > > > > > 2 s 2004/03/01 12:46:20 > > > > > > Now, for each group, we sort by Date > > > > > > 1 g 2004/03/06 15:41:28 > > > 1 j 2004/03/06 15:39:23 > > > 1 a 2004/03/06 15:39:23 > > > > > > 3 u 2004/01/01 10:10:20 > > > 3 a 2003/01/01 15:10:30 > > > > > > 2 s 2004/03/01 12:46:20 > > > > > > Now, for each group with the same date, we sort by name > > > > > > 1 g 2004/03/06 15:41:28 > > > 1 a 2004/03/06 15:39:23 > > > 1 j 2004/03/06 15:39:23 > > > > > > 3 u 2004/01/01 10:10:20 > > > 3 a 2003/01/01 15:10:30 > > > > > > 2 s 2004/03/01 12:46:20 > > > > > > And now, we sort the group by name, using the first record of each group > > > (The order is g,s,u) > > > > > > 1 g 2004/03/06 15:41:28 > > > 1 a 2004/03/06 15:39:23 > > > 1 j 2004/03/06 15:39:23 > > > > > > 2 s 2004/03/01 12:46:20 > > > > > > 3 u 2004/01/01 10:10:20 > > > 3 a 2003/01/01 15:10:30 > > > > > > > > > > > > That's should be the result of the query. Maybe it's not as simple as I > > > thought at the beginning. > > > I hope somebody can help me. Thanks! > > > > > > > > > > > > > > > > > >
Hi, I must not have understood your requirement. ;( /* And now, we sort the group by name, using the first record of each group (The order is g,s,u) 1 g 2004/03/06 15:41:28 1 a 2004/03/06 15:39:23 1 j 2004/03/06 15:39:23 2 s 2004/03/01 12:46:20 3 u 2004/01/01 10:10:20 3 a 2003/01/01 15:10:30 */ create table #tmp(Code int,Name sysname,DateFrom datetime) insert #tmp select 2, 's', '2004/03/01 12:46:20' union all select 3, 'a', '2003/01/01 15:10:30' union all select 1, 'j', '2004/03/06 15:39:23' union all select 3, 'u', '2004/01/01 10:10:20' union all select 1, 'g', '2004/03/06 15:41:28' union all select 1, 'a', '2004/03/06 15:39:23' go select * from #tmp order by Code asc, DateFrom desc, Name asc select code, name, datefrom from #tmp tboo order by ( select top 1 name from #tmp tbii where tbii.code = tboo.code and tbii.datefrom = tboo.datefrom -- Consider ignoring the time. order by code asc, -- Not really needed. datefrom desc, -- Not really needed. name asc -- Is really needed. ) asc, code asc, datefrom desc, name select CODE, Name, DateFrom from ( select CODE, Name, DateFrom, ( select top 1 Name from #tmp T2 where T2.CODE = T1.CODE order by DateFrom desc) TopNameForThisCode from #tmp T1 ) T order by TopNameForThisCode, CODE, DateFrom desc go drop table #tmp go -- -oj http://www.rac4sql.net [quoted text, click to view] "Star" <----> wrote in message news:u8lZk2hBEHA.3748@TK2MSFTNGP11.phx.gbl... > > select * > > from tb > > order by code asc, datefrom desc, name asc > > Thanks for your answer, oj, but that doesn't work. I don't get the results > sorted by name for each group. > > > > >
It might even be simpler (without all this subquery) if the OP posts ddl+sample data+expected result. -- -oj http://www.rac4sql.net [quoted text, click to view] "Delbert Glass" <delbert@noincoming.com> wrote in message news:uOf6E8iBEHA.1732@TK2MSFTNGP12.phx.gbl... > Relocate the subquery to the select list > giving it an alias, and use the alias > in the ORDER BY. That way you can see the > value. Which might be helpfull to figure > out what is going on. [1] > > Below is an example of the rearrangement > (but does not contain the ignore the time part stuff). > > [1] Perhaps, the ORDER BY clause in your subquery > was incomplete causing the NameOfGroupLeader > to be undetermined. If you still have problems, > post your lastest query and the output > (including the NameOfGroupLeader column). > > select > ( > select top 1 name > from tb tbii > where tbii.code = tboo.code > and tbii.datefrom = tboo.datefrom -- Consider ignoring the time. > order by > code asc, -- Not really needed. > datefrom desc, -- Not really needed. > name asc -- Is really needed. > ) as NameOfGroupLeader, > code, > name, > datefrom > from tb tboo > order by > NameOfGroupLeader asc, > code asc, > datefrom desc, > name asc > > Bye, > Delbert Glass > >
[quoted text, click to view] > select * > from tb > order by code asc, datefrom desc, name asc
Thanks for your answer, oj, but that doesn't work. I don't get the results sorted by name for each group.
Delbert, Thanks a lot for your help. It worked *almost* great. For some reason, I still get a name unsorted. I've tried to find where is the problem, but I cannot see it. Here is a screenshot of the results of my query (as you can see, there is one record unsorted) http://lemforever.com/temp/query.jpg Thanks!
Looks like you expect 59760 and 59744 to have been adjacent. I suspect the code column is not integer and 58760's code is lower case leter L; rather than, the letter one. Bye, Delbert Glass [quoted text, click to view] "Star" <----> wrote in message news:%23Utc6%23hBEHA.3748@tk2msftngp13.phx.gbl... > Delbert, > > Thanks a lot for your help. It worked *almost* great. > For some reason, I still get a name unsorted. I've tried to find where is > the problem, but I cannot see it. > Here is a screenshot of the results of my query (as you can see, there is > one record unsorted) > > http://lemforever.com/temp/query.jpg > > Thanks! > >
Relocate the subquery to the select list giving it an alias, and use the alias in the ORDER BY. That way you can see the value. Which might be helpfull to figure out what is going on. [1] Below is an example of the rearrangement (but does not contain the ignore the time part stuff). [1] Perhaps, the ORDER BY clause in your subquery was incomplete causing the NameOfGroupLeader to be undetermined. If you still have problems, post your lastest query and the output (including the NameOfGroupLeader column). select ( select top 1 name from tb tbii where tbii.code = tboo.code and tbii.datefrom = tboo.datefrom -- Consider ignoring the time. order by code asc, -- Not really needed. datefrom desc, -- Not really needed. name asc -- Is really needed. ) as NameOfGroupLeader, code, name, datefrom from tb tboo order by NameOfGroupLeader asc, code asc, datefrom desc, name asc Bye, Delbert Glass
Star, How about select CODE, Name, DateFrom from ( select CODE, Name, DateFrom, ( select top 1 Name from yourTable T2 where T2.CODE = T1.CODE order by DateFrom desc) TopNameForThisCode from yourTable T1 ) T order by TopNameForThisCode, CODE, DateFrom desc SK [quoted text, click to view] Star wrote: >Hi, > >I have table like this > >CODE Name DateFrom > 1 d american 2004/03/06 15:41:28 > 1 c american 2004/03/06 15:39:23 > 1 b american 2004/03/06 15:39:23 > > 2 burt walker 2004/03/01 12:46:20 > > 3 x john 2003/01/01 15:10:30 > 3 z john 2004/01/01 10:10:20 > > >I want to make a query that returns this information grouped by code and >sorted by name and datefrom (desc) >I mean, each group (grouped by code) has to be sorted by datefrom (desc) and >name (asc), but the final query will give me everything sorted by name >(using the first of each group to find out which one goes first) > >This should be the result: > > 2 burt walker 2004/03/01 12:46:20 > > 1 d american 2004/03/06 15:41:28 > 1 b american 2004/03/06 15:39:23 > 1 c american 2004/03/06 15:39:23 > > 3 z john 2004/01/01 10:10:20 > 3 x john 2003/01/01 15:10:30 > >I tried this: > >select * from Table order by code, datefrom desc, name > >and it sorts each individual subgroup, but it also sorts by code... > >Sorry If I didn't explain it too clearly. I hope you guys can understand the >problem and give me some hints. > >Thanks! > > > >
(in another branch of the thread) [quoted text, click to view] > TopNameForThisCode
Surely, you've notice I've been doing: TopNameForThisCodeOnThisDay Include/exclude the date condition in the subquery as desired. (be sure to say which way you want in your next post) Bye, Delbert Glass
[quoted text, click to view] > Looks like you expect 59760 and 59744 to have been adjacent.
Yes, exactly. [quoted text, click to view] > I suspect the code column is not integer > and 58760's code is lower case leter L; > rather than, the letter one.
The type of Code is integer. I thought the same than you the first time, but it's an integer. Here is the definition: CREATE TABLE [Subs_Subjects] ( [ID] [int] IDENTITY (1, 1) NOT NULL , [Code] [int] NOT NULL , [Name] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [DateFrom] [datetime] NOT NULL CONSTRAINT [DF_Subs_Subjects_DateFrom] DEFAULT (getdate()), [AKA] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Ethic] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Group] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [TitleInGroup] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [DOB] [datetime] NULL , [Race] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Sex] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Height] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Weight] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Eyes] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Hair] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Comments] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [SSN] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Tattoos] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Occupation] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [DLNo] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [DLState] [char] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [AlienRegistration] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [C0] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [C1] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [C2] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [C3] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [C4] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [C5] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [C6] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [C7] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [C8] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [C9] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [FK_CustomGroup_ID] [int] NULL , [FK_CustomSubGroup_ID] [int] NULL , [FK_Pictures_ID] [int] NULL , [ImportName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [ImportDate] [datetime] NULL , [Source] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , CONSTRAINT [PK_HumIntel] PRIMARY KEY CLUSTERED ( [ID] ) ON [PRIMARY] ) ON [PRIMARY] GO Thanks a lot for your help, Delbert.
I said: [quoted text, click to view] > cast(cast(tbii.datefrom as integer) as datetime)
don't do that, it rounds everything after lunch to the following day. ---- However that is not want causing 59760 and 59744 to be non-adjacent. The subquery needs to include name in the ORDER BY clause for TOP (thus the ORDER BY clause needs to be present and non-commented out). Bye, Delbert Glass
Just add [name] at the end of the order by list, if you want ascending name for maching code and date. SK [quoted text, click to view] Star wrote: >Thanks for your answer, Steve. > >It looks fine, but there is problem where we have the same date. Here is a >screenshot: > > http://lemforever.com/temp/query2.jpg > > > > >
Yes! It works perfect now. Thanks a lot, Steve.
Here it is. For some reason, it seems that belongs to a different group. http://lemforever.com/temp/query3.jpg But it's ok, Delbert. I think I can use Steve's solution, which is as fast as yours. If you think it's too much trouble to find the problem, we can use the ofher solution. Thanks a lot. I really appreciate your help.
Here is the trouble: The time part in the date condition of the subquery is not being ignored. The rows 59815 and 59761 come where they did because they got 59762's name since they have exactly the date&time as it does (and same code). Meanwhile, row 59760 got it's own name since there is nothing else with a code of 1 and exactly the same date&time; and thus, come out where it did. Bye, Delbert Glass [quoted text, click to view] "Star" <----> wrote in message news:uIqxVTjBEHA.3284@TK2MSFTNGP09.phx.gbl... > Here it is. For some reason, it seems that belongs to a different group. > > http://lemforever.com/temp/query3.jpg > > But it's ok, Delbert. I think I can use Steve's solution, which is as fast > as yours. > If you think it's too much trouble to find the problem, we can use the ofher > solution. > > Thanks a lot. I really appreciate your help. > >
Thanks for the clarification, Delbert. If I find out something, I'll let you know. Thanks again
Don't see what you're looking for? Try a search.
|