组合两个select语句时遇到问题

时间:2014-04-02 18:49:14

标签: sql

我想加入这两个查询,以便将数据一起呈现,但我不知道如何正确连接两个select语句。

以下是我尝试的方式:

Select *
FROM    Products.Products
Join
SELECT Products.Prices.ProductID, 
    Max(IIf(Products.Prices.Code='ONEK',Products.Prices.Price,Null)) AS ONEK, 
    Max(IIf(Products.Prices.Code='FIVEK',Products.Prices.Price,Null)) AS FIVEK, 
    Max(IIf(Products.Prices.Code='TENK',Products.Prices.Price,Null)) AS TENK, 
    Max(IIf(Products.Prices.Code='TWENTYFIVEK',Products.Prices.Price,Null)) AS  TWENTYFIVEK,
    Max(IIf(Products.Prices.Code='Fifty',Products.Prices.Price,Null)) AS Fifty, 
    Max(IIf(Products.Prices.Code='OneHundred',Products.Prices.Price,Null)) AS OneHundred, 
    Max(IIf(Products.Prices.Code='FiveHundred',Products.Prices.Price,Null)) AS FiveHundred
FROM Products.Prices
GROUP BY Products.Prices.ProductID;

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

将一个查询作为子查询并定义连接列:

Select *
FROM    Products.Products
Join
(SELECT Products.Prices.ProductID, 
    Max(IIf(Products.Prices.Code='ONEK',Products.Prices.Price,Null)) AS ONEK, 
    Max(IIf(Products.Prices.Code='FIVEK',Products.Prices.Price,Null)) AS FIVEK, 
    Max(IIf(Products.Prices.Code='TENK',Products.Prices.Price,Null)) AS TENK, 
    Max(IIf(Products.Prices.Code='TWENTYFIVEK',Products.Prices.Price,Null)) AS  TWENTYFIVEK,
    Max(IIf(Products.Prices.Code='Fifty',Products.Prices.Price,Null)) AS Fifty, 
    Max(IIf(Products.Prices.Code='OneHundred',Products.Prices.Price,Null)) AS OneHundred, 
    Max(IIf(Products.Prices.Code='FiveHundred',Products.Prices.Price,Null)) AS FiveHundred
FROM Products.Prices
GROUP BY Products.Prices.ProductID
) A
ON Products.ProductID = A.ProductID;