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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help using case statement

Author  Topic 

wkm1925
Posting Yak Master

207 Posts

Posted - 2008-06-07 : 06:59:40
I've these following table
SEL
Date_Taken | Main_ID | Time | Daily_Rainfall
---------------------------------------------------
...
...
...
4/3/2005 | 202 | 1015 | 2
4/3/2005 | 202 | 1045 | 2
4/3/2005 | 202 | 1215 | 7
4/3/2005 | 203 | 715 | 2
4/3/2005 | 203 | 1345 | 2
4/3/2005 | 203 | 1530 | 7
...
...
...
5/29/2005 | 203 | 1100 | 56
5/29/2005 | 203 | 1130 | 156
5/29/2005 | 203 | 1145 | 256
...
...
...


Station_Info
State | Main_ID
--------------------
SEL | 202
SEL | 203
SEL | 204
SEL | 205
SEL | 209

Main_ID | Month_Year | 3MthCumRf | 6MthCumRf | 9MthCumRf | 12MthCumRf
------------------------------------------------------------------------------
202 | 4/30/2005 | 525.8 | 683.11 | 356.33 | 754.33
203 | 4/30/2005 | 435.5 | 400.9 | 290.34 | 234.34
204 | 4/30/2005 | 265.53 | 453.21 | 543.66 | 753.24
205 | 4/30/2005 | 251.38 | 754.33 | 478.34 | 785.22
209 | 4/30/2005 | 259.5 | 356.34 | 894.33 | 354.78
202 | 5/30/2005 | 565.8 | 383.11 | 756.33 | 254.33
203 | 5/30/2005 | 485.5 | 444.9 | 744.34 | 755.34



This query only display Max(Daily_Rainfall) per day from SEL


SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL,
CASE WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=3
THEN t3.[3MthCumRf]
WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=6
THEN t3.[6MthCumRf]
WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=9
THEN t3.[9MthCumRf]
WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=12
THEN t3.[12MthCumRf]
END AS CumRf
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1 INNER JOIN dbo.LT t3 ON t3.STATION_ID =t2.STATION_ID
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL'
ORDER BY MAIN_ID,DATE_TAKEN,TIME

In my query, i still can query if last3month using CASE WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=3 THEN t3.[3MthCumRf]

How to adjust my CASE statement
if Main_ID=203, Date_Taken=5/5/2005,

i should pickup following rows
Main_ID | Month_Year | 3MthCumRf | 6MthCumRf | 9MthCumRf | 12MthCumRf
-------------------------------------------------------------------
203 | 5/30/2005 | 485.5 | 444.9 | 744.34 | 755.34

if Main_ID=203, Date_Taken=4/5/2005,
i should pickup following rows
Main_ID | Month_Year | 3MthCumRf | 6MthCumRf | 9MthCumRf | 12MthCumRf
203 | 4/30/2005 | 435.5 | 400.9 | 290.34 | 234.34

Need someone help... plz

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-07 : 09:36:31
Can you explain you requirement in words please? The query seems like you're taking all the greatest rainfall records per day from 3 months back to current date but the sample o/p reqd suggest you need to find the record for last day of month.
Go to Top of Page

wkm1925
Posting Yak Master

207 Posts

Posted - 2008-06-07 : 09:56:20
SEL
Date_Taken | Main_ID | Time | Daily_Rainfall
---------------------------------------------------
...
...
...
4/3/2005 | 202 | 1015 | 2
4/3/2005 | 202 | 1045 | 2
4/3/2005 | 202 | 1215 | 7
4/3/2005 | 203 | 715 | 2
4/3/2005 | 203 | 1345 | 2
4/3/2005 | 203 | 1530 | 7
...
...
...
5/29/2005 | 203 | 1100 | 56
5/29/2005 | 203 | 1130 | 156
5/29/2005 | 203 | 1145 | 256
...
...
...
**This table contains rainfall value for each Main_ID from time to time

