在办公室,我们需要合并一个MS SQL表,每个Quotaion号应该得到1行。 见表:https://drive.google.com/file/d/0B8AuHxQASEWyaEhfSVpDT3lIV1U/view?usp=sharing
尝试使用SELECT DISTINCT
但无法获得它。
SQL Server查询:
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
答案 0 :(得分:2)
如果保留所有专栏,则无法保留。您可以从select
中删除[FamilyDescription],[NameVariant],[StringValue],也可以在目标结果中为这些值添加新列。
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
你应该做些什么来保持所有价值观:
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