SQL查询计算重复项数量

5

我得到的结果是:

Code    Declaration 
123     a1 - 2nos
123     a2 - 230nos
123     a3 - 5nos
123     a1 - 100nos
123     a3 - 6nos

可以将重复的内容相加,然后按照以下格式显示输出:
Code    Declaration
123     a1 - 102nos
123     a2 - 230nos
123     a3 - 11nos

3
你可以分享一下SQL查询语句吗? - Max
3个回答

4
我将根据您之前发布的问题回答此问题。下面是CTE版本,它将拆分数据并计算声明的总数:
;with cte (code, DeclarationItem, Declaration) as
(
  select Code,
    cast(left(Declaration, charindex(',',Declaration+',')-1) as varchar(50)) DeclarationItem,
         stuff(Declaration, 1, charindex(',',Declaration+','), '') Declaration
  from yourtable
  union all
  select code,
    cast(left(Declaration, charindex(',',Declaration+',')-1) as varchar(50)) DeclarationItem,
    stuff(Declaration, 1, charindex(',',Declaration+','), '') Declaration
  from cte
  where Declaration > ''
),
s2 as
(
  select code, 
    ltrim(rtrim(left(declarationitem, charindex('-', declarationitem)-1))) col1
    , reverse(left(reverse(declarationitem), charindex('-', reverse(declarationitem))-1)) col2
  from cte
),
fnl as
(
  select code, col1,
    left(col2, patindex('%[Aa-Zz]%', col2)-1) value,
    substring(col2, patindex('%[Aa-Zz]%', col2), len(col2)) str
  from s2
) 
select code, col1 +' - '+ cast(sum(cast(value as int)) as varchar(50)) + str as declarationitem
from fnl
group by code, col1, str

请查看带演示的SQL Fiddle:http://sqlfiddle.com/#!3/82d50/42 结果如下:
| CODE | DECLARATIONITEM |
--------------------------
|  123 | 123 a1 - 102nos |
|  123 | 123 a2 - 230nos |
|  123 |  123 a3 - 11nos |

在声明项中分割这个连字符是否可能?这意味着我需要将a1 - 102 nos显示到三列中。如果连字符在一列中,那么我会将其删除并在网页上显示。 - Affan
@Affan,你希望结果看起来像什么? - Taryn
我需要在ASP网页中打印这个SQL结果..我需要将这个声明项拆分成两个..我不想显示这个连字符.. - Affan
我需要将结果显示在网页上,格式如下: 代码声明 数量 123 a1 102个 123 a2 230个 123 a3 11个我需要将该声明项拆分成两个部分,不使用连字符...你能指导我吗? - Affan
将来如果您有其他问题,应该直接发布一个新的问题以供参考。 - Taryn

2
Declare @t Table([Code] int, [Declaration] varchar(max))    
Insert Into @t VALUES (123,'a1 - 2nos'),(123,'a2 - 230nos'),(123,'a3 - 5nos'),(123,'a1 - 100nos'),(123,'a3 - 6nos')


;With Cte As(
Select 
    Code
    ,Substring([Declaration],0,PatIndex('%-%',[Declaration])) Part1 
    ,Cast(Substring(LTRIM(RTRIM(Substring([Declaration],PatIndex('%-%',[Declaration])+1,Len([Declaration])))),0,PatIndex('%nos%',LTRIM(RTRIM(Substring([Declaration],PatIndex('%-%',[Declaration])+1,Len([Declaration])))))) As Int) Part2
From @t)
Select Code,Part1 + '-' +  Cast(Sum(Part2) AS Varchar(10)) + 'nos' As Declaration
From Cte
Group By Code,Part1

1
SELECT Code, SUBSTRING(Declaration, 0, CHARINDEX('-', Declaration) + 2) + 
             CAST(SUM(SUBSTRING(Declaration,
                                CHARINDEX('-', Declaration) + 2, 
                                PATINDEX('%[Aa-Zz]%', SUBSTRING(Declaration, 
                                                                CHARINDEX('-', Declaration) + 2,
                                                                LEN(Declaration)
                                                                )
                                         ) - 1
                                ) + 0) AS varchar(max)) + 
             REVERSE(SUBSTRING(REVERSE(Declaration), 0, PATINDEX('%[0-9]%', REVERSE(Declaration)))) AS Decalration
FROM your_table
GROUP BY Code, 
         SUBSTRING(Declaration, 0, CHARINDEX('-', Declaration) + 2),
         REVERSE(SUBSTRING(REVERSE(Declaration), 0, PATINDEX('%[0-9]%', REVERSE(Declaration))))

演示SQLFiddle


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接