我目前有以下的查询编写的内部查询的设计师。 我问一个问题,昨天和它的工作对它自己的但我想将其纳入现有报告。
SELECT Distinct
i.ProductNumber
,i.ProductType
,i.ProductPurchaseDate
,ih.SalesPersonComputerID
,ih.SalesPerson
,ic2.FlaggedComments
FROM [Products] i
LEFT OUTER JOIN
(SELECT Distinct
MIN(c2.Comments) AS FlaggedComments
,c2.SalesKey
FROM [SalesComment] AS c2
WHERE(c2.Comments like 'Flagged*%')
GROUP BY c2.SalesKey) ic2
ON ic2.SalesKey = i.SalesKey
LEFT JOIN [SalesHistory] AS ih
ON ih.SalesKey = i.SalesKey
WHERE
i.SaleDate between @StartDate and @StopDate
AND ih.Status = 'SOLD'
我的问题昨天是我想要的方式选择只有第一个评论对每个销售。 我有一个查询的用于选择标记的意见,但我希望双方的第一排和标记的意见。 他们都是从同一个表中。 这是查询的提供和它的工作对它自己但我不能找出如何使其工作与现有的查询。
SELECT a.DateTimeCommented, a.ProductNumber, a.Comments, a.SalesKey
FROM (
SELECT
DateTimeCommented, ProductNumber, Comments, SalesKey,
ROW_NUMBER() OVER(PARTITION BY ProductNumber ORDER BY DateTimeCommented) as RowN
FROM [SalesComment]
) a
WHERE a.RowN = 1
谢谢你这么多你的帮助。