我如何连接2个字段,但我也有CASE条件。这是我的sql:
INSERT INTO #TempTable([Name],[TotalBets])
SELECT CASE
WHEN @Limited ='All'
THEN DPL.[FirstName]
WHEN @Limited ='Blind_No'
THEN DPL.[FirstName]
ELSE '*****'
END AS DPL.[FirstName] + ' ' +
CASE
WHEN @Limited ='All'
THEN DPL.[LastName]
WHEN @Limited ='Blind_No'
AND [BlindAccount] = 1
THEN '*****'
WHEN @Limited ='Blind_No'
AND [BlindAccount] = 0
THEN DPL.[LastName]
ELSE '*****'
END AS DPL.[LastName] [Name],
SUM(FWA.TotalBetAmount) [TotalBets]
FROM [WarehouseMgmt].[FactWalletAgr] FWA
JOIN [WarehouseMgmt].[DimPlayer] DPL ON FWA.[PlayerId] = DPL.[Id]
JOIN [WarehouseMgmt].[DimGame] DG ON FWA.[GameId] = DG.[Id]
GROUP BY DPL.[FirstName] + ' ' + DPL.[LastName]
但是这个查询是错误的,当我有CASE条件时我不知道如何连接。 有任何帮助或建议吗?
答案 0 :(得分:2)
您不能在group by子句中使用别名。我已经改变了它,如下所示。如果您使用的是sql server 2008的上述版本,请使用concat
函数。连接两个字符串连接有时会导致NULL
。
INSERT INTO #TempTable([Name],[TotalBets])
SELECT ( CASE
WHEN @Limited ='All'
THEN DPL.[FirstName]
WHEN @Limited ='Blind_No'
THEN DPL.[FirstName]
ELSE '*****'
END) + ' ' +
( CASE
WHEN @Limited ='All'
THEN DPL.[LastName]
WHEN @Limited ='Blind_No'
AND [BlindAccount] = 1
THEN '*****'
WHEN @Limited ='Blind_No'
AND [BlindAccount] = 0
THEN DPL.[LastName]
ELSE '*****'
END ) [Name],
SUM(FWA.TotalBetAmount) [TotalBets]
FROM [WarehouseMgmt].[FactWalletAgr] FWA
JOIN [WarehouseMgmt].[DimPlayer] DPL ON FWA.[PlayerId] = DPL.[Id]
JOIN [WarehouseMgmt].[DimGame] DG ON FWA.[GameId] = DG.[Id]
GROUP BY ( CASE
WHEN @Limited ='All'
THEN DPL.[FirstName]
WHEN @Limited ='Blind_No'
THEN DPL.[FirstName]
ELSE '*****'
END) + ' ' +
( CASE
WHEN @Limited ='All'
THEN DPL.[LastName]
WHEN @Limited ='Blind_No'
AND [BlindAccount] = 1
THEN '*****'
WHEN @Limited ='Blind_No'
AND [BlindAccount] = 0
THEN DPL.[LastName]
ELSE '*****'
END )
答案 1 :(得分:0)
在派生表中执行case
和连接。 Group by
结果。
select [Name], sum(TotalBetAmount) as [TotalBets]
from
(
SELECT CASE
WHEN @Limited ='All'
THEN DPL.[FirstName]
WHEN @Limited ='Blind_No'
THEN DPL.[FirstName]
ELSE '*****'
END + ' ' +
CASE
WHEN @Limited ='All'
THEN DPL.[LastName]
WHEN @Limited ='Blind_No'
AND [BlindAccount] = 1
THEN '*****'
WHEN @Limited ='Blind_No'
AND [BlindAccount] = 0
THEN DPL.[LastName]
ELSE '*****'
END AS [Name],
FWA.TotalBetAmount AS TotalBetAmount
FROM [WarehouseMgmt].[FactWalletAgr] FWA
JOIN [WarehouseMgmt].[DimPlayer] DPL ON FWA.[PlayerId] = DPL.[Id]
JOIN [WarehouseMgmt].[DimGame] DG ON FWA.[GameId] = DG.[Id]
) dt
GROUP BY [Name]
答案 2 :(得分:0)
您的查询的一个问题是表达式中间的as
- 以及使用带有句点的别名。也许这解决了你的问题:
INSERT INTO #TempTable([Name],[TotalBets])
SELECT ( (CASE WHEN @Limited = 'All' THEN DPL.[FirstName]
WHEN @Limited = 'Blind_No' THEN DPL.[FirstName]
ELSE '*****'
END)+ ' ' +
(CASE WHEN @Limited = 'All' THEN DPL.[LastName]
WHEN @Limited = 'Blind_No' AND [BlindAccount] = 1 THEN '*****'
WHEN @Limited ='Blind_No' AND [BlindAccount] = 0
THEN DPL.[LastName]
ELSE '*****'
END)
) as [Name],
SUM(FWA.TotalBetAmount) as [TotalBets]
FROM [WarehouseMgmt].[FactWalletAgr] FWA JOIN
[WarehouseMgmt].[DimPlayer] DPL
ON FWA.[PlayerId] = DPL.[Id] JOIN
[WarehouseMgmt].[DimGame] DG
ON FWA.[GameId] = DG.[Id]
GROUP BY DPL.[FirstName] + ' ' + DPL.[LastName];
这假设您不想将"盲人"将名称分成一行。