SQL错误。子查询返回的值超过1

时间:2015-07-30 05:48:54

标签: sql sql-server tsql

我的查询工作正常:

DECLARE @StartDate DateTime  
DECLARE @EndDate DateTime  
SET @StartDate = FLOOR(CAST(DATEADD(WEEK, -1, GETDATE()) AS FLOAT));
SET @EndDate = CAST(GETDATE() AS FLOAT);          
WITH T1 AS 
(   
    SELECT 
        IA.NodeID, 
        IA.InterfaceID,
        SUM(IA.Availability * IA.Weight) AS Sum1,
        SUM(IA.Weight) AS Sum2
    from InterfaceAvailability IA WITH(NOLOCK)  
    where 
        IA.DateTime between @StartDate and @EndDate     
    group by                
        IA.NodeID,
        IA.InterfaceID
)

SELECT 
    IA.nodeid, IA.interfaceid, IA.NodesVendorIcon, IA.NodeName, 
    IA.InterfaceIcon, IA.InterfaceCaption, 
    IA.Availability
FROM (
    SELECT 
        Nodes.NodeID,
        Interfaces.InterfaceID,
        SUM(Sum1) / SUM(Sum2) AS Availability,
        Interfaces.InterfaceIcon, Interfaces.Caption as InterfaceCaption,
        Nodes.Caption as NodeName,
        Nodes.VendorIcon as NodesVendorIcon

    FROM T1
    INNER JOIN Nodes WITH(NOLOCK) 
ON 
    T1.NodeID = Nodes.NodeID
INNER JOIN Interfaces WITH(NOLOCK) 
ON 
    T1.InterfaceID = Interfaces.InterfaceID
    GROUP BY
        Nodes.NodeID, Interfaces.InterfaceID, Interfaces.InterfaceIcon, Interfaces.Caption, Nodes.Caption, Nodes.VendorIcon
        )
    AS IA 

ORDER BY
    NodeID, InterfaceID

但是当我尝试检索另一个我将查询修改为的CustomerName列时抛出SQL错误:

DECLARE @StartDate DateTime  
DECLARE @EndDate DateTime  
SET @StartDate = FLOOR(CAST(DATEADD(WEEK, -1, GETDATE()) AS FLOAT));
SET @EndDate = CAST(GETDATE() AS FLOAT);          
WITH T1 AS 
(   
    SELECT 
        IA.NodeID, 
        IA.InterfaceID,
        SUM(IA.Availability * IA.Weight) AS Sum1,
        SUM(IA.Weight) AS Sum2,
                                N.CustomerName as CustomerName
    from InterfaceAvailability IA WITH(NOLOCK) join Nodes N 
            on IA.NodeID = N.NodeID 

    where 
        IA.DateTime between @StartDate and @EndDate     
    group by                
        IA.NodeID,
        IA.InterfaceID, N.CustomerName
)

SELECT 
    IA.nodeid, IA.interfaceid, IA.NodesVendorIcon, IA.NodeName, 
    IA.InterfaceIcon, IA.InterfaceCaption, 
    IA.Availability,
                (Select Nodes.CustomerName from Nodes)
FROM (
    SELECT 
        Nodes.NodeID,
        Interfaces.InterfaceID,
        SUM(Sum1) / SUM(Sum2) AS Availability,
        Interfaces.InterfaceIcon, Interfaces.Caption as InterfaceCaption,
        Nodes.Caption as NodeName,
        Nodes.VendorIcon as NodesVendorIcon

    FROM T1
    INNER JOIN Nodes WITH(NOLOCK) 
ON 
    T1.NodeID = Nodes.NodeID
INNER JOIN Interfaces WITH(NOLOCK) 
ON 
    T1.InterfaceID = Interfaces.InterfaceID
    GROUP BY
        Nodes.NodeID, Interfaces.InterfaceID, Interfaces.InterfaceIcon, Interfaces.Caption, Nodes.Caption, Nodes.VendorIcon
        )
    AS IA 

请帮忙。谢谢

2 个答案:

答案 0 :(得分:1)

您可以将查询编写为:

SELECT 
    IA.nodeid, IA.interfaceid, IA.NodesVendorIcon, IA.NodeName, 
    IA.InterfaceIcon, IA.InterfaceCaption, 
    IA.Availability,
    IA.CustomerName 
FROM (
    SELECT 
        Nodes.NodeID,
        Interfaces.InterfaceID,
        SUM(Sum1) / SUM(Sum2) AS Availability,
        Interfaces.InterfaceIcon, Interfaces.Caption as InterfaceCaption,
        Nodes.Caption as NodeName,
        Nodes.VendorIcon as NodesVendorIcon,
        Nodes.CustomerName 
    FROM T1
    INNER JOIN Nodes WITH(NOLOCK) 
    ON 
    T1.NodeID = Nodes.NodeID
    INNER JOIN Interfaces WITH(NOLOCK) 
    ON 
    T1.InterfaceID = Interfaces.InterfaceID
    GROUP BY
    Nodes.NodeID, Interfaces.InterfaceID, Interfaces.InterfaceIcon,
    Interfaces.Caption, Nodes.Caption, Nodes.VendorIcon
    ,Nodes.CustomerName 
        )
    AS IA 

答案 1 :(得分:0)

如果我理解正确,问题在于这一行

(Select Nodes.CustomerName from Nodes)

据我所知,你可以拥有不确定数量的列(带有数据透视表的mayby,但我从未使用它)。因此,您无法将select添加为列。你能做的是使用select总是返回单个值(或NULL)的select。所以,如果你使用

(Select TOP 1 Nodes.CustomerName from Nodes)

并且可能是where子句,您可以使用它来选择所需的行。 我不确定单个值是否足够你,如果不是在t-sql中尝试使用google搜索数据透视表,但是我不确定它是否符合我的想法。