Solved

combining of two querys with grouping

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

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
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

24 Experts available now in Live!

Get 1:1 Help Now