Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
wkm1925
Posting Yak Master
207 Posts |
Posted - 2008-05-08 : 21:14:16
|
| I've have these following tabletbllocationMain_ID | Date_Taken | Time |Hit -----------------------------------------206 | 5/9/2008 | 100 | 2206 | 5/9/2008 | 200 | 3206 | 5/6/2008 | 300 | 6201 | 5/1/2008 | 400 | 5201 | 5/4/2008 | 500 | 9201 | 5/7/2008 | 600 | 2204 | 5/2/2008 | 700 | 2204 | 5/3/2008 | 800 | 4204 | 5/6/2008 | 900 | 2203 | 5/7/2008 | 100 | 2203 | 5/8/2008 | 200 | 3203 | 5/9/2008 | 300 | 6202 | 5/4/2008 | 400 | 5202 | 5/3/2008 | 500 | 9202 | 5/8/2008 | 200 | 3205 | 5/2/2008 | 300 | 6205 | 5/1/2008 | 400 | 5205 | 5/9/2008 | 500 | 9tblSetValueMain_ID | Hit2---------------206 | 3201 | 5204 | 3203 | 1202 | 8205 | 7*Main_ID is a primary keyCondition1. Let's say, the current date is 5/9/20082. Result only display the last 7 days data. From above data. it's mean only pickup from 5/3/2008 to 5/9/20083. Every Main_ID only pickup the MAX Hit4. Diff (column on the fly) = Hit - Hit2The expected result shown as followtblResultMain_ID | Date_Taken | Time | Hit | Hit2 | Diff-----------------------------------------------206 | 5/6/2008 | 300 | 6 | 3 | 3201 | 5/4/2008 | 500 | 9 | 5 | 4204 | 5/3/2008 | 800 | 4 | 3 | 1203 | 5/9/2008 | 300 | 6 | 1 | 5............Anyone can help me to built the query? |
|
|
eralper
Yak Posting Veteran
66 Posts |
Posted - 2008-05-09 : 01:45:00
|
| Hello,You can read the article http://www.kodyaz.com/articles/top-n-random-rows-foreach-category-or-group.aspx for selecting top n rows for each group on a data set.For your case you can select top 1 ordered by max for each main_id using the method summarized in the article.The main thing is using the ROW_NUMBER() OVER (PARTITION BY ... ORDER BY ...) syntax.You can partition your results due to MAIN_ID and order them by HIT number. This will give you a row number and you can select only the rows with row number equal to 1.Of course you should eliminate the records out of the 7 days range prior taking the row number in a where clause.I hope this helps,Eralperhttp://www.kodyaz.com-------------Eralperhttp://www.kodyaz.com |
 |
|
|
wkm1925
Posting Yak Master
207 Posts |
Posted - 2008-05-09 : 07:49:47
|
| do you have more simple explanation and example? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-09 : 07:57:38
|
[code]SELECT t1.Main_ID, t1.Date_Taken, t1.Time, t1.Hit, x.Hit2, t1.Hit - x.Hit2 AS DiffFROM tblLocation AS t1INNER JOIN ( SELECT Main_ID, MAX(Hit) AS maxHit FROM tblLocation WHERE Date_Taken >= DATEADD(DAY, -7, GETDATE()) ) AS t2 ON t2.Main_ID = t1.Main_IDINNER JOIN tblSetValue AS x ON x.Main_ID = t1.Main_IDWHERE t2.maxHit = t1.Hit AND t1.Date_Taken >= DATEADD(DAY, -7, GETDATE())[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
wkm1925
Posting Yak Master
207 Posts |
Posted - 2008-05-09 : 10:26:24
|
| Kindest Mr. Peso,Your query sample help me a lot. How about if the table shown as follow,Main_ID | Date_Taken | Time |Hit -----------------------------------------206 | 5/9/2008 | 100 | 2206 | 5/9/2008 | 200 | 3206 | 5/9/2008 | 300 | 6206 | 5/9/2008 | 400 | 5206 | 5/8/2008 | 100 | 9206 | 5/8/2008 | 200 | 2206 | 5/8/2008 | 300 | 2206 | 5/8/2008 | 400 | 4204 | 5/9/2008 | 100 | 2204 | 5/9/2008 | 200 | 3204 | 5/9/2008 | 300 | 2204 | 5/9/2008 | 400 | 2204 | 5/8/2008 | 100 | 2204 | 5/8/2008 | 200 | 3204 | 5/8/2008 | 300 | 2204 | 5/8/2008 | 400 | 2.........We can see, 1 Main_ID have a many Date_Taken, 1 Date_Taken have many Time.In 1 day, just cater the MAX hit? The expected result shown as followtblResultMain_ID | Date_Taken | Time | Hit------------------------------------206 | 5/9/2008 | 300 | 6206 | 5/8/2008 | 100 | 9204 | 5/9/2008 | 200 | 3204 | 5/8/2008 | 200 | 3......... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-09 : 10:34:13
|
quote: Originally posted by wkm1925 Kindest Mr. Peso,Your query sample help me a lot. How about if the table shown as follow,Main_ID | Date_Taken | Time |Hit -----------------------------------------206 | 5/9/2008 | 100 | 2206 | 5/9/2008 | 200 | 3206 | 5/9/2008 | 300 | 6206 | 5/9/2008 | 400 | 5206 | 5/8/2008 | 100 | 9206 | 5/8/2008 | 200 | 2206 | 5/8/2008 | 300 | 2206 | 5/8/2008 | 400 | 4204 | 5/9/2008 | 100 | 2204 | 5/9/2008 | 200 | 3204 | 5/9/2008 | 300 | 2204 | 5/9/2008 | 400 | 2204 | 5/8/2008 | 100 | 2204 | 5/8/2008 | 200 | 3204 | 5/8/2008 | 300 | 2204 | 5/8/2008 | 400 | 2.........We can see, 1 Main_ID have a many Date_Taken, 1 Date_Taken have many Time.In 1 day, just cater the MAX hit? The expected result shown as followtblResultMain_ID | Date_Taken | Time | Hit------------------------------------206 | 5/9/2008 | 300 | 6206 | 5/8/2008 | 100 | 9204 | 5/9/2008 | 200 | 3204 | 5/8/2008 | 200 | 3.........
If you're using sql 2000SELECT t1.Main_ID,t1.Date_Taken,t1.Time,t1.HitFROM YourTable t1INNER JOIN (SELECT Main_ID ,Date_Taken ,MAX(Hit) AS MaxHit FROM YourTable GROUP BY Main_ID ,Date_Taken)t2ON t2.Main_ID = t1.Main_IDAND t2.Date_Taken=t1.Date_TakenAND t2.MaxHit=t1.Hit And if sql 2005:-SELECT t.Main_ID,t.Date_Taken,t.Time,t.HitFROM(SELECT ROW_NUMBER() OVER (PARTITION BY Main_ID ,Date_Taken ORDER BY Hit DESC) AS RowNo,*FROM YourTable)tWHERE t.RowNo=1 |
 |
|
|
wkm1925
Posting Yak Master
207 Posts |
Posted - 2008-05-10 : 01:23:52
|
| Kindest mr. eralper, mr. peso and mr. visakh16,tq to all of you. your info is really great. |
 |
|
|
|
|
|
|
|