Author |
Topic |
Ken Blum
Constraint Violating Yak Guru
383 Posts |
Posted - 2008-01-04 : 11:34:08
|
I have this SQL...Select Client.Customer_No, Client.City,WashRun.LF_Desc AS Formula,SUM(WashRun.LF_WT_Target) AS Target_WeightFROM ClientINNER JOIN WashRun ON WashRun.Customer_No = Client.Customer_No AND WashRun.BOF_Date BETWEEN '10/1/2007' and '01/01/2008'WHERE Client.Company Like 'DEMO'GROUP BY Client.Customer_No, Client.City,WashRun.LF_Desc ORDER By Client.City, Target_Weight DESC This returns data like so...15145 Albuquerque D - Mats 53913415145 Albuquerque N - Butcher Coats 27947515145 Albuquerque A - Col Pants / Covs / Cotton 27923015145 Albuquerque M - Bar / Wind / Glass Towel 21727715145 Albuquerque N4 - Meat Cutter Gloves 13800015145 Albuquerque B - Color Shirts 12328015145 Albuquerque E - Shop Towels 10562415145 Albuquerque E7 - Dust Mops / CRT / Fender 10172515145 Albuquerque H1 -Bath Towels - Hospital 4725015145 Albuquerque B1 - Heavy Soil Garments 4090515145 Albuquerque B3 - Heavy Soil Garments 3821015145 Albuquerque Z1 - Blue Bar Towel 3667515145 Albuquerque J3 - Col Food Service / Apron 3307515145 Albuquerque N - Butcher Coats / Wht Apron 1968515145 Albuquerque P - Executive Shirts 1390015145 Albuquerque C - White Industrial Garments 1350015145 Albuquerque O - Executive Pants 1100015145 Albuquerque I - Inkers 707715145 Albuquerque E6 - New Dyed Shop Towels 590015145 Albuquerque B5 - Yellow Transportation 200015145 Albuquerque D - ESD Garments 180015145 Albuquerque H1 - Bath Towels - Hospital 67515145 Albuquerque H1 - Med Soil White Linen 50015145 Albuquerque B4 - Knit Shirts 36815145 Albuquerque W - Resour 20011529 Belleville Mats 65828011529 Belleville Bar Mops 14744011529 Belleville Cotton Garments 12924011529 Belleville Color Pants 11553511529 Belleville Heavy Soil Shirts 5652011529 Belleville Schering 5598011529 Belleville Color Shirts 5328011529 Belleville Butcher Coats 4707011529 Belleville Whites 2167511529 Belleville Heavy Soil Cotton 1807511529 Belleville Colors 1582511529 Belleville Bath Towels 1440011529 Belleville Schering Poly Coats 1215011529 Belleville Mops 903511529 Belleville Sun Chemical 468011529 Belleville Yellow Freight 307511529 Belleville Kraft Foods 175511529 Belleville Clean Machine 112511529 Belleville Aprons 67511529 Belleville New White Towels 45011529 Belleville 012441 Coppell Mats (D) 162624012441 Coppell Jeans (A5) 69187512441 Coppell Colored Pants (A) 60634512441 Coppell Colored Shirts (B) 34982012441 Coppell Cotton Shirts (A5) 303425How can I get this SQL to only return the TOP 3 records in each Client.City?TIA |
|
graz
Chief SQLTeam Crack Dealer
4149 Posts |
|
pootle_flump
1064 Posts |
Posted - 2008-01-04 : 11:43:55
|
Ken - remove the trailing dot from the link Graz - aren't these SS 2005 specific? |
 |
|
graz
Chief SQLTeam Crack Dealer
4149 Posts |
Posted - 2008-01-04 : 11:46:16
|
Ack! Yes, they are SQL Server 2005 specific. My apologies for that. Sorry, I don't have a solution off the top of my head in SQL Server 2000.=================================================Creating tomorrow's legacy systems today. One crisis at a time. |
 |