Station_Info
State | Main_ID
--------------------
SEL | 202
SEL | 203
SEL | 204
SEL | 205
SEL | 209
...
...
***This table contains Main_ID location
LT
Main_ID | Month_Year | 3MthCumRf | 6MthCumRf | 9MthCumRf | 12MthCumRf
------------------------------------------------------------------------------
202 | 4/30/2005 | 525.8 | 683.11 | 356.33 | 754.33
203 | 4/30/2005 | 435.5 | 400.9 | 290.34 | 234.34
204 | 4/30/2005 | 265.53 | 453.21 | 543.66 | 753.24
205 | 4/30/2005 | 251.38 | 754.33 | 478.34 | 785.22
209 | 4/30/2005 | 259.5 | 356.34 | 894.33 | 354.78
202 | 5/30/2005 | 565.8 | 383.11 | 756.33 | 254.33
203 | 5/30/2005 | 485.5 | 444.9 | 744.34 | 755.34
***This table contains Main_ID CumRfValue. Combination Main_ID and Month_Year give a unique value.

I run query below,
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL,
CASE WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=3
THEN t3.[3MthCumRf]
WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=6
THEN t3.[6MthCumRf]
WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=9
THEN t3.[9MthCumRf]
WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=12
THEN t3.[12MthCumRf]
END AS CumRf
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1 INNER JOIN dbo.LT t3 ON t3.STATION_ID =t2.STATION_ID
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL'
ORDER BY MAIN_ID,DATE_TAKEN,TIME

In 1 day having so many Daily_Rainfall.
So, the above query, only display the MAX(Daily_Rainfall) in each Date_Taken.

I dont know to pickup from LT table, which is consist Main_ID and Month_Year.
If from SEL, Main_ID=202 and Date_Taken=4/5/2005, how to pick up from LT table below row
Main_ID | Month_Year | 3MthCumRf | 6MthCumRf | 9MthCumRf | 12MthCumRf
------------------------------------------------------------------------------
202 | 4/30/2005 | 525.8 | 683.11 | 356.33 | 754.33

not
202 | 5/30/2005 | 565.8 | 383.11 | 756.33 | 254.33

plz help me to adjust the above query

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-07 : 10:22:36
May be this:-

SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL,
CASE WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=3
THEN t3.[3MthCumRf]
WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=6
THEN t3.[6MthCumRf]
WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=9
THEN t3.[9MthCumRf]
WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=12
THEN t3.[12MthCumRf]
END AS CumRf
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1 INNER JOIN
CROSS APPLY (SELECT TOP 1 * FROM dbo.LT
WHERE STATION_ID =t2.STATION_ID
AND Month_Year <=t1.DATE_TAKEN
ORDER BY Month_Year DESC)t3


WHERE t2.STATE='SEL'
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
ORDER BY MAIN_ID,DATE_TAKEN,TIME
Go to Top of Page

wkm1925
Posting Yak Master

207 Posts

Posted - 2008-06-07 : 10:29:13
shows errow below,
Msg 156, Level 15, State 1, Line 17
Incorrect syntax near the keyword 'CROSS'.
Msg 156, Level 15, State 1, Line 20
Incorrect syntax near the keyword 'ORDER'.

CROSS APPLY (SELECT TOP 1 * FROM dbo.LT
WHERE STATION_ID =t2.STATION_ID
AND Month_Year <=t1.DATE_TAKEN
ORDER BY Month_Year DESC)t3

In LT table there's no STATION_ID column
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-07 : 12:36:49
quote:
Originally posted by wkm1925

shows errow below,
Msg 156, Level 15, State 1, Line 17
Incorrect syntax near the keyword 'CROSS'.
Msg 156, Level 15, State 1, Line 20
Incorrect syntax near the keyword 'ORDER'.

CROSS APPLY (SELECT TOP 1 * FROM dbo.LT
WHERE STATION_ID =t2.STATION_ID
AND Month_Year <=t1.DATE_TAKEN
ORDER BY Month_Year DESC)t3

In LT table there's no STATION_ID column


But you had used STATION_ID in your posted query. i just modified what you gave me as sample query.May be this will work.

SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL,
CASE WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=3
THEN t3.[3MthCumRf]
WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=6
THEN t3.[6MthCumRf]
WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=9
THEN t3.[9MthCumRf]
WHEN DATEDIFF(mm,t1.DATE_TAKEN,GETDATE()) <=12
THEN t3.[12MthCumRf]
END AS CumRf
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2
ON t2.MAIN_ID=t1.MAIN_ID
AND t1.RowNo=1
CROSS APPLY (SELECT TOP 1 * FROM dbo.LT
WHERE MAIN_ID =t2.MAIN_ID
AND Month_Year <=t1.DATE_TAKEN
ORDER BY Month_Year DESC)t3
WHERE t2.STATE='SEL'
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
ORDER BY MAIN_ID,DATE_TAKEN,TIME
Go to Top of Page

wkm1925
Posting Yak Master

207 Posts

Posted - 2008-06-07 : 13:43:29
may be my LT table not having a good design and a rubbish. that's giving a problem. i'm trying to ignore the LT table.

SEL
Date_Taken | Main_ID | Time | Daily_Rainfall
---------------------------------------------------
...
...
...
4/2/2005 | 202 | 1015 | 12
4/2/2005 | 202 | 1045 | 76
4/2/2005 | 202 | 1215 | 17
4/3/2005 | 202 | 1015 | 2
4/3/2005 | 202 | 1045 | 2
4/3/2005 | 202 | 1215 | 7
4/3/2005 | 203 | 715 | 2
4/3/2005 | 203 | 1345 | 2
4/3/2005 | 203 | 1530 | 7
...
...
...
5/29/2005 | 203 | 1100 | 56
5/29/2005 | 203 | 1130 | 156
5/29/2005 | 203 | 1145 | 256
5/30/2005 | 203 | 1130 | 89
5/30/2005 | 203 | 1145 | 77
...
...
...
**This table contains rainfall value for each Main_ID from time to time

Station_Info
State | Main_ID
--------------------
SEL | 202
SEL | 203
SEL | 204
SEL | 205
SEL | 209
...
...
***This table contains Main_ID location

These following query will display MAX Daily_Rainfall for each Main_ID, each Date_Taken.
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL'
ORDER BY MAIN_ID,DATE_TAKEN,TIME

It will display as follow:-
Date_Taken | Main_ID | Time | Daily_Rainfall
-------------------------------------------------
...
...
4/2/2005 | 202 | 1045 | 76
4/3/2005 | 202 | 1215 | 7
...
...
5/29/2005| 203 | 1145 | 256
5/30/2005| 203 | 1130 | 89
...
...

I hope Mr. Visakh can show me the best way to do the SUM(Daily_Rainfall) each month, so the result shown as follow

Date_Taken |Main_ID | Daily-Rainfall
------------------------------------------------
...
...
4/1/2005 | 202 | 83
5/1/2005 | 203 | 345
**Date_Taken will display starting day of the month 4/1/2005, 5/1/2005, 6/1/2005

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-07 : 15:23:14
[code]SELECT CAST(CAST(m.Month AS varchar(2)) + '/1/' + CAST(m.Year AS varchar(4)) AS datetime) AS Date_Taken,m.Main_ID,m.Daily_Rainfall
FROM
(SELECT MONTH(s.Date_Taken) AS Month,
YEAR(s.Date_Taken) AS Year,
Main_ID,
SUM(s.Daily_Rainfall) As Daily_Rainfall
FROM SEL s
INNER JOIN Station_Info si
ON si.Main_ID=s.Main_ID
WHERE si.State='SEL'
GROUP BY MONTH(s.Date_Taken),YEAR(s.Date_Taken),Main_ID)m
ORDER BY Main_ID,Date_Taken[/code]
Go to Top of Page

wkm1925
Posting Yak Master

207 Posts

Posted - 2008-06-07 : 15:34:15
where should i put
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL'

in query

SELECT CAST(CAST(m.Month AS varchar(2)) + '/1/' + CAST(m.Year AS varchar(4)) AS datetime) AS Date_Taken,m.Main_ID,m.Daily_Rainfall
FROM
(SELECT MONTH(s.Date_Taken) AS Month,
YEAR(s.Date_Taken) AS Year,
Main_ID,
SUM(s.Daily_Rainfall) As Daily_Rainfall
FROM SEL s
INNER JOIN Station_Info si
ON si.Main_ID=s.Main_ID
WHERE si.State='SEL'
GROUP BY MONTH(s.Date_Taken),YEAR(s.Date_Taken),Main_ID)m
ORDER BY Main_ID,Date_Taken
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-07 : 15:47:24
quote:
Originally posted by wkm1925

