首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MS SQL Server数据整合

MS SQL Server数据整合
EN

Stack Overflow用户
提问于 2017-06-27 03:57:25
回答 1查看 67关注 0票数 1

在办公室我们需要合并一个MS SQL表,我们应该在每个Quotaion数字中得到1行。参见表:https://drive.google.com/file/d/0B8AuHxQASEWyaEhfSVpDT3lIV1U/view?usp=sharing

已尝试使用SELECT DISTINCT,但无法获取。

SQL Server查询:

代码语言:javascript
复制
    SELECT DISTINCT ([QuotationNumber])
      ,[CreationDate]
      ,[QuotationDate]
      ,[CustomerNumber]
      ,[CustomerName]
      ,[SalesPersonName]
      ,[Rreg]
      ,[ProductDescription]
      ,[FamilyDescription]
      ,[NameVariant]
      ,[StringValue]
FROM [ABC].[dbo].[DEF]
WHERE [CreationDate] > "2017-05-08 00:00:00.000"
ORDER BY [CreationDate] DESC
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-27 04:05:02

如果保留所有列,则无法保留。要么从select中删除FamilyDescription、NameVariant、StringValue,要么在目标结果中为这些值添加新列。

代码语言:javascript
复制
SELECT DISTINCT ([QuotationNumber])
      ,[CreationDate]
      ,[QuotationDate]
      ,[CustomerNumber]
      ,[CustomerName]
      ,[SalesPersonName]
      ,[Rreg]
      ,[ProductDescription]
FROM [ABC].[dbo].[DEF]
WHERE [CreationDate] > "2017-05-08 00:00:00.000"
ORDER BY [CreationDate] DESC

为了保留所有值,您应该执行以下操作:

代码语言:javascript
复制
SELECT DISTINCT [QuotationNumber]
      ,[CreationDate]
      ,[QuotationDate]
      ,[CustomerNumber]
      ,[CustomerName]
      ,[SalesPersonName]
      ,[Rreg]
      ,[ProductDescription]
      ,MIN([FamilyDescription]) [FamilyDescription]
      ,MAX(CASE WHEN NameVariant = 'DT_residual' THEN StringValue END) [DT_residual]
      ,MAX(CASE WHEN NameVariant = 'DT_interestRateSum' THEN StringValue END) [DT_interestRateSum]
      ,MAX(CASE WHEN NameVariant = 'DT_depositValue' THEN StringValue END) [DT_depositValue]
FROM [ABC].[dbo].[DEF]
GROUP BY [QuotationNumber]
      ,[CreationDate]
      ,[QuotationDate]
      ,[CustomerNumber]
      ,[CustomerName]
      ,[SalesPersonName]
      ,[Rreg]
      ,[ProductDescription]
HAVING [CreationDate] > "2017-05-08 00:00:00.000"
ORDER BY [CreationDate] DESC
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44767722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档