Sql select语句从同一个表中选择4个值

时间:2014-02-24 22:05:26

标签: asp.net sql-server

大家好我已经创建了两个select语句,但我想将它们合并在一起,给出一个只显示4条记录的select语句,有人可以帮忙吗?

SELECT 
    DATENAME(month, BID.Date) AS Years, 
    COUNT(DATENAME(Month, BID.Date)) AS Total 
FROM 
    Auction 
INNER JOIN 
    BID ON Auction.AuctionID = BID.AuctionID 
INNER JOIN 
    Item ON Auction.ItemID = Item.ItemID 
WHERE 
    (Auction.Status = 'Expired') 
    AND (Item.SellerID = @seller) 
    AND (BID.Status = 'Won') 
GROUP BY 
    DATENAME(month, BID.Date)

SELECT 
    DATENAME(month, BID.Date) AS Years1, 
    COUNT(DATENAME(Month, BID.Date)) AS Total1 
FROM 
    Auction 
INNER JOIN 
    BID ON Auction.AuctionID = BID.AuctionID 
INNER JOIN 
    Item ON Auction.ItemID = Item.ItemID 
WHERE 
    (Auction.Status = 'UnSold') 
    AND (Item.SellerID = @seller)  
GROUP BY 
    DATENAME(month, BID.Date)

1 个答案:

答案 0 :(得分:0)

SELECT 
    DATENAME(month, BID.Date) AS Years 
   ,COUNT(CASE WHEN Auction.Status = 'Expired' 
                   AND  Item.SellerID = @seller 
                   AND  BID.Status = 'Won'
               THEN  DATENAME(Month, BID.Date) ELSE NULL END) AS Total1
   ,COUNT(CASE WHEN Auction.Status = 'UnSold'
                   AND Item.SellerID = @seller
               THEN DATENAME(Month, BID.Date) ELSE NULL END)  AS  Total2

FROM       Auction   INNER JOIN  BID 
ON         Auction.AuctionID = BID.AuctionID 
INNER JOIN Item 
ON         Auction.ItemID = Item.ItemID 
GROUP BY   DATENAME(month, BID.Date)