现在,下面的查询检索了多条产品记录。如何将结果集限制为仅检索每个product_ID的一条记录?将从不同的产品返回多个记录,但每个product_id只需要一个产品系列。这是MS SQL 2005
SELECT DISTINCT dbo.Products.Product_ID AS Expr1,
CASE
WHEN dbo.Products.thumbnail IS NULL
OR dbo.Products.thumbnail = ''
THEN dbo.Products.Smimage
ELSE dbo.Products.thumbnail
END AS image ,
dbo.Products.ProductTitle ,
'<img WIDTH="62" src="http://media.companyinc.com/companyinc/SKUimages/small/' + dbo.Products.Smimage + '">' AS URLImage,
dbo.INV_dropshippers_To_ProductOptions.sku ,
dbo.Products.Discontinued ,
dbo.Products.CloseOut ,
dbo.Products.Special ,
dbo.Products.Active ,
dbo.Products.location_id ,
dbo.INV_dropshippers_To_ProductOptions.dropshipper_id ,
dbo.Products.season ,
dbo.Products.thumbnail ,
dbo.Products.Smimage ,
dbo.Products.CustomField2 ,
dbo.Products.pt_type ,
dbo.Products.PartNumber ,
dbo.Products.RetailPrice AS Price ,
dbo.skupurchasedreport.Product_Id ,
dbo.skupurchasedreport.totalprice_date1 ,
dbo.skupurchasedreport.totalprice_date2 ,
dbo.skupurchasedreport.qtypurchased_date1 ,
dbo.skupurchasedreport.qtypurchased_date2 ,
dbo.skupurchasedreport.totalprice_date3 ,
dbo.skupurchasedreport.qtypurchased_date3 ,
dbo.INV_dropshippers_To_ProductOptions.quantity AS currentqty
FROM dbo.Products
INNER JOIN dbo.skupurchasedreport
ON dbo.Products.Product_ID = dbo.skupurchasedreport.Product_Id
LEFT OUTER JOIN dbo.Options AS Options_2
RIGHT OUTER JOIN dbo.INV_dropshippers_To_ProductOptions
ON Options_2.Opt_ID = dbo.INV_dropshippers_To_ProductOptions.option_id3
LEFT OUTER JOIN dbo.Options AS Options_1
ON dbo.INV_dropshippers_To_ProductOptions.option_id2 = Options_1.Opt_ID
LEFT OUTER JOIN dbo.Options
ON dbo.INV_dropshippers_To_ProductOptions.option_id1 = dbo.Options.Opt_ID
ON dbo.Products.Product_ID = dbo.INV_dropshippers_To_ProductOptions.product_id
GROUP BY dbo.skupurchasedreport.Product_Id ,
dbo.INV_dropshippers_To_ProductOptions.product_id ,
dbo.Products.Product_ID ,
dbo.Products.ProductTitle ,
dbo.Products.thumbnail ,
dbo.Products.Smimage ,
dbo.INV_dropshippers_To_ProductOptions.sku ,
dbo.Products.Discontinued ,
dbo.Products.CloseOut ,
dbo.Products.Special ,
dbo.Products.Active ,
dbo.Products.location_id ,
dbo.INV_dropshippers_To_ProductOptions.dropshipper_id,
dbo.Products.season ,
dbo.Products.CustomField2 ,
dbo.Products.pt_type ,
dbo.Products.PartNumber ,
dbo.Products.RetailPrice ,
dbo.skupurchasedreport.Product_Id ,
dbo.skupurchasedreport.totalprice_date1 ,
dbo.skupurchasedreport.totalprice_date2 ,
dbo.skupurchasedreport.totalprice_date1 ,
dbo.skupurchasedreport.totalprice_date3 ,
dbo.skupurchasedreport.qtypurchased_date1 ,
dbo.skupurchasedreport.qtypurchased_date2 ,
dbo.skupurchasedreport.qtypurchased_date3 ,
dbo.INV_dropshippers_To_ProductOptions.quantity
答案 0 :(得分:3)
SELECT .. FROM .. INNER JOIN (SELECT ...)...GROUP BY SomeField
答案 1 :(得分:1)
如果您希望每个产品ID获得一个结果,那么您必须在除产品ID之外的所有其他列上获取MAX,然后仅按产品ID进行分组。这可能会导致其他问题,因此您需要了解其后果,更重要的是,您需要了解数据的性质。
答案 2 :(得分:0)
我会尝试解释这项技巧,但会由您来解决问题。
让我们从表开始:
DECLARE @tbl TABLE ( Id int, Color varchar(10) )
INSERT INTO @tbl
( Id, Color )
SELECT 1 ,'BLUE' UNION
SELECT 2 ,'BLUE' UNION
SELECT 3 ,'RED' UNION
SELECT 4 ,'GREEN' UNION
SELECT 5 ,'GREEN' UNION
SELECT 6 ,'GREEN' UNION
SELECT 7 ,'YELLOW' UNION
SELECT 8 ,'YELLOW';
现在我想只列出每种颜色的一个项目(类似于你的问题)
WITH abcd
AS ( SELECT Id
,Color
,ROW_NUMBER() OVER ( PARTITION BY Color ORDER BY Id ) AS rn
FROM @tbl
)
SELECT Id ,Color
FROM abcd
WHERE rn = 1
返回
Id Color
----------- ----------
1 BLUE
4 GREEN
3 RED
7 YELLOW