[英]Using CASE in a sql Select statement with count
I am trying to execute a Select
statement which includes multiple tables from a database and which has a calculated column of count added in it. 我试图执行一个
Select
语句,该语句包括一个数据库中的多个表,并在其中添加了计算所得的列数。 I have problem adding another calculated column where I want to use CASE
statement. 我在要使用
CASE
语句的地方添加另一个计算列时CASE
。
My code looks as follows: 我的代码如下所示:
SELECT
Customers.FirstName, Customers.LastName, Customers.PrivateNumber,
Branches.Name, Pawns.ID, Pawns.StartDate, Pawns.FinishDate,
Items.Name, Items.Mass, Items.Quantity, Pawns.CurrentSum,
Transactions.[Percent], Currencies.Name as Currency,
COUNT(1) OVER (PARTITION BY Pawns.ID) AS Cnt,
NewDistrPrcnt = (CASE WHEN COUNT(1) OVER (PARTITION BY Pawns.ID) > '1'
THEN Items.Mass/SUM(Transactions.[Percent])*Transactions.[Percent]
Else Transactions.[Percent] End)
FROM
Customers
INNER JOIN
Pawns ON Customers.ID = Pawns.CustomerID
INNER JOIN
Items ON Pawns.ID = Items.PawnID
INNER JOIN
Branches ON Pawns.BranchID = Branches.ID AND Pawns.LocationID = Branches.ID
INNER JOIN
PawnTypes ON Pawns.PawnTypeID = PawnTypes.ID
INNER JOIN
Currencies ON PawnTypes.CurrencyID = Currencies.ID
LEFT OUTER JOIN
Transactions ON Pawns.ID = Transactions.PawnID AND Pawns.FinishDate = Transactions.Date
WHERE
(Pawns.StatusID = 6) AND (Pawns.FinishDate BETWEEN '15Oct2013' AND '23Oct2013')
ORDER BY
Pawns.ID, Branches.Name, Pawns.FinishDate DESC
The following error is returned when I try to run the query: 当我尝试运行查询时,返回以下错误:
Msg 8120, Level 16, State 1, Line 1
消息8120,第16级,状态1,第1行
Column 'Customers.FirstName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.选择列表中的“ Customers.FirstName”列无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中。
Can anyone please help me in solving this issue? 谁能帮我解决这个问题?
The issue is with SUM(Transactions.[Percent])
.Replace it with window function as : (SUM(Transactions.[Percent]) over ())
and try as: 问题出在
SUM(Transactions.[Percent])
。将其替换为窗口函数: (SUM(Transactions.[Percent]) over ())
并尝试如下:
SELECT
Customers.FirstName, Customers.LastName, Customers.PrivateNumber,
Branches.Name, Pawns.ID, Pawns.StartDate, Pawns.FinishDate,
Items.Name, Items.Mass, Items.Quantity, Pawns.CurrentSum,
Transactions.[Percent], Currencies.Name as Currency,
COUNT(1) OVER (PARTITION BY Pawns.ID) AS Cnt
,NewDistrPrcnt = (CASE WHEN COUNT(1) OVER (PARTITION BY Pawns.ID) > '1'
THEN Items.Mass/(SUM(Transactions.[Percent]) over ())*Transactions.[Percent]
Else Transactions.[Percent] End)
FROM
Customers
INNER JOIN
Pawns ON Customers.ID = Pawns.CustomerID
INNER JOIN
Items ON Pawns.ID = Items.PawnID
INNER JOIN
Branches ON Pawns.BranchID = Branches.ID AND Pawns.LocationID = Branches.ID
INNER JOIN
PawnTypes ON Pawns.PawnTypeID = PawnTypes.ID
INNER JOIN
Currencies ON PawnTypes.CurrencyID = Currencies.ID
LEFT OUTER JOIN
Transactions ON Pawns.ID = Transactions.PawnID AND Pawns.FinishDate = Transactions.[Date]
WHERE
(Pawns.StatusID = 6) AND (Pawns.FinishDate BETWEEN '15Oct2013' AND '23Oct2013')
ORDER BY
Pawns.ID, Branches.Name, Pawns.FinishDate DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.