| Author |
Topic |
|
NTC
Starting Member
21 Posts |
Posted - 2007-11-07 : 21:30:23
|
| need some sql assistance....have this:Model DateA 2/2/07B 1/2/07C 1/1/07A 3/1/07C 2/2/07I think you get the idea....random.... I need sorted by Model that's important - but then with lowest date first:C 1/1/07 C 2/2/07B 1/2/07A 2/2/07A 3/1/07have the ability to create a control column if that helps...but am struggling with the sql statement that will give me the correct results and would welcome input....thanks in advance... |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-11-07 : 21:36:45
|
| [code]Select Model,DateFROM [Table]Group by Model,DateOrder by Model desc, Date asc[/code] |
 |
|
|
NTC
Starting Member
21 Posts |
Posted - 2007-11-07 : 21:49:20
|
| thnks but doesn't result in the correct sort/order...has models with more recent dates appearing before models with olderModel DatePool 7/2/2007Pool 7/3/2007B99 4/27/2005B99 5/5/2005B99 5/6/2005B99 5/9/2005B98 4/25/2005B98 4/26/2005B98 4/27/2005just a snippet from the results...the B98 has the earliest date 4/25 and so should be the first model group |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-11-07 : 21:55:01
|
| What is the proposed sort order for "Model" then? If you just order by date, the models will be out of order. If you sort by model as well, then it will be essentially in alphabetical order either asc or desc depending on what you want.If you want the earliest date first, you have switch the order by to:ORDER BY Date asc , Model descmy original response would produce your original sample of desired results.It helps to be a little more specific so that you get what you need. |
 |
|
|
NTC
Starting Member
21 Posts |
Posted - 2007-11-07 : 22:04:45
|
| well data speaks louder than words....the original example of what is needed is valid;the model with the lowest date first; grouped with that entire model's groupthe next model with the next lowest date next; grouped with that entire models groupetc.I'm thinking maybe it needs a control column created:lowest date/model = 1 then all of that model = 1 next lowest date/different model = 2 then all of that model = 2etc.and then sort by the control column.... but this could be a red herring idea.... There is a logical pattern so it should be do-able...just can't get it together to do however..... |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-11-07 : 22:42:20
|
That is clearer, and yes ..easy with another column to identify sort orderquote: If exists (Select * from sysobjects where name = 'Foo')Drop Table FoogoCreate Table Foo (Model char(5) not null,TestDate datetime not null,sortorder int null)goInsert Into Foo (Model,TestDate)Select 'Pool', '7/2/2007' UNION ALLSelect 'Pool','7/3/2007' UNION ALLSelect 'B99', '4/27/2005' UNION ALLSelect 'B99', '5/5/2005' UNION ALLSelect 'B99', '5/6/2005' UNION ALLSelect 'B99', '5/9/2005' UNION ALLSelect 'B98', '4/25/2005' UNION ALLSelect 'B98', '4/26/2005' UNION ALLSelect 'B98', '4/27/2005'GOSelect model,min(testdate) as mindate,identity(int,1,1) as oidInto #RankFROM Foo GRoup by modelUpdate FOoSet Sortorder = oidFROM #Rank inner join Foo on #Rank.Model = Foo.ModelSelect Model,TestDateFROM FooOrder by SortOrder asc,TestDate ascDrop Table #Rank
Produces:quote: B98 2005-04-25 00:00:00.000B98 2005-04-26 00:00:00.000B98 2005-04-27 00:00:00.000B99 2005-04-27 00:00:00.000B99 2005-05-05 00:00:00.000B99 2005-05-06 00:00:00.000B99 2005-05-09 00:00:00.000Pool 2007-07-02 00:00:00.000Pool 2007-07-03 00:00:00.000
So if you follow that...it is essentially what you proposed above. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-11-08 : 08:50:06
|
Just join your model table to a result set that returns the lowest date per model.select m.Model, m.[Date]from models minner join ( select Model, Min(date) as MinDate from models group by model ) m2 on m.Model = m2.Modelorder by m2.MinDate ASC, m.Model ASC, m.[Date] ASC - Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-08 : 09:53:01
|
Or this one, if you have SQL Server 2005SELECT Model, TestDateFROM ModelORDER BY MIN(TestDate) OVER (PARTITION BY Model), Model, TestDate E 12°55'05.25"N 56°04'39.16" |
 |
|
|
NTC
Starting Member
21 Posts |
Posted - 2007-11-08 : 10:54:30
|
| Really great - thank you all. Don't have SqlS5 - so copied in jeff's inner join code, only since it was less than yak's when I looked at both this a.m., and it worked like a champ out of the gates. Much appreciated. |
 |
|
|
|