【问题标题】:How do I merge two SELECT queries with different WHERE clauses如何合并具有不同 WHERE 子句的两个 SELECT 查询
【发布时间】:2021-07-25 23:18:41
【问题描述】:

我正在尝试使用不同的 WHERE 子句并使用 GROUP BY 对两个不同的选择编写查询。 我浏览了示例,但我的示例不同,因为我有多个 SELECT 字段。 这是示例数据:

--    drop table #temp_counts;
create table #temp_counts(
report_date smalldatetime not null, 
Emp_id varchar(10) not null,
source_system varchar(10) not null
)

Insert into #temp_counts values ('2021-05-02 00:00:00', '12411', 'ABC');
Insert into #temp_counts values ('2021-05-02 00:00:00', '56421', 'ABC');
Insert into #temp_counts values ('2021-05-02 00:00:00', '45411', 'ABC');
Insert into #temp_counts values ('2021-05-02 00:00:00', '75411', 'ABC');
Insert into #temp_counts values ('2021-05-02 00:00:00', '13245', 'XYZ');
Insert into #temp_counts values ('2021-05-02 00:00:00', '66245', 'XYZ');
Insert into #temp_counts values ('2021-05-02 00:00:00', '77245', 'XYZ');
Insert into #temp_counts values ('2021-05-02 00:00:00', '98245', 'XYZ');
Insert into #temp_counts values ('2021-05-02 00:00:00', '34245', 'XYZ');
Insert into #temp_counts values ('2021-05-02 00:00:00', '29245', 'XYZ');

Insert into #temp_counts values ('2021-05-03 00:00:00', '14524', 'ABC');
Insert into #temp_counts values ('2021-05-03 00:00:00', '17824', 'ABC');
Insert into #temp_counts values ('2021-05-03 00:00:00', '32524', 'ABC');
Insert into #temp_counts values ('2021-05-03 00:00:00', '16724', 'XYZ');
Insert into #temp_counts values ('2021-05-03 00:00:00', '19924', 'XYZ');
Insert into #temp_counts values ('2021-05-03 00:00:00', '89424', 'XYZ');
Insert into #temp_counts values ('2021-05-03 00:00:00', '48324', 'XYZ');
Insert into #temp_counts values ('2021-05-03 00:00:00', '16000', 'XYZ');

Insert into #temp_counts values ('2021-05-04 00:00:00', '18724', 'ABC');
Insert into #temp_counts values ('2021-05-04 00:00:00', '12904', 'XYZ');

Insert into #temp_counts values ('2021-05-05 00:00:00', '12074', 'ABC');
Insert into #temp_counts values ('2021-05-05 00:00:00', '12784', 'XYZ');
Insert into #temp_counts values ('2021-05-05 00:00:00', '12324', 'XYZ');
Insert into #temp_counts values ('2021-05-05 00:00:00', '75124', 'XYZ');

这些是我想合并的查询:

select count(*) emp_count,  report_date , 'ABC' source_system from #temp_counts 
where  source_system = 'ABC'
group by report_date
order by report_date

select count(*) emp_count,  report_date , 'XYZ' source_system from #temp_counts 
where  source_system = 'XYZ'
group by report_date
order by report_date

我尝试了以下两种方法:

--Method 1
select fir.emp_count, fir.report_date, fir.source_system from
(select count(*) emp_count,  report_date , 'ABC' source_system from #temp_counts 
where  source_system = 'ABC') as fir
inner join
(select count(*) emp_count,  report_date , 'XYZ' source_system from #temp_counts 
where  source_system = 'XYZ') as sec
on fir.report_date = sec.report_date
group by fir.report_date
order by fir.report_date
--Method 2
select count(*) emp_count,  report_date , 'ABC' source_system from #temp_counts 
where  source_system = 'ABC'
UNION ALL
select count(*) emp_count,  report_date , 'XYZ' source_system from #temp_counts 
where  source_system = 'XYZ'
group by report_date
order by report_date

两者都报错:

Msg 8120, Level 16, State 1, Line 61
Column '#temp_counts.report_date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

请指导

【问题讨论】:

  • 你的预期输出是什么?
  • 用您正在使用的数据库标记您的问题。

标签: sql aggregate-functions union-all


【解决方案1】:

您似乎只想按 report_date 和 source_system 分组:

select count(*) emp_count,  report_date , source_system 
FROM #temp_counts
group by report_date, source_system
order by source_system,report_date

如果您想获得特定源系统的结果,那么您可以组合条件:

select count(*) emp_count,  report_date , source_system 
FROM #temp_counts 
where  source_system in ('ABC', 'XYZ')
group by report_date, source_system
order by source_system,report_date

您可以更改 order by 以按您想要显示的顺序显示行

只是为了说明如何使用联合:

select count(*) emp_count,  report_date , source_system from #temp_counts 
where  source_system = 'ABC'
group by report_date

union all

select count(*) emp_count,  report_date , source_system from #temp_counts 
where  source_system = 'XYZ'
group by report_date
order by source_system, report_date

【讨论】:

  • 我仍然想知道使用 UNION 或 UNION ALL 的解决方案
猜你喜欢
  • 2015-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多