where should i put
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL'

in query

SELECT CAST(CAST(m.Month AS varchar(2)) + '/1/' + CAST(m.Year AS varchar(4)) AS datetime) AS Date_Taken,m.Main_ID,m.Daily_Rainfall
FROM
(SELECT MONTH(s.Date_Taken) AS Month,
YEAR(s.Date_Taken) AS Year,
Main_ID,
SUM(s.Daily_Rainfall) As Daily_Rainfall
FROM SEL s
INNER JOIN Station_Info si
ON si.Main_ID=s.Main_ID
WHERE si.State='SEL'
GROUP BY MONTH(s.Date_Taken),YEAR(s.Date_Taken),Main_ID)m
ORDER BY Main_ID,Date_Taken




Didnt understand that. The second query itself will give you the posted output.
Go to Top of Page

wkm1925
Posting Yak Master

207 Posts

Posted - 2008-06-07 : 15:57:51
SEL
Date_Taken | Main_ID | Time | Daily_Rainfall
---------------------------------------------------
...
...
...
4/2/2005 | 202 | 1015 | 12
4/2/2005 | 202 | 1045 | 76
4/2/2005 | 202 | 1215 | 17
4/3/2005 | 202 | 1015 | 2
4/3/2005 | 202 | 1045 | 2
4/3/2005 | 202 | 1215 | 7
4/3/2005 | 203 | 715 | 2
4/3/2005 | 203 | 1345 | 2
4/3/2005 | 203 | 1530 | 7
...
...
...
5/29/2005 | 203 | 1100 | 56
5/29/2005 | 203 | 1130 | 156
5/29/2005 | 203 | 1145 | 256
5/30/2005 | 203 | 1130 | 89
5/30/2005 | 203 | 1145 | 77
...
...
...
**This table contains rainfall value for each Main_ID from time to time

Station_Info
State | Main_ID
--------------------
SEL | 202
SEL | 203
SEL | 204
SEL | 205
SEL | 209
...
...
***This table contains Main_ID location
so, i run these
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL'
to display

It will display as follow:-
Date_Taken | Main_ID | Time | Daily_Rainfall
-------------------------------------------------
...
...
4/2/2005 | 202 | 1045 | 76
4/3/2005 | 202 | 1215 | 7
...
...
5/29/2005| 203 | 1145 | 256
5/30/2005| 203 | 1130 | 89
...
...
** Each day will display the MAX(Daily_Rainfall).

I hope mr. visakh show me how to query from subquery as follow

