执行子查询备选方案

时间:2017-05-10 08:06:15

标签: sql sql-server tsql

我正在寻找有关如何绕过

的建议
  

“无法对包含。的表达式执行聚合函数   聚合或子查询“。

在@Instances代码中选择PlateID,在下面的示例中。有没有人有任何想法或建议?

DECLARE @Instances AS TABLE(PlateID INT);

INSERT INTO @Instances(PlateID)VALUES(11638),(11637),(11632),(11659)

DECLARE @NumberofPlates INT;
SELECT @NumberofPlates = COUNT(*) FROM @Instances;

SELECT Instance_Plate_Room_Instance_ID_LNK 
from dbo.M_Instance_Plate 
WHERE Instance_Plate_Deleted = 0 
group by Instance_Plate_Room_Instance_ID_LNK
having sum(case 
              when Instance_Plate_Plate_ID_LNK not in (SELECT PlateID
                                                       FROM @Instances)
                 then 1 else 0 end) = 0 and
       SUM(case 
              when Instance_Plate_Plate_ID_LNK in (SELECT PlateID 
                                                   FROM @Instances) 
                 then 1 else 0 end) = @NumberofPlates;

3 个答案:

答案 0 :(得分:0)

如果没有你在查询中包含的物理表的结构,我会为此模拟一些随机数据,并将一个似乎有用的查询组合在一起?

DECLARE @Instances AS TABLE(PlateID INT);
INSERT INTO @Instances(PlateID) VALUES (11638),(11637),(11632),(11632);

DECLARE @M_Instance_Plate TABLE (Instance_Plate_Plate_ID_LNK INT, Instance_Plate_Deleted INT, Instance_Plate_Room_Instance_ID_LNK INT);
INSERT INTO @M_Instance_Plate SELECT 11638, 0, 100;
INSERT INTO @M_Instance_Plate SELECT 11637, 0, 100;
INSERT INTO @M_Instance_Plate SELECT 11632, 0, 100;
INSERT INTO @M_Instance_Plate SELECT 11632, 0, 200;
INSERT INTO @M_Instance_Plate SELECT 11632, 1, 300;

DECLARE @NumberofPlates INT;
SELECT @NumberofPlates = COUNT(*) FROM @Instances;

WITH x AS (
    SELECT 
        Instance_Plate_Room_Instance_ID_LNK,
        SUM(CASE WHEN Instance_Plate_Plate_ID_LNK IS NULL THEN 1 ELSE 0 END) AS test_1, --Any missing
        SUM(CASE WHEN Instance_Plate_Plate_ID_LNK IS NOT NULL THEN 1 ELSE 0 END) AS test_2 --Has coverage
    FROM 
        @M_Instance_Plate ip
        LEFT JOIN @Instances i ON i.PlateID = ip.Instance_Plate_Plate_ID_LNK
    WHERE 
        Instance_Plate_Deleted = 0
    GROUP BY 
        Instance_Plate_Room_Instance_ID_LNK)
SELECT
    Instance_Plate_Room_Instance_ID_LNK
FROM
    x
WHERE
    test_1 = 0
    AND test_2 = @NumberofPlates;

答案 1 :(得分:0)

INSERT INTO @Instances(PlateID)VALUES(11638),(11637),(11632),(11659)

--DECLARE @NumberofPlates INT;
--SELECT @NumberofPlates = COUNT(*) FROM @Instances;

SELECT Instance_Plate_Room_Instance_ID_LNK 
FROM dbo.M_Instance_Plate p
WHERE Instance_Plate_Deleted = 0 
    AND NOT EXISTS (

        SELECT 1 
        FROM @Instances i
            LEFT JOIN M_Instance_Plate m ON i.PlateID = m.Instance_Plate_Plate_ID_LNK
        WHERE m.Instance_Plate_Room_Instance_ID_LNK = p.Instance_Plate_Room_Instance_ID_LNK
            AND m.Instance_Plate_Plate_ID_LNK IS NULL

        UNION ALL

        SELECT 1
        FROM @Instances i
            JOIN M_Instance_Plate m ON i.PlateID = m.Instance_Plate_Plate_ID_LNK
        WHERE m.Instance_Plate_Room_Instance_ID_LNK = p.Instance_Plate_Room_Instance_ID_LNK
        GROUP BY i.PlateID
        HAVING COUNT(*) != 1
    )

答案 2 :(得分:0)

试试这个。是等效表达式(检查table变量是否存在于table变量中,然后与变量@numberofplates匹配)

HAVING @NumberofPlates = (
        SELECT COUNT(1) AS cc
        FROM @Instances AS a
        WHERE a.PlateID = Instance_Plate_Plate_ID_LNK
        )