Solved

combining of two querys with grouping

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

792 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