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
 Modifying the query

Author  Topic 

sqlbug
Posting Yak Master

201 Posts

Posted - 2009-01-05 : 12:37:54
We have two tables – lets call them SAMPLE_DATA and REAL_DATA tables containing basic information about the data and details data, respectively. Sample_num from the sample_data is the foreign key in real_data table.
In the real_data table, data for each hour of the day are stored in separate rows. Before, we had one sample number for the whole day, for a plot. Now, we have 24 sample numbers for the 24 hours of the day for a plot. As a result – we need to change the query that shows output on a webpage as a table of all 24 values for a day in each row. Because (reminder) – before, we had one sample number for a day for a plot, now we have 24 sample number for 24 hourly values.
Here’s what we have now in short:

SELECT RDATA_SAMPLE_NUM, METHOD_NAME Method, UNIT_ABBREV Unit, SUM(Hour1) AS Hour1, SUM(Hour2) AS Hour2,......SUM(Hour24) AS Hour24 FROM
(SELECT DISTINCT RDATA_SAMPLE_NUM, METHOD_NAME, UNIT_ABBREV,
(CASE RDATA_HOUR WHEN 1 THEN ISNULL(CONVERT(decimal(4,4),RDATA_VALUE/10000.0), 0) END) AS Hour1,
(CASE RDATA_HOUR WHEN 2 THEN ISNULL(CONVERT(decimal(4,4),RDATA_VALUE/10000.0), 0) END) AS Hour2,......
(CASE RDATA_HOUR WHEN 24 THEN ISNULL(CONVERT(decimal(4,4),RDATA_VALUE/10000.0), 0) END) AS Hour24,
FROM REAL_DATA,SAMPLE_DATA,......
WHERE RDATA_SAMPLE_NUM = SAMPLE_NUM
AND DATEPART("MM", SAMPLE_START_DATE) = ...
AND DATEPART("YYYY", SAMPLE_START_DATE) = ...
GROUP BY ...) S
GROUP BY ...

Will highly appreciate your help, thanks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-05 : 12:41:52
so whats will be output format?
Go to Top of Page

sqlbug
Posting Yak Master

201 Posts

Posted - 2009-01-05 : 14:34:12
Output was tied to a gridview to display like:

Method Unit Hour1 Hour2 ... Hour24
------- ---- ----- ------ ...-------
Method Unit 0.011 0.923 ... 0.008
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-06 : 02:56:55
Are you using sql 2005?
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-01-06 : 05:43:53
If you are on sql2000, you could use something like this-

SELECT  RDATA_SAMPLE_NUM, METHOD_NAME as Method, UNIT_ABBREV as Unit,
Sum(CASE WHEN RDATA_HOUR = 1 THEN RDATA_VALUE/10000.0 END) AS Hour1,
Sum(CASE WHEN RDATA_HOUR = 2 THEN RDATA_VALUE/10000.0 END) AS Hour2,
...
...
Sum(CASE WHEN RDATA_HOUR = 24 THEN RDATA_VALUE/10000.0 END) AS Hour24,
From
REAL_DATA,SAMPLE_DATA,......
WHERE
RDATA_SAMPLE_NUM = SAMPLE_NUM
..
GROUP BY
RDATA_SAMPLE_NUM, METHOD_NAME , UNIT_ABBREV


Above query will give you hourly reports for all RDATA_SAMPLE_NUM, METHOD_NAME , UNIT_ABBREV combinations. Include the time/date column in your group by to split it by days/months/years etc.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-06 : 08:28:47
and if you're using sql 2005 use PIVOT operator.
Go to Top of Page
   

- Advertisement -