简体   繁体   English

查询返回到行而不是一个

[英]Query is returning to rows instead of just one

I have aa query to return the dimensions of a package in M2 (square metres) and UN (unity's).我有一个查询以返回 M2(平方米)和 UN(单位)的 package 的尺寸。 With the current query it is returning two different lines, because I am using a CASE WHEN .对于当前查询,它返回两条不同的行,因为我使用的是CASE WHEN This is the query:这是查询:

SELECT DISTINCT(C.Package) 'Package',
    CASE S.Unity WHEN 'M2' THEN SUM(L.Qt*S.ConvEst) ELSE NULL END 'M2',
    CASE S.Unity WHEN 'UN' THEN SUM(L.Qt) ELSE NULL END 'UN'
FROM 
    PackageTable AS C
INNER JOIN 
    PackageTableRows L ON L.Package = C.Package
INNER JOIN 
    Products S ON S.Product = L.Product
WHERE 
    C.Package = '587496'
GROUP BY 
    C.Package, S.Unity

This result:这个结果:

在此处输入图像描述

But what I really want is the query to return is something like this:但我真正想要的是返回的查询是这样的:

在此处输入图像描述

With only one line.只有一条线。 I know for that I am not using CASE WHEN correctly and that is why I need your help.我知道我没有正确使用CASE WHEN ,这就是我需要你帮助的原因。

You have several problems here.你在这里有几个问题。 Firstly, DISTINCT is not a function it's an operator.首先, DISTINCT不是function 它是一个运算符。 DISTINCT affects the entire dataset and causes only distinct rows to be returned. DISTINCT影响整个数据集并导致仅返回不同的行。 It's not DISTINCT ({Column Name}) it's SELECT DISTINCT {Columns} .它不是DISTINCT ({Column Name})它是SELECT DISTINCT {Columns}

Next, you have both DISTINCT and GROUP BY ;接下来,您同时拥有DISTINCTGROUP BY this is a flaw.这是一个缺陷。 A GROUP BY clause already causes your data to be returned in distinct groups, so a DISTINCT is both redundant and unneeded overhead. GROUP BY子句已经导致您的数据以不同的组返回,因此DISTINCT既是多余的又是不必要的开销。 Get rid of the DISTINCT .摆脱DISTINCT If you are getting different results when you have a DISTINCT with a GROUP BY this is a strong indication that your GROUP BY clause is wrong and needs addressing (most likely you have too many columns in the clause).如果您在使用GROUP BYDISTINCT得到不同的结果,这强烈表明您的GROUP BY子句是错误的并且需要解决(很可能子句中的列太多)。

Finally, when performing conditional aggregation the aggregate function should be around the entire CASE expression, not an expression in the THEN .最后,在执行条件聚合时,聚合 function 应该围绕整个CASE表达式,而不是THEN中的表达式。 Then also means that you then need to remove the column in your WHEN clause from the GROUP BY as I suspect the only reason you have it there is because you had to:然后也意味着您需要从GROUP BY中删除WHEN子句中的列,因为我怀疑您拥有它的唯一原因是因为您必须

This results in:这导致:

SELECT C.Package AS Package,
       SUM(CASE S.Unity WHEN 'M2' THEN L.Qt * S.ConvEst END) AS M2,
       SUM(CASE S.Unity WHEN 'UN' THEN L.Qt END) AS UN
FROM dbo.PackageTable C
     INNER JOIN dbo.PackageTableRows L ON L.Package = C.Package
     INNER JOIN dbo.Products S ON S.Product = L.Product
WHERE C.Package = '587496'
GROUP BY C.Package;

It's mostly correct.大部分是正确的。 You need to GROUP BY only on C.Package to bring it into a single line.您只需对 C.Package 进行 GROUP BY 即可将其合并到一行中。 For this it should return 0 for case else conditions and aggregation should be on the full case conditions rather than only on the measure.为此,对于 case else 条件,它应该返回 0,并且聚合应该在完整的 case 条件下,而不仅仅是在度量上。

So it will look like this.所以它看起来像这样。

SELECT C.Package 'Package',
SUM(CASE S.Unity WHEN 'M2' THEN (L.Qt*S.ConvEst) ELSE 0 END ) as 'M2', 
SUM(CASE S.Unity WHEN 'UN' THEN SL.Qt ELSE 0 END) AS 'UN'
FROM PackageTable AS C
INNER JOIN PackageTableRows L ON L.Package=C.Package
INNER JOIN Products S ON S.Product=L.Product
WHERE C.Package='587496'
GROUP BY C.Package

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM