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
 Combining the rows

Author  Topic 

sqlbug
Posting Yak Master

201 Posts

Posted - 2009-01-05 : 18:01:45
This is in reference to my earlier posting "Modifying the query". Here's what I wrote:
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 ...

Now, if I use DAY_NUM in the query instead of RDATA_SAMPLE_NUM (sample number) thats causing the problem now, I end up with multiple rows (like 24 rows for 24 hours of the day). If I could combine them, I would get what I wnated.
Any ideas? Thanks.

revdnrdy
Posting Yak Master

220 Posts

Posted - 2009-01-05 : 18:56:21
Now, if I use DAY_NUM in the query instead of RDATA_SAMPLE_NUM (sample number) thats causing the problem now, I end up with multiple rows (like 24 rows for 24 hours of the day). If I could combine them, I would get what I wnated.

Based on what you said...

I suggest you may look at the PARTITION clause. It seems to offer what you want. It may help keep the groupings or hours together
Go to Top of Page

sqlbug
Posting Yak Master

201 Posts

Posted - 2009-01-05 : 19:09:47
Thanks, I got it.
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-01-05 : 20:28:03
quote:
Originally posted by sqlbug

This is in reference to my earlier posting "Modifying the query". Here's what I wrote:
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 ...

Now, if I use DAY_NUM in the query instead of RDATA_SAMPLE_NUM (sample number) thats causing the problem now, I end up with multiple rows (like 24 rows for 24 hours of the day). If I could combine them, I would get what I wnated.
Any ideas? Thanks.



Also you don't need distinct over here when you use group by.
Go to Top of Page
   

- Advertisement -