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 |
hbadministrator
Posting Yak Master
120 Posts |
Posted - 2013-05-06 : 09:31:41
|
I have the code that shows 2 lines and I would like to get it to show all on one line instead. Also if you know a better Case when Else please help with that. This I am sure is very simple and stupid. Sorry for wasting your time on this.SELECT [disp-no], CASE WHEN (Billed <> 'True') THEN (SUM([hours-bill])) END AS Unbilledhr, CASE WHEN (Billed = 'True') THEN (SUM([hours-bill])) END AS billedhr, SUM([hours-bill]) AS totalhr, CASE WHEN (Billed <> 'TRUE') THEN (SUM([cost-amt])) END AS unbilledamt, CASE WHEN (Billed = 'TRUE') THEN (SUM([cost-amt])) END AS billedamt, SUM([cost-amt])AS totalamt, SUM([hours-bill]) AS totalhrFROM dbo.[jc-time-sh]WHERE ([disp-no] = '046210')GROUP BY [disp-no], Billeddisp-no, Unbilledhr, billedhr, totalhr, unbilledamt, billedamt, Totalamt046210, 17.00, NULL, 441.58, NULL, 441.58, 17.00046210, NULL, 412.00, NULL, 11417.65, 11417.65, 412.00 |
|
hbadministrator
Posting Yak Master
120 Posts |
Posted - 2013-05-06 : 09:49:42
|
I created the above as a view and then created another view that did a sum and gave me the lines I was looking for but I was wondering if this can all be done in 1 view?SELECT [disp-no], SUM(Unbilledhr) AS Unbilledhr, SUM(billedhr) AS Billedhr, SUM(totalhr) AS Totalhr, SUM(unbilledamt) AS Unbilledamt, SUM(billedamt) AS Billedamt, SUM(totalamt) AS TotalamtFROM dbo.JL_comtimeshGROUP BY [disp-no] |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2013-05-06 : 09:53:34
|
without knowing the details of the data, we (at least I) can only guess... use Max for each "case" e.g. SELECT [disp-no], Max(CASE WHEN (Billed <> 'True') THEN (SUM([hours-bill]))) END AS Unbilledhr, Max(CASE WHEN (Billed = 'True') THEN (SUM([hours-bill]))) END AS billedhr.........FROM dbo.[jc-time-sh]WHERE ([disp-no] = '046210')GROUP BY [disp-no]CheersMIK |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-05-06 : 09:55:04
|
--Try this??SELECT [disp-no],SUM( CASE WHEN (Billed <> 'True') THEN [hours-bill] END) AS Unbilledhr,SUM( CASE WHEN (Billed = 'True') THEN [hours-bill])) END) AS billedhr, SUM([hours-bill]) AS totalhr,SUM( CASE WHEN (Billed <> 'TRUE') THEN [cost-amt] END) AS unbilledamt,SUM( CASE WHEN (Billed = 'TRUE') THEN [cost-amt] END) AS billedamt, SUM([cost-amt])AS totalamt, SUM([hours-bill]) AS totalhrFROM dbo.[jc-time-sh]WHERE ([disp-no] = '046210')GROUP BY [disp-no]--Chandu |
|
|
|
|
|
|
|