具有MIN开放日期和产品类型的ID的唯一销售量

时间:2013-11-20 21:37:29

标签: sql-server

我想提取每日销售数据,它会显示每天开立的每个新帐户的单独行。我使用以下代码尝试了这一点,但是,我继续获取重复记录,并且不确定如何阻止这种情况发生。

该表格包含以下内容:

[AccountID]
[ID]
[AccountNumber]
[OpenDate]
[CloseDate]
[ProductType]


SELECT
[Account].[ID],
[ProdutType],
MIN ([OpenDate]) AS OpenDate,
MIN [CloseDate] AS CloseDate


FROM
[Account]

GROUP BY
[ID],
[ProductType]

1 个答案:

答案 0 :(得分:0)

请尝试使用此语法,根据您的查询进行调整。我需要看到结果

With cte as
(
select          

[AccountID],
[ID],
[AccountNumber],
[OpenDate],
[CloseDate],
[ProductType],
                ROW_NUMBER() OVER(PARTITION BY [AccountID] ORDER BY [OpenDate] ASC)  as [FileCount]
From                [Account]
)
select * from cte 
Group By [AccountID],
[ID],
[AccountNumber],
[OpenDate],
[CloseDate],
[ProductType]