我有一个查询,它读取代码的描述。 两个查询都单独运行,它们返回一行但组合后又会产生一个重复的行。下面是查询
SELECT source, description = IsNull(Max(RTRIM(LTRIM(description))), 'UNKNOWN')
FROM SOURCES where source = RTRIM(LTRIM('SCDGIFT12'))
GROUP BY source
SELECT UPPER(SOURCECODE) as SOURCECODE,ISNULL(COUNT(ORDER_ID),0) AS ORDID
FROM SRC_CODE
WHERE CODE = 'SCDGIFT12' AND ORDER_DATE BETWEEN CONVERT(DATETIME,'10/01/2013')
AND CONVERT(DATETIME,'01/26/2014')
GROUP BY sourcecode
SELECT SOURCECODE,ORDID,DESCRIPTION
FROM ( SELECT UPPER(SOURCECODE) as SOURCECODE, ISNULL(COUNT(ORDER_ID),0) AS ORDID )
FROM SRC_CODE
WHERE SOURCECODE = 'SCDGIFT12' and ORDER_DATE BETWEEN CONVERT(DATETIME,'10/01/2013') AND CONVERT(DATETIME,'01/26/2014')AND (UPPER(SOURCECODE) IS NOT NULL)
GROUP BY UPPER(SOURCECODE)) AS T LEFT JOIN (
SELECT source, description = IsNull(Max(RTRIM(LTRIM(description))), 'UNKNOWN') from SOURCES group by source) as B ON RTRIM(LTRIM(T.SOURCECODE)) = RTRIM(LTRIM(B.SOURCE))
ORDER BY sourcecode
答案 0 :(得分:0)
我甚至不知道从哪里开始回答这个问题。
这是您重新格式化的代码,括号移到了正确的位置“我希望”
SELECT SOURCECODE
,ORDID
,DESCRIPTION
FROM ( SELECT UPPER(SOURCECODE) AS SOURCECODE
,ISNULL(COUNT(ORDER_ID), 0) AS ORDID
FROM SRC_CODE
WHERE SOURCECODE = 'SCDGIFT12'
AND ORDER_DATE BETWEEN CONVERT(DATETIME, '10/01/2013')
AND CONVERT(DATETIME, '01/26/2014')
AND ( UPPER(SOURCECODE) IS NOT NULL )
GROUP BY UPPER(SOURCECODE) ) AS T
LEFT JOIN ( SELECT source
,description = ISNULL(MAX(RTRIM(LTRIM(description))), 'UNKNOWN')
FROM SOURCES
GROUP BY source ) AS B
ON RTRIM(LTRIM(T.SOURCECODE)) = RTRIM(LTRIM(B.SOURCE))
ORDER BY sourcecode
我不确定你甚至想要用它来做什么,但如果你想要的是摆脱重复,你可以放DISTINCT
来消除重复的行,但这并不能解决你的根本问题。你需要弄清楚你想要计算的是什么,并想出更好的方法将它放在一起而不是加入子查询。