Solved

I need to create a View or Query in SQL Server 2005 that contains 2 queries

Posted on 2011-02-18
2
180 Views
Last Modified: 2012-05-11
For starters, I am new to SQL Server So I apologize for my ignorance in advance.
I've been working in Access for 10 years
I want to use theresults of the queries below as a source for an asp page.

I have 2 queries which each output 3 columns.
XNAC_Name           Model      ServiceableCallCount
XNAC_Name           Model      Solved Count

The criteria for the 3rd column is the only thing that changes.
I would like to combine the 2 queries  to get 4 columns
XNAC_Name           Model      ServiceableCallCount      Solved Count

How can these best be combined ?
'**This is the query to get Serviceable Call Count:
SELECT     RCAMasterFinal.XNAC_Major, XNAC_Lookup.XNAC_Name, RCAMasterFinal.Model, Count(*)AS ServiceableCalls
FROM         RCAMasterFinal INNER JOIN
                      XNAC_Lookup ON RCAMasterFinal.XNAC_Major = XNAC_Lookup.XNAC_Major
WHERE     (RCAMasterFinal.CallDate BETWEEN CONVERT(DATETIME, '2011-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2011-01-31 00:00:00', 102)) AND 
                      (RCAMasterFinal.ResolutionCode < 9)
 AND RCAMasterFinal.XNAC_Major = 'STP'

Group By    RCAMasterFinal.XNAC_Major, XNAC_Lookup.XNAC_Name, RCAMasterFinal.Model

'**This is the query to get Solved Count:

SELECT     RCAMasterFinal.XNAC_Major, XNAC_Lookup.XNAC_Name, RCAMasterFinal.Model, Count(*)AS SolvedCalls
FROM         RCAMasterFinal INNER JOIN
                      XNAC_Lookup ON RCAMasterFinal.XNAC_Major = XNAC_Lookup.XNAC_Major
WHERE     (RCAMasterFinal.CallDate BETWEEN CONVERT(DATETIME, '2011-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2011-01-31 00:00:00', 102)) AND 
                      (RCAMasterFinal.ResolutionCode = 5)
Or (RCAMasterFinal.ResolutionCode = 8)  AND RCAMasterFinal.XNAC_Major = 'STP'
Group By    RCAMasterFinal.XNAC_Major, XNAC_Lookup.XNAC_Name, RCAMasterFinal.Model

Open in new window

0
Comment
Question by:Mswetsky
2 Comments
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
I believe you can do this in one query, essentially sum using a case statement.

SELECT     RCAMasterFinal.XNAC_Major, XNAC_Lookup.XNAC_Name, RCAMasterFinal.Model, Count(*)AS ServiceableCalls, sum(case when RCAMasterFinal.ResolutionCode = 5 or RCAMasterFinal.ResolutionCode = 8 then 1 else 0 end) as SolvedCount
FROM         RCAMasterFinal INNER JOIN
                      XNAC_Lookup ON RCAMasterFinal.XNAC_Major = XNAC_Lookup.XNAC_Major
WHERE     (RCAMasterFinal.CallDate BETWEEN CONVERT(DATETIME, '2011-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2011-01-31 00:00:00', 102)) AND
                      (RCAMasterFinal.ResolutionCode < 9)
 AND RCAMasterFinal.XNAC_Major = 'STP'

Group By    RCAMasterFinal.XNAC_Major, XNAC_Lookup.XNAC_Name, RCAMasterFinal.Model

0
 
LVL 1

Author Closing Comment

by:Mswetsky
Comment Utility
Wow, That is a great solution from a different perspective than I was looking. Thank you so much!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

728 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

14 Experts available now in Live!

Get 1:1 Help Now