简体   繁体   English

将两个SQL查询的结果组合为单独的列

[英]Combining the results of two SQL queries as separate columns

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. 谢谢。

You can aliasing both query and Selecting them in the select query 您可以在select查询中对查询和选择它们进行别名
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

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

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. 您还可以使用CTE来获取所需的信息组,并将它们连接在一起,如果您希望它们位于同一行中。 Example, depending on which SQL syntax you use, here: 示例,取决于您使用的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. 您可以根据所提取的数据确定所需的JOIN类型,并确保在获取信息的组中具有相同的字段,以便将它们全部放入一行中。 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. 此外,为了性能,CTE是要走的路,而不是内联SELECT等。 Hope this helps. 希望这可以帮助。

how to club the 4 query's as a single query 如何将4个查询作为单个查询加入

show below query 显示如下查询

  1. total number of cases pending + 2.cases filed during this month ( base on sysdate) + total number of cases (1+2) + no. 本月提交的案件总数+ 2.cases(基于sysdate)+案件总数(1 + 2)+否。 cases disposed where nse= disposed + no. 案件处理nse = dispos + no。 of cases pending (other than nse <> disposed) 待处理的案件(除了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个月)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM