如何在SQL查询的一个字段中求和多个字段?

时间:2019-01-07 07:38:28

标签: sql sql-server

输入中有很多代码,此代码有一个父级

SELECT [fromAccCode]= 
CASE WHEN [fromAccCode] IN (201010, 201020, 201442, 201521, 201611,201612,209050,
209060,209090,209110,213040,213163,213165,213166,213190) THEN r1
WHEN [fromAccCode] IN (201040, 201048, 201050, 201090, 201120, 201150
,201155) THEN r2
WHEN [fromAccCode] IN (201443,206020,207010,207040,207100,207130,209612
) THEN r3

我用此代码设置输入

SELECT [fromAccCode] FROM [dbo].[source_table] WHERE ','+'201010, 
201020,259861,754265,213040,213163,213165,201040,
201048, 201050,201443,206020'+',' like '%,'+cast([fromAccCode] 
AS varchar(10))+',%'

我希望您有3记录1,2,3的值{sum),值之和(r1)和值之和({ {1}}),并且此字段具有值

2 个答案:

答案 0 :(得分:1)

最初,我认为您可以拆分WHEN,然后将它们求和。

SELECT 
SUM(CASE WHEN [fromAccCode] IN (201010, 201020, 201442, 201521, 201611, 201612, 209050, 209060, 209090, 209110, 213040, 213163, 213165, 213166, 213190) THEN r1 END) AS TotalR1,
SUM(CASE WHEN [fromAccCode] IN (201040, 201048, 201050, 201090, 201120, 201150, 201155) THEN r2 END) AS TotalR2,
SUM(CASE WHEN [fromAccCode] IN (201443, 206020, 207010, 207040, 207100, 207130, 209612) THEN r3 END) AS TotalR3
FROM [dbo].[source_table]
WHERE [fromAccCode] IN (
  201010, 201020, 201442, 201521, 201611, 201612, 209050, 209060, 209090, 209110, 213040, 213163, 213165, 213166, 213190,
  201040, 201048, 201050, 201090, 201120, 201150, 201155,
  201443, 206020, 207010, 207040, 207100, 207130, 209612);

但是您似乎在寻找这种怪兽。

SELECT src.fromAccCode,
(CASE 
 WHEN ref.TypeR = 1 THEN src.r1
 WHEN ref.TypeR = 2 THEN src.r2
 WHEN ref.TypeR = 3 THEN src.r3 
 END) AS rx,
SUM([value]) as SumValues
FROM [dbo].[source_table] as src
JOIN (SELECT DISTINCT fromAccCode, TypeR FROM (VALUES 
 (201010,1), (201020,1), (201442,1), (201521,1), (201611,1), (201612,1), (209050,1), (209060,1), (209090,1), (209110,1), 
 (213040,1), (213163,1), (213165,1), (213166,1), (213190,1),
 (201040,2), (201048,2), (201050,2), (201090,2), (201120,2), (201150,2), (201155,2),
 (201443,3), (206020,3), (207010,3), (207040,3), (207100,3), (207130,3), (209612,3)
) val (fromAccCode, TypeR)) AS ref ON ref.fromAccCode = src.fromAccCode
GROUP BY src.fromAccCode,
(CASE 
 WHEN ref.TypeR = 1 THEN src.r1
 WHEN ref.TypeR = 2 THEN src.r2
 WHEN ref.TypeR = 3 THEN src.r3 
 END);

顺便说一句,关于类似的标准。
使用CONCAT而不是强制转换为varchar也可以。

WHERE ',' + '201010,201020,259861,754265,213040,213163,213165,201040,201048,201050,201443,206020' + ',' 
      LIKE CONCAT('%,', [fromAccCode], ',%')

答案 1 :(得分:1)

首先:添加一个字段以设置案例数

$ which ihaskell
/home/yuvilio/.local/bin/ihaskell
$ ihaskell install --stack
$ jupyter kernelspec list
Available kernels:
  haskell      /home/yuvilio/.local/share/jupyter/kernels/haskell
...
$

您不需要以下代码段:

$ jupyter-console --kernel=haskell
Jupyter console 5.2.0
IHaskell 0.9.1.0 GHC 8.6.3
In [1]: sum[1..5]
:15
In [2]: double x = x + x
In [3]: double 3
:6

因为您有fromAccCode值

秒:

  select  [fromAccCode], --your code
field_name=  --add this code
          CASE 
      WHEN [fromAccCode] IN (201010,201020,201442,201521,201611,201612,209050,209060,209090,209110,213040,213163,213165,213166,213190) THEN 'r1         
      WHEN [fromAccCode] IN (201040,201048,201050,201090,201120,201150,201155) THEN r2
      WHEN [fromAccCode] IN (201443,206020,207010,207040,207100,207130,209612) THEN r3     
               ELSE null
          END