Solved

cte recursion, or multiple select statements

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now