组合两个返回不同行数的SQL查询

时间:2019-04-16 08:25:54

标签: sql sql-server

我需要将两个数据集(查询)合并为一个SSRS报告的一个数据集。它们都传递相同的参数来获取结果,但问题是它们返回的结果数不同,因为您传递的字符串包含用定界符分隔的ID,并且每个查询都返回参数中传递的所有ID的结果。

第一个查询传递了一个参数,例如@Sem_ID = '137210;137211'

SELECT Sem_ID, Sem_KW, Sem_Jahr, Sem_SemNrKata, Sem_von, Sem_bis, h.chr_name, h.chr_ort
FROM acc_seminar.t_Seminar
JOIN dbo.tbl_hotel h ON Sem_Ht_ID=h.pk_hotel
WHERE Sem_ID IN (SELECT value  
FROM fn_Split(@Sem_ID, ';')  
WHERE RTRIM(value) <> '') AND (Sem_SemTh_ID != 76 AND Sem_Sto = 0)

对于@Sem_ID中的每个值,返回一行:ID 137210和ID 137211。

enter image description here

第二个查询传递相同的参数并返回多行:例如,我得到7行结果:ID 137210为5行,ID 137211为2行。

SELECT tu.TU_Name AS Name, tu.TU_Vorname AS Vorname, ParkgebuehrBrutto AS Preis
FROM acc_seminar.t_Teiluber_Ext tux
JOIN acc_seminar.t_Teiluber tu ON tux.TeiluberId = tu.TU_ID
JOIN acc_seminar.t_Seminar s ON tu.TU_Sem_ID = s.Sem_ID
WHERE s.Sem_ID IN (SELECT value  
                   FROM fn_Split(@Sem_ID, ';')  
                   WHERE RTRIM(value) <> '')
  AND ParkgebuehrBrutto IS NOT NULL

enter image description here

我需要以某种方式组合这些查询,以便在报表中传递一个查询。

我尝试使用UNION,但两个查询的列数均不同。有什么建议吗?

2 个答案:

答案 0 :(得分:1)

希望我能正确理解你。

-- query 1
SELECT
  col1,
  col2,
  col3,
  col4,
  NULL col5 -- this column is not exist in the query 1
FROM ...

UNION ALL

-- query 2
SELECT
  NULL col1, -- this column is not exist in the query 2
  col2,
  col3,
  NULL col4, -- this column is not exist in the query 2
  col5
FROM ...

您对UNION ALL的查询:

SELECT
  Sem_ID, Sem_KW, Sem_Jahr, Sem_SemNrKata, Sem_von, Sem_bis, h.chr_name, h.chr_ort,
  NULL Name, NULL Vorname, NULL Preis
FROM acc_seminar.t_Seminar
JOIN dbo.tbl_hotel h ON Sem_Ht_ID=h.pk_hotel
WHERE Sem_ID IN (SELECT value  
FROM fn_Split(@Sem_ID, ';')  
WHERE RTRIM(value) <> '') AND (Sem_SemTh_ID != 76 AND Sem_Sto = 0)

UNION ALL

SELECT
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  tu.TU_Name AS Name, tu.TU_Vorname AS Vorname, ParkgebuehrBrutto AS Preis
FROM acc_seminar.t_Teiluber_Ext tux
JOIN acc_seminar.t_Teiluber tu ON tux.TeiluberId = tu.TU_ID
JOIN acc_seminar.t_Seminar s ON tu.TU_Sem_ID = s.Sem_ID WHERE s.Sem_ID IN (SELECT value  
FROM fn_Split(@Sem_ID, ';')  
WHERE RTRIM(value) <> '')  AND ParkgebuehrBrutto IS NOT NULL

答案 1 :(得分:1)

另一种方法如何组合两个记录集:

;WITH cte1 AS(
  SELECT Sem_ID, Sem_KW, Sem_Jahr, Sem_SemNrKata, Sem_von, Sem_bis, h.chr_name, h.chr_ort, ROW_NUMBER()OVER(ORDER BY Sem_ID) N
  FROM acc_seminar.t_Seminar
  JOIN dbo.tbl_hotel h ON Sem_Ht_ID=h.pk_hotel
  WHERE Sem_ID IN (SELECT value  
  FROM fn_Split(@Sem_ID, ';')  
  WHERE RTRIM(value) <> '') AND (Sem_SemTh_ID != 76 AND Sem_Sto = 0)
),
cte2 AS(
  SELECT tu.TU_Name AS Name, tu.TU_Vorname AS Vorname, ParkgebuehrBrutto AS Preis, ROW_NUMBER()OVER(ORDER BY tu.TU_Name) N
  FROM acc_seminar.t_Teiluber_Ext tux
  JOIN acc_seminar.t_Teiluber tu ON tux.TeiluberId = tu.TU_ID
  JOIN acc_seminar.t_Seminar s ON tu.TU_Sem_ID = s.Sem_ID
  WHERE s.Sem_ID IN (SELECT value  
                     FROM fn_Split(@Sem_ID, ';')  
                     WHERE RTRIM(value) <> '')
    AND ParkgebuehrBrutto IS NOT NULL
)
SELECT
  cte1.Sem_ID, cte1.Sem_KW, cte1.Sem_Jahr, cte1.Sem_SemNrKata, cte1.Sem_von, cte1.Sem_bis, cte1.chr_name, cte1.chr_ort,
  cte2.Name,cte2.Vorname,cte2.Preis
FROM cte1
FULL JOIN cte2 ON cte1.N=cte2.N
ORDER BY ISNULL(cte1.N,cte2.N)

但是我不明白为什么您在报表中不使用两个不同的数据集?