Solved

cte recursion, or multiple select statements

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

735 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