Solved

cte recursion, or multiple select statements

Posted on 2011-09-09
3
382 Views
Last Modified: 2012-06-22
Now that I can get my data correctly with two cte's, I need to be able to combine the data into one row.  I've been looking at recursive cte information online, but can't quite get what I need.  Ultimately, my query should return the following:

Name, BillRate, BilledHours, BilledAmt, UnbilledHours, UnbilledAmt

As an added bonus, I'd like to get total hours and total amount at the end, but I haven't even started that part yet... :)

Thanks for any assistance.
----- Billed ------------------

;with LDcte(name,billrate, Billedhours , BilledAmt  ) as (
   select name, (billext/reghrs), reghrs, billext
   from LD
   -- just to ensure we do not have any divide by zero errors
   where reghrs > 0 and WBS1 = '0042009.10'
   and BillStatus = 'F'
)
select name, billrate, sum(Billedhours) [BilledHours], sum(BilledAmt) [BilledAmt]
from LDcte
group by name, billrate
;

----  Works  ----------------------------------------
--------------------------------------------------------
---------------   Unbilled   -----------------------------


;with LDcte(name,billrate, UnBilledhours , UnBilledAmt  ) as (
   select name, (billext/reghrs), reghrs, billext
   from LD
   -- just to ensure we do not have any divide by zero errors
   where reghrs > 0 and WBS1 = '0042009.10'
   and BillStatus in ('B','H')
)
select name, billrate, sum(UnBilledhours) [UnBilledHours], sum(UnBilledAmt) [UnBilledAmt]
from LDcte
group by name, billrate
;

Open in new window

0
Comment
Question by:charkerr6111
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
dqmq earned 500 total points
ID: 36512658
TRy this:
;with LDcte(name, billrate, Billedhours , BilledAmt, UnbilledHours, UnbilledAmt  ) as (
   select name, (billext/reghrs), reghrs, billext, Null, Null
   from LD
   -- just to ensure we do not have any divide by zero errors
   where reghrs > 0 
   and BillStatus = 'F'
   union all
   select name, (billext/reghrs), null, null, reghrs, billext
   from LD
   -- just to ensure we do not have any divide by zero errors
   where reghrs > 0 
   and BillStatus in ('B','H')
 )
select name, billrate, sum(Billedhours) [BilledHours], sum(BilledAmt) [BilledAmt], sum(Unbilledhours) unbilledhours, sum(unbilledAmt) unbilledamt
from LDcte
where WBS1 = '0042009.10' 
group by name, billrate

Open in new window

0
 

Author Comment

by:charkerr6111
ID: 36512699
Never occurred to me to use null values, tried all kinds of things. Thanks again!
0
 
LVL 42

Expert Comment

by:dqmq
ID: 36512715
;with LDcte(name, billrate, Billedhours , BilledAmt, UnbilledHours, UnbilledAmt  ) as (
   select name, (billext/reghrs), reghrs, billext, Null, Null
   from LD
   -- just to ensure we do not have any divide by zero errors
   where reghrs > 0
   and WBS1 = '0042009.10'
   and BillStatus = 'F'
   union all
   select name, (billext/reghrs), null, null, reghrs, billext
   from LD
   -- just to ensure we do not have any divide by zero errors
   where reghrs > 0
   and WBS1 = '0042009.10'
   and BillStatus in ('B','H')
 )
select  '' as Line, name, billrate, sum(Billedhours) [BilledHours], sum(BilledAmt) [BilledAmt], sum(Unbilledhours) unbilledhours, sum(unbilledAmt) unbilledamt
from LDcte
group by name, billrate
union all
select 'Total', as Line, Null, avg(billrate), sum(billedhours) billedhours, sum(billedamt) billedamt,
sum(unbilledhours) unbilledhours, sum(unbilledamt) unbilledamt
order by linetype, name, billrate


0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS  - Parameters with comma 10 40
Where clause to fliter varchar with Characters 12 57
Begin Transaction 12 25
Creating Scalar Function 3 17
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question