Combining the results of two SQL queries as separate columns

前端 未结 4 1230
轮回少年
轮回少年 2020-11-27 16:32

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

相关标签:
4条回答
  • 2020-11-27 17:00

    how to club the 4 query's as a single query

    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. cases disposed where nse= disposed + no. of cases pending (other than nse <> disposed)

    nsc = nature of case

    report is taken on 06th of every month

    ( monthly report will be counted from 05th previous month to 05th present of present month)

    0 讨论(0)
  • 2020-11-27 17:04

    You can aliasing both query and Selecting them in the select query
    http://sqlfiddle.com/#!2/ca27b/1

    SELECT x.a, y.b FROM (SELECT * from a) as x, (SELECT * FROM b) as y
    
    0 讨论(0)
  • 2020-11-27 17:08

    You can use a 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
    
    0 讨论(0)
  • 2020-11-27 17:24

    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:

    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.

    0 讨论(0)
提交回复
热议问题