合并 2 个 SQL 查询

发布于 2024-08-06 14:11:17 字数 957 浏览 3 评论 0原文

我有 2 个查询,想在不使用联合的情况下合并到 1 个结果集中。

查询 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

我希望它返回年、月、同日、总计等值。我该如何实现这一目标?联盟没有做我想要它做的事。我必须进行联接和表别名吗?子查询?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痴情换悲伤 2024-08-13 14:11:17

好吧……这个怎么样:

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

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
天涯离梦残月幽梦 2024-08-13 14:11:17

试试这个:

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

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
风吹雪碎 2024-08-13 14:11:17

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

我怀疑你不应该这样做,而应该弄清楚为什么工会不适合你。

你尝试了什么?

发生了什么?

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?

瀞厅☆埖开 2024-08-13 14:11:17

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

SELECT -- query 1

SELECT -- query 2

然后您的客户端可以独立读取 2 个结果集 - 这取决于您使用的语言,但在使用 SqlDataReader 的 C# 中您可以使用 NextResult

SqlDataReader reader = GetReader();
while (reader.Read())
{
    // Process result of query 1
}
reader.NextResult();
while (reader.Read())
{
    // Process result of query 2
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文