组合使用联合的结果集

时间:2012-02-21 03:26:03

标签: sql sql-server union

我有使用联合的查询,我想将查询合并到一个结果集中。这是示例查询。它返回两个结果集。我想将它们合二为一。谢谢!

QUERY 1

SELECT COUNT(*) as 'Have you taken an interest/vocational assessment - Pre'
  FROM YESSCAYouth
WHERE Assess2Pre1 = 1
UNION
SELECT COUNT(*)
  FROM YESSCAYouth
 WHERE Assess2Pre1 = 2
UNION
SELECT COUNT(*)
  FROM YESSCAYouth
 WHERE Assess2Pre1 = 3
UNION
SELECT COUNT(*)
  FROM YESSCAYouth
 WHERE Assess2Pre1 = 1 or Assess2Pre1 = 2 or Assess2Pre1 = 3
   AND Assess2PreDate BETWEEN CAST('01/01/2012' AS DATETIME) AND CAST('12/31/2012' AS DATETIME)

QUERY 2

SELECT COUNT(*) as 'Have you taken an interest/vocational assessment - Post'
  FROM YESSCAYouth
 WHERE Assess2Post1 = 1
UNION
SELECT COUNT(*) 
  FROM YESSCAYouth
 WHERE Assess2Post1 = 2
UNION
SELECT COUNT(*)
  FROM YESSCAYouth
 WHERE Assess2Post1 = 3
UNION
SELECT COUNT(*) 
  FROM YESSCAYouth
 WHERE Assess2Post1 = 1 or Assess2Post1 = 2 or Assess2Post1 = 3
   AND Assess2PostDate BETWEEN CAST('01/01/2012' AS DATETIME) AND CAST('12/31/2012' AS DATETIME)

3 个答案:

答案 0 :(得分:2)

你想要这样的东西:

select
   COALESCE(a1.Level,a2.Level) as Level,
   COALESCE(a1.Cnt,0) as 'Have you taken an interest/vocational assessment - Pre',
   COALESCE(a2.Cnt,0) as 'Have you taken an interest/vocational assessment - Post'
from
    (select Assess2Pre1,COUNT(*) from YESSCAYouth where Assess2Pre1 in (1,2,3) group by Assess2Pre1)
        a1(Level,Cnt)
            full outer join
    (select Assess2Post1,COUNT(*) from YESSCAYouth where Assess2Post1 in (1,2,3) group by Assess2Post1)
        a2(Level,Cnt)
            on
                a1.Level = a2.Level         
union all
select
    4,
    SUM(CASE WHEN Assess2Pre1 in (1,2,3) and Assess2PreDate between '20120101' and '20121231' THEN 1 END),
    SUM(CASE WHEN Assess2Post1 in (1,2,3) and Assess2PostDate between '20120101' and '20121231' THEN 1 END)
from YESSCAYouth

在上面,我匹配了第二和第三列,以便它们代表两个不同状态的可比结果。然后我引入了一个联合来引入基于日期的查询。

如果不是您正在寻找的内容,请在您的问题中添加一些示例数据和预期结果。 (我注意到如果其中一个“级别”的前置和后置计数都为0,我的上述查询将与您的不同。)


其他说明:in()是真正的太空保护程序,完全等同于column = value1 or column = value2...mm/dd/yyyy不是一种安全的格式,无需使用CONVERT()并指定显式格式即可转换为日期。另一方面,yyyymmdd将始终正确转换。

最后,在名称包含数字的列周围有一些设计气味 - 它表明可能有其他相同的列具有相同的名称但数字不同 - 通常情况下,正确的方法是对此进行建模引入一个列来保存该数字,并将数据存储为多行。

答案 1 :(得分:0)

你只需将这两个查询联合起来;在您的查询周围包装括号并查询两个并在其间放置一个union子句,它将为您提供一个组合结果集。

(查询1)

联盟

(查询2)

编辑:如果您有不同的列名,可以尝试将其放入临时表并对其进行联合;尝试在SQL Management Studio中运行它,它会给你一个想法;

select 'tt' as t, 'tete' as tt, 'tets' as ttt
into #test

select 'tt' as tre, 'tete' as tt, 'tets' as ttt
into #testt

select * from #test 
union 
select * from #testt

drop table #test
drop table #testt

答案 2 :(得分:0)

SELECT 1 as g, 'Have you taken an interest/vocational assessment - Pre' as x

UNION ALL 

SELECT 2 as g, cast(COUNT (1) as varchar) as x --as 'Have you taken an interest/vocational assessment - Pre'
FROM YESSCAYouth 
WHERE Assess2Pre1 = 1

UNION ALL

SELECT 3 as g, cast(COUNT (1) as varchar) as x FROM YESSCAYouth
WHERE Assess2Pre1 = 2

UNION ALL

SELECT 4 as g, cast(COUNT (1) as varchar) as x FROM YESSCAYouth
WHERE Assess2Pre1 = 3

UNION ALL

SELECT 5 as g, cast(COUNT (1) as varchar) as x FROM YESSCAYouth
WHERE Assess2Pre1 = 1 or Assess2Pre1 = 2 or Assess2Pre1 = 3
AND
Assess2PreDate 
BETWEEN CAST('01/01/2012' AS DATETIME) AND CAST('12/31/2012' AS DATETIME)

UNION ALL
--QUERY 2 ****************************************

SELECT
6 as g, 'Have you taken an interest/vocational assessment - Post'

UNION
ALL 

SELECT
7 as g,cast(COUNT (1) as varchar) as x --as 'Have you taken an interest/vocational assessment - Post'
FROM
YESSCAYouth
WHERE
Assess2Post1 = 1

UNION
ALL

SELECT
8 as g, cast(COUNT (1) as varchar) as x FROM YESSCAYouth
WHERE
Assess2Post1 = 2

UNION
ALL

SELECT
9 as g, cast(COUNT (1) as varchar) as x FROM YESSCAYouth
WHERE
Assess2Post1 = 3

UNION
ALL

SELECT
10 as g, cast(COUNT (1) as varchar) as x FROM YESSCAYouth
WHERE
Assess2Post1 = 1 or Assess2Post1 = 2 or Assess2Post1 = 3
AND
Assess2PreDate 
BETWEEN CAST('01/01/2012' AS DATETIME) AND CAST('12/31/2012' AS DATETIME)
ORDER
BY g