Solved

cte recursion, or multiple select statements

Posted on 2011-09-09
3
379 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
  • 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
migration MS SQL database to Oracle 30 61
sql 2014,  lock limit 5 32
TSQL query to generate xml 4 33
Sql server get data from a usp to use in a usp 5 16
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

776 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