您好我想选择,每个与出价ID为'赢'的出价ID相关联的拍卖ID,以及每个拍卖ID,状态为'已过期'作为我声明中的两个单独列,有人可以帮忙,第一列将被叫赢,第二列将过期
SELECT
Auction.AuctionID,
DATENAME(mm, BID.Date) AS Month
FROM
BID INNER JOIN Auction ON BID.AuctionID = Auction.AuctionID
WHERE
BID.Status = 'Won'
ORDER BY Month
期望的结果:
AuctionID Month Won Expired
----------- ------------------------------ ---- -------
1 January 32 22
2 March 10 22
3 April 0 2
答案 0 :(得分:4)
你可以试试这样的东西吗,这不完全是一个概述
***
SELECT
Auction.AuctionID,
DATENAME(mm, b1.Date) AS Month,
COUNT(*) AS `Won`,
(SELECT count(*) from BID b2 WHERE DATENAME(mm, b1.Date) = DATENAME(mm, b2.Date))-COUNT(*) AS `EXPIRED`
FROM
BID b1
INNER JOIN Auction ON BID.AuctionID = Auction.AuctionID
WHERE
BID.Status = 'Won'
GROUP BY Month
***
答案 1 :(得分:0)
拍卖不能同时赢得并过期,因此您不需要例如:AuctionId 1:won:120214,过期050214?
好吧,让我们想象一下:
with p as (
SELECT Auction.AuctionID, DATENAME(mm, BID.Date) AS Month
FROM BID
INNER JOIN Auction ON BID.AuctionID = Auction.AuctionID)
select AuctionId, max(Won), Max(Expired)
from
(
select AuctionId, Month as Won, null as Expired from p
WHERE (BID.Status = 'Won')
union
select AuctionId, null, Month from p
WHERE (BID.Status = 'Expired')
) z
ORDER BY Won, Expired
答案 2 :(得分:0)
如果你想要的结果是这样的:
AuctionID Month Won Expired
----------- ------------------------------ ---- -------
1 January Won NULL
2 January Won NULL
3 January NULL Expired
然后你可以使用这个查询:
SELECT
A.AuctionID,
DATENAME(mm, B.Date) AS Month,
CASE Status WHEN 'Won' THEN 'Won' ELSE NULL END AS Won,
CASE Status WHEN 'Expired' THEN 'Expired' ELSE NULL END AS Expired
FROM BID b
INNER JOIN Auction a ON B.AuctionID = A.AuctionID
ORDER BY Month, AuctionID
但如果你想要的结果是这样的:
AuctionID Month Status
----------- ------------------------------ --------------------
1 January Won
2 February Won
3 January Expired
然后这个查询会:
SELECT
A.AuctionID,
DATENAME(mm, B.Date) AS Month,
Status
FROM BID b
INNER JOIN Auction a ON B.AuctionID = A.AuctionID
ORDER BY Status Desc, Month DESC, AuctionID
第三次尝试。为了得到这个:
Month Won Expired
------------------------------ ----------- -----------
January 1 1
February 1 0
使用此查询:
SELECT
DATENAME(mm, B.Date) AS Month,
SUM(CASE WHEN Status = 'Won' THEN 1 ELSE 0 END) AS Won,
SUM(CASE WHEN Status = 'Expired' THEN 1 ELSE 0 END) AS Expired
FROM BID b
INNER JOIN Auction a ON B.AuctionID = A.AuctionID
GROUP BY DATENAME(mm, B.Date), B.Date
ORDER BY b.Date
答案 3 :(得分:-1)
试试这个..
SELECT
Auction.AuctionID,
DATENAME(mm, BID.Date) AS Month,
CASE status
WHEN Won THEN Status As Won
WHEN Expired THEN Status As Expired
END
FROM
BID INNER JOIN Auction ON BID.AuctionID = Auction.AuctionID
ORDER BY Month