|
Ken Blum
Constraint Violating Yak Guru
383 Posts |
Posted - 2008-01-04 : 11:49:46
|
Thanks but we are still using SQL 2000. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-04 : 12:07:39
|
Try this:-SELECT t.* FROM( Select Client.Customer_No, Client.City, WashRun.LF_Desc AS Formula, SUM(WashRun.LF_WT_Target) AS Target_Weight FROM Client INNER JOIN WashRun ON WashRun.Customer_No = Client.Customer_No AND WashRun.BOF_Date BETWEEN '10/1/2007' and '01/01/2008' WHERE Client.Company Like 'DEMO' GROUP BY Client.Customer_No, Client.City,WashRun.LF_Desc )tINNER JOIN( SELECT t1.City,t1.Target_Weight,COUNT(*)+1 AS 'RowNo' FROM ( Select Client.Customer_No, Client.City, WashRun.LF_Desc AS Formula, SUM(WashRun.LF_WT_Target) AS Target_Weight FROM Client INNER JOIN WashRun ON WashRun.Customer_No = Client.Customer_No AND WashRun.BOF_Date BETWEEN '10/1/2007' and '01/01/2008' WHERE Client.Company Like 'DEMO' GROUP BY Client.Customer_No, Client.City,WashRun.LF_Desc ORDER By Client.City, Target_Weight DESC )t1 WHERE t1.City=t.City AND t1.Target_Weight>t.Target_Weight)tmpON tmp.City=t.CityAND tmp.Target_Weight=t.Target_WeightWHERE tmp.RowNo<=3ORDER By t.City, t.Target_Weight DESC |
 |
|
pootle_flump
1064 Posts |
Posted - 2008-01-04 : 12:43:06
|
quote: Originally posted by pootle_flump Ken - remove the trailing dot from the link 
Ooh - you fiend. You edited your post and there is no "Edited on..." info. I look even denser than usual. |
 |
|
Ken Blum
Constraint Violating Yak Guru
383 Posts |
Posted - 2008-01-04 : 12:49:25
|
I couldn't get that to work. First problem was that it could not use Order by in T1 subquery so I changed that to do a SELECT TOP 10000.Then it says that "The column prefix 't' does not match with a table name or alias name used in the query.", and it' on the first line. I played with it for a while but could not get it to work. I think it's related to the T1 Where clause. |
 |
|
graz
Chief SQLTeam Crack Dealer
4149 Posts |
Posted - 2008-01-04 : 14:22:07
|
quote: Originally posted by pootle_flump
quote: Originally posted by pootle_flump Ken - remove the trailing dot from the link 
Ooh - you fiend. You edited your post and there is no "Edited on..." info. I look even denser than usual.
Yes, sorry. As the forum admin I can edit posts without leaving a trail I usually add a note when I do it but just forgot this time.=================================================Creating tomorrow's legacy systems today. One crisis at a time. |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
RogerSGI
Starting Member
9 Posts |
Posted - 2008-01-07 : 15:33:42
|
I am looking at something similar but can't wrap how the Refer point 2 works.I have a Material Number, Date, Remark.From this record set I would like the MatNo and Remark field from the lastest date.Sample data coming in.MatNo Date Remark 64281 2007-12-28 00:00:00.000 27999 64281 2007-09-18 00:00:00.000 94235- 9-18-07 64281 2007-06-27 00:00:00.000 92947- 6-27-07 64281 2006-03-16 00:00:00.000 86665- 3-16-06 64281 2005-10-13 00:00:00.000 85211 / 10-13-05 64283 2007-10-18 00:00:00.000 94384 / 10/18/07 64283 2006-03-24 00:00:00.000 86701- 3-24-06 64283 2005-10-13 00:00:00.000 85211-- 10-13-05 65692 2007-10-18 00:00:00.000 94391- 10-18-07 65692 2007-06-27 00:00:00.000 92948- 6-27-07 65692 2007-05-08 00:00:00.000 92080- 5/08/07 67479 2007-11-05 00:00:00.000 94846 67479 2007-11-02 00:00:00.000 94846Result data needed 64281 2007-12-28 00:00:00.000 27999 64283 2007-10-18 00:00:00.000 94384 / 10/18/07 65692 2007-10-18 00:00:00.000 94391- 10-18-07 67479 2007-11-05 00:00:00.000 94846Any ideas? |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-01-08 : 00:28:08
|
Select t1.* from table t inner join (Select Matno,max(date) as date from table group by Matno) as t2 on t1.Matno=t2.Matno and t1.date=t2.dateMadhivananFailing to plan is Planning to fail |
 |
|
RogerSGI
Starting Member
9 Posts |
Posted - 2008-01-08 : 14:09:51
|
Thanks this helped. |
 |
|
|