Solved

Join 2 Queries

Posted on 2014-04-23
4
164 Views
Last Modified: 2014-04-27
Hi Guys,

I want to be able to combine two queries into 1 result set.

I have for example:

QUERY 1:
Select Distinct Name,Count(*) AS TotalCount
FROM Table1
WHERE
Condition1 > 0
Group BY Name
ORDER BY Name

QUERY2:

Select Distinct Name,Count(*) AS TotalCountWithCondition
FROM Table1
WHERE
Condition1 > 0
AND Condtion2 = 0
Group BY Name
ORDER BY Name

So basically query2 is a subset of Query1 with the extra condition "AND Condition2=0"

I want this to be displayed as 1 returned set, say with column headers like

Name|TotalCount|TotalCountWithCondition


Thanks in advance.
0
Comment
Question by:chudmarek
4 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40019372
Normally you would UNION the results, don't know how well will this work with DISTINCT:
Select Distinct Name,Count(*) AS TotalCount
FROM Table1
WHERE 
Condition1 > 0
Group BY Name
ORDER BY Name
UNION
Select Count(*) AS TotalCountWithCondition
FROM Table1
WHERE 
Condition1 > 0
AND Condtion2 = 0 
Group BY Name
ORDER BY Name

Open in new window

0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 40019378
HI,

pls try

SELECT T1.Name, T1.TotalCount, T2.TotalCountWithCondition
FROM(
SELECT Name, Count(Name) AS TotalCount, Condition1
FROM Table1
GROUP BY Name, Condition1
HAVING Condition1>0) as T1 LEFT JOIN (SELECT Name, Count(Name) AS TotalCountWithCondition
FROM Table1
GROUP BY Name, Condition1, Condition2
HAVING Condition1>0 AND Condition2=0) AS T2 ON T1.Name = T2.Name

Open in new window

Regards
0
 
LVL 26

Accepted Solution

by:
skullnobrains earned 500 total points
ID: 40019379
union will produce the desired results but on different rows instead of columns

--

dealing with a count you need something like this

select
 sum( case when condition1 > 0 then 1 else 0 ) as totalCount
 , sum( case when condition1 > 0 AND Condtion2 = 0 then 1 else 0 ) as totalCountWithCondition
from table1
group by name
order by name
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40019391
select distinct
....
group by
....

WRONG

You NEVER need to add "distinct" if you are using a group by clause.

and,
 I would choose skullnobrains to write this query :)
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Some sers suddenly getting error popup msg 28 89
SQL Server Designer 19 41
SQL Update trigger 5 18
Have a conversion issue with varchar to int in a SQL: Query. 1 30
INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

773 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