说明:我试图引用SomeID
表Readings
的列,该表由Readings
网络中的根节点组成1}} Connections
使用UpstreamID
和DownstreamID
进行建模。
生成的查询为我提供了一组根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
吗?
答案 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}}