?
Solved

combining of two querys with grouping

Posted on 2006-06-12
3
Medium Priority
?
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

770 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