Solved

Join 2 Queries

Posted on 2014-04-23
4
162 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 48

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

762 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

17 Experts available now in Live!

Get 1:1 Help Now