[英]How to combine two queries from same DB with different where clauses?
[英]How do I merge two SELECT queries with different WHERE clauses
我正在嘗試使用不同的 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.
請指導
似乎您只想按 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.