如何在最外层的SELECT中引用子查询中的列?

时间:2017-10-28 02:58:21

标签: sql sql-server select

说明:我试图引用SomeIDReadings的列,该表由Readings网络中的根节点组成1}} Connections使用UpstreamIDDownstreamID进行建模。

生成的查询为我提供了一组根Readings的所有上游Readings,并按月{a {}}'汇总/分组。 DailyReadings属于。{/ p>

到目前为止我有什么

我有以下查询:

SomeDate

问题

我可以从开始遍历的根-- Query values of readings upstream of AnotherIDs that match their SpecialNumber for a whole month, summing values SELECT md.SomeID AS 'Upstream SomeID', m.AnotherID, -- I'd like to refer to the root SomeID here CAST(DATEPART(YEAR, SomeDate) AS VARCHAR) + ', ' + CAST(DATEPART(MONTH, SomeDate) AS VARCHAR) AS 'Month', SUM(md.SomeValue) AS 'Sum of SomeValue' FROM DailyReadings rd INNER JOIN Readings r ON r.SomeID = rd.SomeID WHERE rd.SomeID IN ( SELECT UpstreamID FROM Connections WHERE DownstreamID IN ( -- I want this column in my outer select. -- I want a column for this ID (SomeID) SELECT SomeID FROM Readings WHERE SpecialNumber = AnotherID AND SpecialNumber IN ( '227796B', '225044', '578283', '578133', '578132', '577925', '577878', '224998', '228282', '577394', '227844', '225094', '578131', '218374', '577732', '227845', '224978', '227773', '578280', '578078', '577930', '228220', '578121', '227389', '218206', '578179', '228034', '227864', '22593', '578040', '578131' ) OR SpecialNumber LIKE '%228166%' OR SpecialNumber LIKE '%577968%' OR SpecialNumber LIKE '%228220%' OR SpecialNumber LIKE '%577394%' OR SpecialNumber LIKE '%578132%' ) ) GROUP BY rd.SomeID, AnotherID, DATEPART(YEAR, SomeDate), DATEPART(MONTH, SomeDate) ORDER BY 'Month' DESC, AnotherID DESC 集中显示SomeID,以查找结果表中每个行的上游Readings吗?

2 个答案:

答案 0 :(得分:0)

我建议您随时创建子表然后加入它们,也可以使用Cte完成,但首先尝试一下。您可能希望在选择列表中添加所需的列,并相应地修改组。

    SELECT  
     md.SomeID AS 'Upstream SomeID'
    ,m.AnotherID
    -- I'd like to refer to the root SomeID here
    ,CAST(DATEPART(YEAR, SomeDate) AS VARCHAR) + ', ' + CAST(DATEPART(MONTH, SomeDate) AS VARCHAR) as 'Month' 
    ,SUM(md.SomeValue) AS 'Sum of SomeValue' 
FROM DailyReadings rd 
INNER JOIN Readings r ON r.SomeID = rd.SomeID
INNER JOIN (SELECT UpstreamID 
    FROM Connections) A
On rd.someid = a.upstreamid
INNER JOIN (SELECT SomeID 
        FROM Readings
        WHERE SpecialNumber = AnotherID
        AND SpecialNumber IN  
        ( 
             '227796B' 
            ,'225044' 
            ,'578283' 
            ,'578133' 
            ,'578132' 
            ,'577925' 
            ,'577878' 
            ,'224998' 
            ,'228282' 
            ,'577394' 
            ,'227844' 
            ,'225094' 
            ,'578131' 
            ,'218374' 
            ,'577732' 
            ,'227845' 
            ,'224978' 
            ,'227773' 
            ,'578280' 
            ,'578078' 
            ,'577930' 
            ,'228220' 
            ,'578121' 
            ,'227389' 
            ,'218206' 
            ,'578179' 
            ,'228034' 
            ,'227864' 
            ,'22593' 
            ,'578040' 
            ,'578131' 
        ) 
        OR SpecialNumber LIKE '%228166%' 
        OR SpecialNumber LIKE '%577968%' 
        OR SpecialNumber LIKE '%228220%' 
        OR SpecialNumber LIKE '%577394%' 
        OR SpecialNumber LIKE '%578132%') B
On A.DownstreamId = B.SomeId

答案 1 :(得分:0)

因此,您必须根据实际架构进行翻译,但我不知道为什么您不能加入这些表格。请注意,阅读表连接两次:

{{1}}