当我只做错误的解决方案[TOTALTesellum]时,我在case语句中使用SUM函数。我在这里可以做的是我的SQL代码
SELECT CUS.NAME AS [Tedarikçi], GOOD.NAME AS [Ürün] ,
SUM(CASE WHEN PDET.ORDERDATE <= GETDATE() THEN PDET.COUNT ELSE 0 END) AS[Haftalık Sipariş],
SUM(CASE WHEN PDET.COUNT IS NOT NULL THEN PDET.COUNT ELSE 0 END ) AS [Toplam Sipariş],
SUM(CASE WHEN TES.TESELLUMDATE IS NOT NULL THEN TES.ORDERAMOUNT ELSE 0 END) AS[TOTALTesellum],
-- SUM(TES.ORDERAMOUNT) AS [Toplam Tesellum],
(CASE WHEN SUM(CASE WHEN PDET.ORDERDATE <= GETDATE() THEN PDET.COUNT ELSE 0 END) > SUM(PDET.COUNT) THEN SUM(CASE WHEN PDET.ORDERDATE <= GETDATE() THEN PDET.COUNT ELSE 0 END) - SUM(PDET.COUNT) ELSE 0 END) AS [Fazlalık] ,
(CASE WHEN SUM(PDET.COUNT) > SUM(CASE WHEN PDET.ORDERDATE <= GETDATE() THEN PDET.COUNT ELSE 0 END) THEN SUM(PDET.COUNT) - SUM(CASE WHEN PDET.ORDERDATE <= GETDATE() THEN PDET.COUNT ELSE 0 END) ELSE 0 END) AS [Eksiklik]
--ORDER BY TES.TESELLUMDATE DESC AS [asxasxa]
FROM dbo.IDTESELLUMDATA TES INNER JOIN
dbo.IDLANDPRELOADS PRE ON TES.SUPPLIERID = PRE.EXPORTERCUSTOMERID
INNER JOIN
dbo.IDLANDPRELOADDETAILS PDET ON PDET.PRELOADID = PRE.ID
INNER JOIN
dbo.IDCUSTOMERS CUS ON TES.SUPPLIERID = CUS.ID AND
PRE.EXPORTERCUSTOMERID = CUS.ID
INNER JOIN
dbo.IDLANDGOODSDEFINITIONS GOOD ON TES.GOODID = GOOD.ID AND PDET.GOODSID = GOOD.ID
GROUP BY CUS.NAME , GOOD.NAME
答案 0 :(得分:0)
我从您的查询中了解到 - 只需重新格式化并重新安排脚本,这可以解决您的问题
SELECT M.[Ürün],
M.[Haftalık Sipariş],
M.[Toplam Sipariş],
M.[TOTALTesellum],
CASE WHEN M.[Haftalık Sipariş] > M.[PDET COUNT] THEN M.[Haftalık Sipariş] - M.[PDET COUNT] ELSE 0 END AS [Fazlalık],
CASE WHEN M.[PDET COUNT] > M.[Haftalık Sipariş] THEN M.[PDET COUNT]-M.[Haftalık Sipariş] ELSE 0 END AS [Eksiklik]
FROM(
SELECT CUS.NAME AS [Tedarikçi], GOOD.NAME AS [Ürün] ,
SUM(CASE WHEN PDET.ORDERDATE <= GETDATE() THEN PDET.COUNT ELSE 0 END) AS[Haftalık Sipariş],
SUM(CASE WHEN PDET.COUNT IS NOT NULL THEN PDET.COUNT ELSE 0 END ) AS [Toplam Sipariş],
SUM(DISTINCT CASE WHEN TES.TESELLUMDATE IS NOT NULL THEN TES.ORDERAMOUNT ELSE 0 END) AS[TOTALTesellum],
SUM(PDET.COUNT) AS [PDET COUNT]
FROM dbo.IDTESELLUMDATA TES
INNER JOIN dbo.IDLANDPRELOADS PRE ON TES.SUPPLIERID = PRE.EXPORTERCUSTOMERID
INNER JOIN dbo.IDLANDPRELOADDETAILS PDET ON PDET.PRELOADID = PRE.ID
INNER JOIN dbo.IDCUSTOMERS CUS ON TES.SUPPLIERID = CUS.ID AND PRE.EXPORTERCUSTOMERID = CUS.ID
INNER JOIN dbo.IDLANDGOODSDEFINITIONS GOOD ON TES.GOODID = GOOD.ID AND PDET.GOODSID = GOOD.ID
GROUP BY CUS.NAME, GOOD.NAME
)M