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 |
|
frank.svs
Constraint Violating Yak Guru
368 Posts |
Posted - 2007-07-11 : 02:55:22
|
| Hi pals,I need a SELECT which populates the aggregated data into a temp.Please help me out.CREATE TABLE [dbo].[test] ( [id] [int] IDENTITY (1, 1) NOT NULL , [col1] [int] NULL , [col2] [int] NULL , [place] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [status] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]GOid col1 col2 place status1 3 2 kerala Progess2 2 4 kerala Progess3 2 4 kerala Progess4 1 4 delhi Complete5 2 5 delhi Complete6 3 2 delhi Complete7 2 4 hyd Incomplete8 1 4 hyd Incomplete9 1 4 hyd IncompleteCREATE TABLE [dbo].[trg_tbl] ( [sid] [int] NULL , [sum_col1_Progress] [int] NULL , [avg_col2_Progress] [int] NULL , [sum_col1_Complete] [int] NULL , [avg_col2_Complete] [int] NULL , [sum_col1_Incomplete] [int] NULL , [avg_col2_Incomplete] [int] NULL ) ON [PRIMARY]GOInitially i inserted a dummy rec with all nulls in trg_tblMy requirement is, i need to load the summarized data into the trg_bl Group by "place" fieldUPDATE trg_tbl SET sum_col1_Progress = (SELECT SUM(col1) FROM test WHERE status=’Progress’ GROUP BY place )UPDATE trg_tbl SET sum_col2_Progress = (SELECT AVG(col2) FROM test WHERE status=’Progress’ GROUP BY place )UPDATE trg_tbl SET sum_col1_Complete = (SELECT SUM(col1) FROM test WHERE status=’Complete’ GROUP BY place )UPDATE trg_tbl SET avg_col2_Complete = (SELECT AVG(col2) FROM test WHERE status=’Complete’ GROUP BY place )UPDATE trg_tbl SET [sum_col2_Incomplete] = (SELECT SUM(col1) FROM test WHERE status=’Incomplete’ GROUP BY place )UPDATE trg_tbl SET [sum_col2_Incomplete] = (SELECT AVG(col2) FROM test WHERE status=’Incomplete’ GROUP BY place )Instead of writing so many updates can we write in a single SELECT stmt which select's all the aggregated data and loads into tmp table with above aggregated column names.Any suggestions will be appreciated.Thanks and regards,franky |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-07-11 : 03:04:15
|
I usually use something along these lines:SELECT place, [ProgressSum] = SUM(CASE WHEN status=’Progress’ THEN col1 ELSE 0 END), [ProgressCount] = SUM(CASE WHEN status=’Progress’ THEN 1 ELSE 0 END), [CompleteSum] = SUM(CASE WHEN status=’Complete’ THEN col1 ELSE 0 END), [CompleteCount] = SUM(CASE WHEN status=’Complete’ THEN 1 ELSE 0 END),...FROM test GROUP BY place for the Average you'll have to divide ProgressSum / ProgressCountKristen |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-07-11 : 03:07:36
|
| Read about Cross-tab reports in sql server help fileMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-07-11 : 03:08:24
|
MadhivananFailing to plan is Planning to fail |
 |
|
|
frank.svs
Constraint Violating Yak Guru
368 Posts |
Posted - 2007-07-12 : 11:23:16
|
| Thanks Kristen. |
 |
|
|
|
|
|
|
|