Solved

combining of two querys with grouping

Posted on 2006-06-12
3
246 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

758 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

17 Experts available now in Live!

Get 1:1 Help Now