Sql server 合并2个SQL查询

Sql server 合并2个SQL查询,sql-server,resultset,Sql Server,Resultset,我有两个查询要合并到一个结果集中,而不使用union 问题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 datepar

我有两个查询要合并到一个结果集中,而不使用union

问题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
问题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
我希望它以年、月、SameDay和Total的形式返回值。我如何做到这一点?工会没有做我想让它做的事。我必须执行联接和表别名吗?子查询


提前谢谢。

好的……这个怎么样:

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

不使用联合,您可以创建一个临时表,将两个查询的结果写入其中,然后查询临时表

我怀疑你不应该这么做,而是应该弄清楚工会为什么不为你工作

你试过什么

发生了什么事?

试试这个:

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

你为什么要这么做?您是否知道可以让查询返回多个结果集:

SELECT -- query 1

SELECT -- query 2
然后,您的客户机可以独立地读取这两个结果集—这取决于您使用的语言,但在使用SqlDataReader的C中,您使用的是NextResult


union的代码是什么样子的?出现了什么错误?我不想使用union,因为它将数据合并到一个3列表中,并使记录难以确定值。union不是此查询的正确语句。您需要将两个查询视为连接在一起的子查询。或者,我打赌所需的查询可以编写为一个查询,因为您发布的两个查询非常相似。union可以工作,但不会以我希望的格式返回数据:年、月、SameDay、总计。它返回为Year1,month1,sameday Year1,month1,Total Year2,month2,sameday Year2,month2,Total Etchies!很好,这正是我想要的。返回不正确的语法错误:Msg 156,级别15,状态1,第3行关键字“IF”附近的语法不正确。Msg 102,级别15,状态1,第3行“,”附近的语法不正确。什么SQL server?这是MySQL的代码。对于MS SQL Server,用例:SQL Server 2005没有明确说明这一点,只是标记了SQL Server Union,但它没有以我试图实现的方式返回数据,即4列布局。Union将所有内容合并到一个3列列表中。Union不是此查询的正确语法-他正在尝试将2个子查询合并在一起。创建一个报告以分发给其他人。上面Scoregraphic的查询实现了我希望实现的目标,从2个3列查询中得到4列布局。
SqlDataReader reader = GetReader();
while (reader.Read())
{
    // Process result of query 1
}
reader.NextResult();
while (reader.Read())
{
    // Process result of query 2
}