Solved

combining of two querys with grouping

Posted on 2006-06-12
3
271 Views
Last Modified: 2012-06-21
My problem is I need to get the total (distinct) units built in a given time frame. see code

select areaid, count(distinct(serial_no)) as tested, case when wkid = 0 then datename(month,cast(cast(monthid as varchar(2))+'/1/1900' as datetime))
else 'Week ' + cast(wkid as varchar(2)) end as TimeBucket  from ##WhtBrds1147f
group by areaid, wkid, monthid, yearid


I also need to get the total number of defects even if a unit has multiple see code

select areaid,

cast(sum(fail)as int) as fail,

case when wkid = 0 then datename(month,cast(cast(monthid as varchar(2))+'/1/1900' as datetime))
else 'Week ' + cast(wkid as varchar(2)) end as TimeBucket,
0 as goal from ##WhtBrds1147f
where customer in ('HART') and areaid in ('FI')
group by  areaid, wkid, monthid, yearid order by yearid, monthid,areaid, wkid

Both work fine by them self but I need to get the tested and failed in to a single query to get the yield fail/tested grouped by areaid, wkid, monthid, yearid

I could do it by using temp tables... and requery it to get the answer but there has to be a better way.
0
Comment
Question by:LeeHopkins
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 100 total points
ID: 16886625
what happens when you try this...

SELECT areaid,
   COUNT(DISTINCT(serial_no)) as tested,
   CAST(SUM(fail) AS int) AS fail,
   CASE
      WHEN wkid = 0 THEN DATENAME(month,CAST(CAST(monthid as varchar(2))+'/1/1900' as datetime))
      ELSE 'Week ' + CAST(wkid as varchar(2))
   END AS TimeBucket
FROM ##WhtBrds1147f
GROUP BY areaid, wkid, monthid, yearid
0
 

Author Comment

by:LeeHopkins
ID: 16886631
wont the distinct incompass the who select?
0
 

Author Comment

by:LeeHopkins
ID: 16886686
yep that did it, for some reason i was thinking that it would distinct on all the selected items,
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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 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 shrink a transaction log file down to a reasonable size.

786 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