如何将两个SQL行(相同的项目ID)与SUM()数量合并,但只显示最后一行的信息?

时间:2014-07-11 21:50:42

标签: sql sql-server

我试图使用DISTINCT显示唯一记录,但不是每列都有相同的值。

enter image description here

有没有办法合并我的Count列的两行具有相同UPC(项目ID)的值,但只显示列的其余部分的第二(最新)信息?

enter image description here

因为我的'单位成本'有两个不同的值,我的DISTINCT函数仍然会产生两行而不是一行。

如果我只使用TOP 1,那么我的Count值将不会同时为SUM。

OVER(分区)或UNION功能是否有效?

感谢。

这是我当前的SQL查询:

SELECT DISTINCT TOP 10
--1.UPC
CASE WHEN [UPC] IS NULL THEN ( 'Grand Total: ' ) + ' (' +(CONVERT(varchar(10), COUNT(DISTINCT [UPC]))) + ' unique items)'
ELSE [UPC]
END AS [UPC]

--2.ItemCode
,   CASE WHEN [ItemCode] IS NULL THEN ''
    ELSE [ItemCode] 
    END AS [Item Code]

--3.Vendor
,ISNULL(CONVERT(varchar(30),[Vendor]),'') [Vendor#]

--4.MixMatch
,   CASE WHEN [MixMatch] IS NULL THEN ''
    ELSE [MixMatch] 
    END AS [Mix Match]

--5.Description
,   CASE WHEN [Description] IS NULL THEN ''
    ELSE [Description] 
    END AS [Description]

--6.CaseSize
,ISNULL(CONVERT(varchar(5),[CaseSize]),'') [Case Size]

--7.Size
,ISNULL(CONVERT(varchar(10),[Size]),'') [Size]

--8.Measure
,   CASE WHEN [Measure] IS NULL THEN ''
    ELSE [Measure] 
    END AS [Measure]

--9.Date Listed
,ISNULL(CONVERT(varchar(20),[CreationDate]),'') [Date Listed]

--10.Case Cost
--,ISNULL(CONVERT(varchar(20),[CaseCost]),'') [Case Cost]
,ISNULL( '$' + CONVERT(varchar(20),[CaseCost]),'') [Case Cost]

--11.Unit Cost
,ISNULL( '$' + CONVERT(varchar(20),[UnitCost]),'') [Unit Cost]

--14.Count
,CONVERT(int,ROUND(SUM([QtySold]),0)) AS [Count]

FROM [JS_Data].[dbo].[View_ItemMovement_TopSellers]
WHERE (UPC = '82983500006')
   AND (SaleDate BETWEEN '2013-05-01' AND '2014-05-01')

GROUP BY 
GROUPING SETS 
(
    ([UPC], [ItemCode], [Vendor], [MixMatch], [Description], [CaseSize], [Size],  [Measure], [CreationDate], [CaseCost], [UnitCost])
    ,()     
)
ORDER BY UPC

2 个答案:

答案 0 :(得分:2)

你想到partition by是正确的;虽然您还需要使用连接(或结果中的内联SQL)。简化示例如下:

select firstRow.id
, firstRow.upc
, firstRow.name
, sum(d.value) TotalUPCValue
from (
  select id, upc, name
  , row_number() over (partition by upc order by id) r
  from demo
) firstRow
inner join demo d on d.upc = firstRow.upc
where firstRow.r = 1
group by firstRow.id
, firstRow.upc
, firstRow.name

使用SQL Fiddle上的表定义的工作副本:http://sqlfiddle.com/#!6/6bfee/1

这是不使用连接的备用版本:

select id
, upc
, name
, (select sum(d.value) from demo d where d.upc = firstRow.upc) TotalUPCValue
from (
  select id, upc, name
  , row_number() over (partition by upc order by id) r
  from demo
) firstRow
where firstRow.r = 1

SQL小提琴:http://sqlfiddle.com/#!6/6bfee/2

第一种(加入)方法通常应该更快,但值得与您的数据进行比较以确认。

<强>更新

感谢@AndriyM改进我的第二个版本:

select id
, upc
, name
, TotalUPCValue
from (
  select id, upc, name
  , row_number() over (partition by upc order by id) r
  , sum(value) over (partition by upc) as TotalUPCValue
  from demo
) firstRow
where firstRow.r = 1
;

SQL小提琴:http://sqlfiddle.com/#!6/6bfee/7

答案 1 :(得分:0)

刚刚发现了一些东西,

如果我将单位成本列更改为

--11.Unit Cost
,ISNULL( '$' + CONVERT(varchar(20),max([UnitCost])),'') [Unit Cost]

并从我的Group By子句中删除[Unit Cost], 我似乎得到了我想要的输出。