我连接了四个表,但如果我使用第四个表的属性进行GROUP BY,我会得到不同的结果。这是查询:
答案 0 :(得分:0)
基本上有两种选择:
使用嵌套查询加入原始表。
SELECT TA.col1, AggrFunc(col2) AS col2,
(SELECT col3 -- TOP 1? MAX? It must be single row
FROM table1 AS TB
JOIN TA ON TA. = TB. -- INNER JOIN? LEFT OUTER JOIN?
FROM table1 AS TB JOIN table2 JOIN table3
GROUP BY TA.col1;
或使用CTE。您可以更好地控制要返回多少行的额外列
WITH CTE AS
(
SELECT col1, AggrFunc(col2) AS col2
FROM ... JOINs
GROUP BY col1
)
SELECT CTE.*, table1.col3
FROM CTE
JOIN table1 --INNER JOIN? LEFT OUTER JOIN?
尽可能使用窗口功能
SELECT col1, AggrFunc(col2) OVER (PARTITION BY col1) AS col2, extra_col3
FROM ...JOINs...
然后您可以在上面查询CTE或FROM子句以进一步过滤或分组。
SELECT
FROM (query above)
WHERE ...
GROUP BY ...
问题是一样的:如何获得单个extra_col3(在您的情况下为SKU.[Reorder Cycle]
)行?当您的分组数据有多个匹配项时,如何选择一条记录?
答案 1 :(得分:0)
Oke,这是在做这个工作(噢,我做了一个转轴):