| Author |
Topic |
|
JohnBGood
Starting Member
48 Posts |
Posted - 2009-10-05 : 15:04:44
|
| -- I have the following queryDECLARE @foo TABLE ([ID] INT,AssID int,[TimeByDay] datetime, [TaskName] NVARCHAR(255), [ParentId] INT, [cost] MONEY)INSERT @fooSELECT 1,null,null,'Design Activities', null, 0UNION SELECT 2,null,null, 'Write Specs', 1, 0UNION SELECT 3,10,'1/1/2009', 'Write UI Spec', 2, 5UNION SELECT 3,10,'1/2/2009', 'Write UI Spec', 2, 5UNION SELECT 3,10,'1/3/2009', 'Write UI Spec', 2, 5UNION SELECT 3,10,'1/4/2009', 'Write UI Spec', 2, 5UNION SELECT 4,11,'1/1/2009', 'Write Data Spec', 2, 5UNION SELECT 4,11,'1/2/2009', 'Write Data Spec', 2, 5UNION SELECT 5,null,null, 'Build Prototypes', 1, 0UNION SELECT 6,12,'1/1/2009', 'Build Screen Mockkups', 5, 5UNION SELECT 6,12,'1/2/2009', 'Build Screen Mockkups', 5, 5 select TimeByDay, sum(cost) from @foo where timebyDay is not nullgroup by timebyDay /* this query returns the results that I want, specifically:2009-01-01, 15.002009-01-02, 15.002009-01-03, 5.002009-01-04, 5.00However, if I add a second column to my select statement, I'm no longer grouping on "TimeByDay" like I need. For example, if I do this...select TimeByDay, TaskName, sum(cost) from @foo where timebyDay is not nullgroup by timebyDay, TaskName... I get this2009-01-01 Build Screen Mockkups 5.002009-01-02 Build Screen Mockkups 5.002009-01-01 Write Data Spec 5.002009-01-02 Write Data Spec 5.002009-01-01 Write UI Spec 5.002009-01-02 Write UI Spec 5.002009-01-03 Write UI Spec 5.002009-01-04 Write UI Spec 5.00*/-- how do i select multiple fields, but still get the grouping on "TimeByDay" that I want????? |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-10-05 : 15:33:31
|
| If you want to add TaskName, how do you want the results displayed? |
 |
|
|
JohnBGood
Starting Member
48 Posts |
Posted - 2009-10-05 : 15:48:26
|
| I would prefer to have it like this... I need to add taskname because im passing it as a parameter somewhere else.2009-01-01, 15.002009-01-02, 15.002009-01-03, 5.002009-01-04, 5.00 |
 |
|
|
JohnBGood
Starting Member
48 Posts |
Posted - 2009-10-05 : 15:49:38
|
| actually its not task Name that I need to pass - its the 2 ID fields, however I still need the results to look like 2009-01-01, 15.002009-01-02, 15.002009-01-03, 5.002009-01-04, 5.00 |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-10-05 : 16:29:53
|
I'm still not sure what you want the output to look line since you've just shown me the output you already have.. But two things come to mind:1) You can use WITH ROLLUP:select TimeByDay, TaskName, sum(cost) from @foo where timebyDay is not nullgroup by timebyDay, TaskNameWITH ROLLUP 2) Join to the total (or use Cross Apply):SELECT F.TimeByDay, F.TaskName, T.SumCostFROM @Foo AS FINNER JOIN ( select TimeByDay, sum(cost) AS SumCost from @foo where timebyDay is not null group by timebyDay ) AS TON T.TimeByDay = F.TimeByDaywhere F.timebyDay is not null |
 |
|
|
JohnBGood
Starting Member
48 Posts |
Posted - 2009-10-06 : 08:10:18
|
| Thanks Lamprey. This looks promising, however, i still get 8 rows of results. Im trying to get the cost to roll up for each individual TimeByDay entry. What do you think? |
 |
|
|
|
|
|