选择语句以从表中的列获取2值

时间:2014-03-08 08:38:37

标签: sql

您好我想从同一个表中的一列中选择2个值,在拍卖表中有一个列调用状态,状态可以是付费或未售出,我想将这两个值作为2列获取,例如 我想获得已支付状态的d的数量以及状态未售出的ID的数量

SELECT Auction.AuctionID AS Auction, 
       DATENAME(mm, Auction.EndDate) AS Month 
  FROM Auction 
 WHERE (Auction.Status = 'Unsold')


SELECT Auction.AuctionID AS Auction, 
       DATENAME(mm, Auction.EndDate) AS Month 
  FROM Auction 
 WHERE (Auction.Status = 'Paid')

2 个答案:

答案 0 :(得分:1)

使用case

SELECT case when Status = 'Unsold' then AuctionID end AS Auction_Unsold, 
       case when Status = 'Paid' then AuctionID end AS Auction_Paid, 
FROM Auction 
WHERE Status IN ('Unsold','Paid')

答案 1 :(得分:0)

从您的评论到your other question,您似乎需要两个子查询:

SELECT unsold.total, paid.total 
FROM (
  SELECT COUNT(Auction.AuctionID) AS total
  FROM Auction 
  INNER JOIN Item ON Auction.ItemID = Item.ItemID 
  WHERE (Auction.Status = 'UnSold') AND (Item.SellerID = 201)
  GROUP BY Auction.Status
) AS unsold,
(
  SELECT COUNT(BID.BidID) AS total
  FROM BID
  WHERE (BID.Status = 'Paid')
  GROUP BY BID.Status
) AS paid