views:

203

answers:

4

I've got 2 queries I'd like to merge into 1 result set without using union.

Query 1

select  datepart(yy,dateclosed)as 'Year',
    datepart(mm,dateclosed) as 'Month',
    count(*)as 'Total' 
from bug 
where projectid = 44 
and ifclosed = 1
and isnull(createdbyperson,1) <> '-1111111110'
and datepart(yy,dateclosed) > '2000'
group by datepart(yy,dateclosed), datepart(mm,dateclosed)
order by 1,2

Query 2

select  datepart(yy,dateclosed)as 'Year',
    datepart(mm,dateclosed) as 'Month',
    count(*)as 'SameDay' 
from bug 
where   projectid = 44 
     and ifclosed = 1
     and isnull(createdbyperson,1) <> '-1111111110'
     and datepart(yy,dateclosed) > '2000' 
     and CONVERT(VARCHAR(10), dateclosed, 101) = CONVERT(VARCHAR(10), datecreated, 101) 
group by datepart(yy,dateclosed),datepart(mm,dateclosed)
order by 1,2

Id like it to return the values as Year,Month,SameDay,Total. How do I achieve this? Union doesn't do what I want it to do. Do I have to do a join and a table alias? Subquery?

Thanks in advance.

+1  A: 

OK...what about this one:

select a.Year, a.Month, b.SameDay, a.Total
from
(
select  datepart(yy,dateclosed)as 'Year',
    datepart(mm,dateclosed) as 'Month',
    count(*)as 'Total' 
from bug 
where projectid = 44 
and ifclosed = 1
and isnull(createdbyperson,1) <> '-1111111110'
and datepart(yy,dateclosed) > '2000'
group by datepart(yy,dateclosed), datepart(mm,dateclosed)
) a
inner join
(
select  datepart(yy,dateclosed)as 'Year',
    datepart(mm,dateclosed) as 'Month',
    count(*)as 'SameDay' 
from bug 
where   projectid = 44 
        and ifclosed = 1
        and isnull(createdbyperson,1) <> '-1111111110'
        and datepart(yy,dateclosed) > '2000' 
        and CONVERT(VARCHAR(10), dateclosed, 101) = CONVERT(VARCHAR(10), datecreated, 101) 
group by datepart(yy,dateclosed),datepart(mm,dateclosed)
) b
on a.Year = b.Year AND a.Month = b.Month
order by 1,2
Scoregraphic
The union works but doesn't return the data in the format I'd like, Year, Month, SameDay, Total. It returns as Year1,month1,sameday Year1,Month1,Total Year2,month2,sameday Year2,Month2,Total etc
eric
Oh...have a look at my updated query...
Scoregraphic
Yes! Perfect, that does exactly what I was looking for.
eric
A: 

Without using a union you could create a temporary table, write the results of the two queries into it and then query the temporary table.

I suspect that you shouldn't be doing this, but rather working out why the union isn't working for you.

What have you tried?

What happened?

Matt Lacey
Union works, but it doesn't return the data in the manner in which I am trying to achieve, a 4 column layout. Union merges it all into a 3 column list.
eric
Union is not the right syntax for this query - he is trying to join together 2 subqueries.
jnylen
+1  A: 

Try this:

SELECT DATEPART(yy,dateclosed) AS 'Year',
    DATEPART(mm,dateclosed) AS 'Month',
    SUM(IF(CONVERT(VARCHAR(10), dateclosed, 101) = CONVERT(VARCHAR(10), datecreated, 101), 1, 0)) AS SameDay,
    COUNT(*) AS 'Total' 
FROM bug 
WHERE projectid = 44 
    AND ifclosed = 1
    AND ISNULL(createdbyperson,1) <> '-1111111110'
    AND DATEPART(yy,dateclosed) > '2000'
GROUP BY DATEPART(yy,dateclosed), DATEPART(mm,dateclosed)
ORDER BY 1,2
Dark Falcon
Returns incorrect syntax errors:Msg 156, Level 15, State 1, Line 3Incorrect syntax near the keyword 'IF'.Msg 102, Level 15, State 1, Line 3Incorrect syntax near ','.
eric
What SQL server? That was code for MySQL. For MS SQL Server, use CASE: http://www.sql-server-performance.com/faq/iif_tsql_p1.aspx
Dark Falcon
SQL Server 2005, didn't explicitly say that, instead just tagged sql server
eric
A: 

Why is it that you want to do this? Are you aware that you can just have the query return multiple result sets:

SELECT -- query 1

SELECT -- query 2

Your client can then read the 2 result sets independently - it depends on the langauge you use but in C# using SqlDataReader you use NextResult

SqlDataReader reader = GetReader();
while (reader.Read())
{
    // Process result of query 1
}
reader.NextResult();
while (reader.Read())
{
    // Process result of query 2
}
Kragen
To create a report to distribute to people. Scoregraphic's query above achieves what I was looking to accomplish, a 4 column layout from 2 3 column queries.
eric