表#1名称:用户
> ID | NAME | offerID | paymentDate
1 | user1 | 1 | 2014-07-14
2 | user2 | 2 | 2014-07-14
3 | user3 | 2 | 2014-07-30
4 | user4 | 1 | 2014-07-14
5 | user5 | 3 | 2014-07-14
6 | user6 | 1 | 2014-07-30
表#2名称:报价
> ID | NAME | PRICE
1 | offer1| 25
2 | offer2| 45
3 | offer3| 75
如果你看到我有(3)用户在offer1(2)用户在offer2(1)用户在offer3
我如何计算有多少用户会付钱给我#2014; 2014-07-14'多少钱?,
我需要的结果将是' 2014-07-14'喜欢这个
> paymentDate | usersCount | Totalprice
2014-07-14 | 4 | 175
答案 0 :(得分:1)
SELECT sum(offer.price) as Totalprice, count(users.id), paymentDate
FROM users JOIN offers ON users.offerID = offers.ID
WHERE paymentDate = '2014-07-14'
GROUP BY paymentDate;
答案 1 :(得分:1)
这个怎么样?
select u.paymentDate, count(*), sum(o.price)
from users u join
offers o
on u.offerId = o.id
where u.date = '2014-07-14'
group by u.paymentDate;
当然,这并不是说他们将支付。只有数据表明他们会这样做。
您可以删除where
子句以获取所有日期的结果。
答案 2 :(得分:1)
SELECT U.PAYMENTDATE, COUNT(*) AS UserCount, SUM( O.PRICE)
FROM USERS AS U
JOIN OFFERS AS O
ON O.ID = U.OfferId
WHERE U.PAYMENTDATE = '2014-07-14'
GROUP BY U.PAYMENTDATE
答案 3 :(得分:0)
Select PaymentDate, Count(Distinct U.Id) usersCount, Sum(O.Price) Totalprice
From Users U
Join Offer O On O.Id = U.OfferId
Where U.PaymentDate = '2014-07-14'
Group By U.PaymentDate