我回复原始查询,我只想问一个问题(ps:我知道我必须投票和注册,我保证今天会这样做)
使用以下查询(t-sql)我得到了正确的结果,除了现在有重复。
我一直在阅读并认为我可以使用PARTITION BY语法 - 您能否告诉我如何合并PARTITION BY语法?
WITH CALC1 AS (SELECT OTQUOT, OTIT01 AS ITEMS, ROUND(OQCQ01 * OVRC01,2) AS COST
FROM
@LIB@.RTQOTA
WHERE OTIT01 <> ''
UNION ALL
...
SELECT OTQUOT, OTIT10 AS ITEMS, ROUND(OQCQ10 * OVRC10,2) AS COST
FROM
@LIB@.RTQOTA
WHERE OTIT10 <> ''
)
SELECT OTQUOT, DESC, ITEMS, RN
FROM (
SELECT OTQUOT, ITEMS, B.IXRPGP AS GROUP, C.OTRDSC AS DESC, COST, ROW_NUMBER() OVER
(PARTITION BY OTQUOT ORDER BY COST DESC) AS RN
FROM CALC1 AS A INNER JOIN @LIB@.ITMCON AS B ON (A.ITEMS = B.IKITMC) INNER JOIN
DATAGRP.GDSGRP AS C ON (B.IXRPGP = C.OKRPGP)
) T
结果:
60408169 FENCING GNCPDCTP18BGBG 1
60408169 FENCING CGIFESHPD1795BG 2
60408169 FENCING GTTCGIBG 3
60408169 FENCING GBTCGIBG 4
如何摆脱重复? 感谢比尔和所有其他人的帮助(我还在学习!)
答案 0 :(得分:1)
您可以将PARITION BY更改为
(PARTITION BY OTQUOT, C.OTRDSC, ITEMS ORDER BY COST DESC)
并创建一个WHERE CLAUSE
WHERE RN = 1
或者你可以简单地使用(SELECT DISTINCT)
<强> DISTINCT 强>
指定只有唯一的行 出现在结果集中。空值 被认为是相同的目的 DISTINCT关键字。
SELECT DISTINCT OTQUOT, DESC, ITEMS
另外,作为一个想法,为什么你在SUB SELECt中选择你没有在输出选择(B.IXRPGP AS GROUP
)中使用的列?