将两个 SQL 查询的结果组合为单独的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16364187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combining the results of two SQL queries as separate columns
提问by W A K A L E Y
I have two queries which return separate result sets, and the queries are returning the correct output.
我有两个查询返回单独的结果集,并且查询返回正确的输出。
How can I combine these two queries into one so that I can get one single result set with each result in a separate column?
如何将这两个查询合并为一个,以便我可以获得一个结果集,每个结果都在单独的列中?
Query 1:
查询 1:
SELECT SUM(Fdays) AS fDaysSum From tblFieldDays WHERE tblFieldDays.NameCode=35 AND tblFieldDays.WeekEnding=?
Query 2:
查询 2:
SELECT SUM(CHdays) AS hrsSum From tblChargeHours WHERE tblChargeHours.NameCode=35 AND tblChargeHours.WeekEnding=?
Thanks.
谢谢。
回答by Willy Pt
You can aliasing both query and Selecting them in the select query
http://sqlfiddle.com/#!2/ca27b/1
您可以在选择查询http://sqlfiddle.com/#!2/ca27b/1 中别名查询和选择它们
SELECT x.a, y.b FROM (SELECT * from a) as x, (SELECT * FROM b) as y
回答by Lamak
You can use a CROSS JOIN
:
您可以使用CROSS JOIN
:
SELECT *
FROM ( SELECT SUM(Fdays) AS fDaysSum
FROM tblFieldDays
WHERE tblFieldDays.NameCode=35
AND tblFieldDays.WeekEnding=1) A -- use you real query here
CROSS JOIN (SELECT SUM(CHdays) AS hrsSum
FROM tblChargeHours
WHERE tblChargeHours.NameCode=35
AND tblChargeHours.WeekEnding=1) B -- use you real query here
回答by AS4noob
You could also use a CTE to grab groups of information you want and join them together, if you wanted them in the same row. Example, depending on which SQL syntax you use, here:
如果您希望它们在同一行中,您还可以使用 CTE 来获取您想要的信息组并将它们连接在一起。示例,取决于您使用的 SQL 语法,这里:
WITH group1 AS (
SELECT testA
FROM tableA
),
group2 AS (
SELECT testB
FROM tableB
)
SELECT *
FROM group1
JOIN group2 ON group1.testA = group2.testB --your choice of join
;
You decide what kind of JOIN you want based on the data you are pulling, and make sure to have the same fields in the groups you are getting information from in order to put it all into a single row. If you have multiple columns, make sure to name them all properly so you know which is which. Also, for performance sake, CTE's are the way to go, instead of inline SELECT's and such. Hope this helps.
您可以根据要提取的数据决定需要哪种 JOIN,并确保在从中获取信息的组中具有相同的字段,以便将其全部放入一行中。如果您有多个列,请确保正确命名它们,以便您知道哪个是哪个。此外,出于性能考虑,CTE 是要走的路,而不是内联 SELECT 等。希望这可以帮助。
回答by ramreddy aredla
how to club the 4 query's as a single query
如何将 4 个查询合并为一个查询
show below query
显示下面的查询
- total number of cases pending + 2.cases filed during this month ( base on sysdate) + total number of cases (1+2) + no. cases disposed where nse= disposed + no. of cases pending (other than nse <> disposed)
- 待决案件总数 + 2. 本月提交的案件(基于系统日期)+ 案件总数 (1+2) + 没有。已处理的情况,其中 nse= 已处理 + 否。待决案件(nse <> 处置除外)
nsc = nature of case
nsc = 案件性质
report is taken on 06th of every month
每月06日报告
( monthly report will be counted from 05th previous month to 05th present of present month)
(月报将从上月05日算到本月05日)