Select ******, may SUM statement will be here... ==>
from(SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL')s
.... it is true?
The result will every sum(all month), so i get result as follow
Date_Taken |Main_ID | Daily-Rainfall
------------------------------------------------
...
...
4/1/2005 | 202 | 83
5/1/2005 | 203 | 345
**Date_Taken will display starting day of the month 4/1/2005, 5/1/2005, 6/1/2005
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-08 : 05:49:17
[code]SELECT CAST(CAST(m.Month AS varchar(2)) + '/1/' + CAST(m.Year AS varchar(4)) AS datetime) AS Date_Taken,m.Main_ID,m.Daily_Rainfall
FROM
(
SELECT MONTH(t.Date_Taken) AS Month,
YEAR(t.Date_Taken) AS Year,
t.Main_ID,
SUM(t.Daily_Rainfall) As Daily_Rainfall
FROM
(
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL')t
GROUP BY MONTH(t.Date_Taken),YEAR(t.Date_Taken),t.Main_ID)m
ORDER BY Main_ID,Date_Taken[/code]
Go to Top of Page

wkm1925
Posting Yak Master

207 Posts

Posted - 2008-06-08 : 08:58:28
awesome mr. visakh.

**Let's say, GETDATE()=8/31/2005, so the result shown as follow
Date_Taken | Main_ID | Daily_Rainfall
-------------------------------------------
5/1/2005 | 194 | 5
6/1/2005 | 194 | 136
7/1/2005 | 194 | 142
8/1/2005 | 194 | 160
5/1/2005 | 195 | 0
6/1/2005 | 195 | 80
7/1/2005 | 195 | 330
8/1/2005 | 195 | 34
...
...
I do my checking and it very accurate. Really appreciate it

I'm stuck to adjust this query
SELECT CAST(CAST(m.Month AS varchar(2)) + '/1/' + CAST(m.Year AS varchar(4)) AS datetime) AS
Date_Taken,m.Main_ID,m.Daily_Rainfall
FROM
(
SELECT MONTH(t.Date_Taken) AS Month,
YEAR(t.Date_Taken) AS Year,
t.Main_ID,
SUM(t.Daily_Rainfall) As Daily_Rainfall
FROM
(
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL')t
GROUP BY MONTH(t.Date_Taken),YEAR(t.Date_Taken),t.Main_ID)m
ORDER BY Main_ID,Date_Taken


to show the result arranged as below
Date_Taken | Main_ID | CurrentMonth_DRainfall | LastMonth_DRainfall | Last2Month_DRainfall | Last3Month_DRainfall
-----------------------------------------------------------------------------------------------------------------------------
8/1/2005 | 194 | 160 | 142 | 136 | 5
8/1/2005 | 195 | 34 | 330 | 80 | 0
...
...

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-08 : 10:14:16
[code]SELECT CAST(CAST(m.Month AS varchar(2)) + '/1/' + CAST(m.Year AS varchar(4)) AS datetime) AS
Date_Taken,m.Main_ID,m.Daily_Rainfall
FROM
(
SELECT MONTH(t.Date_Taken) AS Month,
YEAR(t.Date_Taken) AS Year,
t.Main_ID,
SUM(t.Daily_Rainfall) As Daily_Rainfall
FROM
(
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL')t
GROUP BY MONTH(t.Date_Taken),YEAR(t.Date_Taken),t.Main_ID)m
ORDER BY Date_Taken DESC,Main_ID ASC[/code]
Go to Top of Page

wkm1925
Posting Yak Master

207 Posts

Posted - 2008-06-08 : 10:24:07
sir,

the above query list down result from above to down. how to make it a result from left to right as follow
to show the result arranged as below

Date_Taken | Main_ID | CurrentMonth_DRainfall | LastMonth_DRainfall | Last2Month_DRainfall | Last3Month_DRainfall
-----------------------------------------------------------------------------------------------------------------------------
8/1/2005 | 194 | 160 | 142 | 136 | 5
8/1/2005 | 195 | 34 | 330 | 80 | 0
...
...


so, Main_ID wont repeat and Date_Taken is same.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-08 : 10:57:37
[code]DECLARE @MonthList varchar(8000),@Sql varchar(8000)

SELECT @MonthList=LEFT(ml.MonthList,LEN(ml.MonthList)-1)
FROM (
SELECT Date_Taken + ',' AS [text()]
FROM
(
SELECT CAST(m.Month AS varchar(2)) + '/1/' + CAST(m.Year AS varchar(4)) AS
Date_Taken,m.Main_ID,m.Daily_Rainfall
FROM
(
SELECT MONTH(t.Date_Taken) AS Month,
YEAR(t.Date_Taken) AS Year,
t.Main_ID,
SUM(t.Daily_Rainfall) As Daily_Rainfall
FROM
(
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL')t
GROUP BY MONTH(t.Date_Taken),YEAR(t.Date_Taken),t.Main_ID)m
ORDER BY Date_Taken DESC,Main_ID ASC)r
WHERE r.Date_Taken<=@Date
FOR XML PATH('')
)ml(MonthList)


SELECT @Sql='
SELECT * FROM
(
SELECT CAST(CAST(m.Month AS varchar(2)) + '/1/' + CAST(m.Year AS varchar(4)) AS datetime) AS
Date_Taken,m.Main_ID,m.Daily_Rainfall
FROM
(
SELECT MONTH(t.Date_Taken) AS Month,
YEAR(t.Date_Taken) AS Year,
t.Main_ID,
SUM(t.Daily_Rainfall) As Daily_Rainfall
FROM
(
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL')t
GROUP BY MONTH(t.Date_Taken),YEAR(t.Date_Taken),t.Main_ID)m
ORDER BY Date_Taken DESC,Main_ID ASC)r
PIVOT SUM(r.Daily_Rainfall) FOR r.Date_Taken IN (['+ REPLACE(@MonthList,',','],[') + '])p
ORDER BY r.Date_Taken DESC,r.Main_ID'

EXEC(@Sql)[/code]
Go to Top of Page

wkm1925
Posting Yak Master

207 Posts

Posted - 2008-06-08 : 11:06:11
i'm try copy and paste, then run. it shows an error as follow

Msg 1033, Level 15, State 1, Line 29
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
Msg 102, Level 15, State 1, Line 57
Incorrect syntax near 'SEL'.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-08 : 14:09:05
quote:
Originally posted by wkm1925

i'm try copy and paste, then run. it shows an error as follow

Msg 1033, Level 15, State 1, Line 29
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
Msg 102, Level 15, State 1, Line 57
Incorrect syntax near 'SEL'.




DECLARE @MonthList varchar(8000),@Sql varchar(8000)

SELECT @MonthList=LEFT(ml.MonthList,LEN(ml.MonthList)-1)
FROM (
SELECT Date_Taken + ',' AS [text()]
FROM
(
SELECT CAST(m.Month AS varchar(2)) + '/1/' + CAST(m.Year AS varchar(4)) AS
Date_Taken,m.Main_ID,m.Daily_Rainfall
FROM
(
SELECT MONTH(t.Date_Taken) AS Month,
YEAR(t.Date_Taken) AS Year,
t.Main_ID,
SUM(t.Daily_Rainfall) As Daily_Rainfall
FROM
(
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL')t
GROUP BY MONTH(t.Date_Taken),YEAR(t.Date_Taken),t.Main_ID)m
ORDER BY Date_Taken DESC,Main_ID ASC)r
WHERE r.Date_Taken<=@Date
FOR XML PATH('')
)ml(MonthList)


SELECT @Sql='
SELECT * FROM
(
SELECT CAST(CAST(m.Month AS varchar(2)) + '/1/' + CAST(m.Year AS varchar(4)) AS datetime) AS
Date_Taken,m.Main_ID,m.Daily_Rainfall
FROM
(
SELECT MONTH(t.Date_Taken) AS Month,
YEAR(t.Date_Taken) AS Year,
t.Main_ID,
SUM(t.Daily_Rainfall) As Daily_Rainfall
FROM
(
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL')t
GROUP BY MONTH(t.Date_Taken),YEAR(t.Date_Taken),t.Main_ID)m
ORDER BY Date_Taken DESC,Main_ID ASC)r
PIVOT SUM(r.Daily_Rainfall) FOR r.Date_Taken IN (['+ REPLACE(@MonthList,',','],[') + '])p
ORDER BY r.Date_Taken DESC,r.Main_ID'

EXEC(@Sql)
Go to Top of Page

wkm1925
Posting Yak Master

207 Posts

Posted - 2008-06-08 : 18:36:08
DECLARE @MonthList varchar(8000),@Sql varchar(8000)

SELECT @MonthList=LEFT(ml.MonthList,LEN(ml.MonthList)-1)
FROM (
SELECT Date_Taken + ',' AS [text()]
FROM
(
SELECT CAST(m.Month AS varchar(2)) + '/1/' + CAST(m.Year AS varchar(4)) AS
Date_Taken,m.Main_ID,m.Daily_Rainfall
FROM
(
SELECT MONTH(t.Date_Taken) AS Month,
YEAR(t.Date_Taken) AS Year,
t.Main_ID,
SUM(t.Daily_Rainfall) As Daily_Rainfall
FROM
(
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL')t
GROUP BY MONTH(t.Date_Taken),YEAR(t.Date_Taken),t.Main_ID)m
ORDER BY Date_Taken DESC,Main_ID ASC)r
WHERE r.Date_Taken<=@Date
FOR XML PATH('')
)ml(MonthList)


SELECT @Sql='
SELECT * FROM
(
SELECT CAST(CAST(m.Month AS varchar(2)) + '/1/' + CAST(m.Year AS varchar(4)) AS datetime) AS
Date_Taken,m.Main_ID,m.Daily_Rainfall
FROM
(
SELECT MONTH(t.Date_Taken) AS Month,
YEAR(t.Date_Taken) AS Year,
t.Main_ID,
SUM(t.Daily_Rainfall) As Daily_Rainfall
FROM
(
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL')t
GROUP BY MONTH(t.Date_Taken),YEAR(t.Date_Taken),t.Main_ID)m
)r
PIVOT SUM(r.Daily_Rainfall) FOR r.Date_Taken IN (['+ REPLACE(@MonthList,',','],[') + '])p
ORDER BY r.Date_Taken DESC,r.Main_ID'

EXEC(@Sql)

still having an error

Msg 1033, Level 15, State 1, Line 29
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
Msg 102, Level 15, State 1, Line 57
Incorrect syntax near 'SEL'.


I'm really stuck...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-09 : 00:58:18
Made some changes. Hope this will work for you.
DECLARE @MonthList varchar(8000),@Sql varchar(8000)

SELECT @MonthList=LEFT(ml.MonthList,LEN(ml.MonthList)-1)
FROM (
SELECT Date_Taken + ',' AS [text()]
FROM
(
SELECT TOP 100 PERCENT CAST(m.Month AS varchar(2)) + '/1/' + CAST(m.Year AS varchar(4)) AS
Date_Taken,m.Main_ID,m.Daily_Rainfall
FROM
(
SELECT MONTH(t.Date_Taken) AS Month,
YEAR(t.Date_Taken) AS Year,
t.Main_ID,
SUM(t.Daily_Rainfall) As Daily_Rainfall
FROM
(
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE='SEL')t
GROUP BY MONTH(t.Date_Taken),YEAR(t.Date_Taken),t.Main_ID)m
ORDER BY Date_Taken DESC,Main_ID ASC)r
WHERE r.Date_Taken<=@Date
FOR XML PATH('')
)ml(MonthList)


SELECT @Sql='
SELECT * FROM
(
SELECT CAST(CAST(m.Month AS varchar(2)) + '''/1/''' + CAST(m.Year AS varchar(4)) AS datetime) AS
Date_Taken,m.Main_ID,m.Daily_Rainfall
FROM
(
SELECT MONTH(t.Date_Taken) AS Month,
YEAR(t.Date_Taken) AS Year,
t.Main_ID,
SUM(t.Daily_Rainfall) As Daily_Rainfall
FROM
(
SELECT t1.MAIN_ID,t1.DATE_TAKEN,t1.TIME,
t1.DAILY_RAINFALL
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY DATE_TAKEN,MAIN_ID
ORDER BY DAILY_RAINFALL DESC) AS RowNo,* FROM dbo.SEL)t1
INNER JOIN dbo.STATION_INFO t2 ON t2.MAIN_ID=t1.MAIN_ID AND
t1.RowNo=1
AND t1.DATE_TAKEN>=CONVERT(VARCHAR(10),DATEADD(m,-3,GETDATE()),101)
AND t1.DATE_TAKEN<CONVERT(VARCHAR(10), GETDATE(), 101)
WHERE t2.STATE=''SEL''
)t
GROUP BY MONTH(t.Date_Taken),YEAR(t.Date_Taken),t.Main_ID
)m
)r
PIVOT SUM(r.Daily_Rainfall) FOR r.Date_Taken IN (['+ REPLACE(@MonthList,',','],[') + '])p
ORDER BY r.Date_Taken DESC,r.Main_ID'

EXEC(@Sql)
Go to Top of Page

wkm1925
Posting Yak Master

207 Posts

Posted - 2008-06-09 : 01:32:18
there's some error shown as follow

Msg 245, Level 16, State 1, Line 35
Conversion failed when converting the varchar value '
SELECT * FROM
(
SELECT CAST(CAST(m.Month AS varchar(2)) + '' to data type int.


My Date_Taken field is a SmallDateTime. When do the query below
select distinct date_taken
from SEL
where main_id=206 and date_taken='4/3/2004'


Output
2004-04-03 00:00:00
Go to Top of Page
    Next Page

- Advertisement -