简体   繁体   English

将表中的COUNT和花药表中的SUM与相关数据一起使用

[英]?Useing COUNT from Table and SUM from anther Table with related data

Table #1 Name: users 表#1名称:users

> 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

Table #2 Name: offer 表#2名称:要约

> ID | NAME  |  PRICE
  1  | offer1|  25
  2  | offer2|  45
  3  | offer3|  75

if you see i have (3) users in offer1 (2)users in offer2 (1)users in offer3 如果您看到我有(3)个要约1中的用户(2)个要约2中的用户(1)个要约3中的用户

how i can count how many user will pay me in '2014-07-14' and how much?, 我如何计算“ 2014-07-14”中有多少用户向我付款以及多少?,

i need result will be for '2014-07-14' Like this 我需要结果将是“ 2014-07-14”这样的结果

> paymentDate  |  usersCount  |  Totalprice
  2014-07-14   |  4           |  175
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;

How about this? 这个怎么样?

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;

Of course, this doesn't say that they will pay. 当然,这并不是说他们付款。 Only that the data says they will. 只是数据表明他们愿意。

You can remove the where clause to get results for all dates. 您可以删除where子句以获取所有日期的结果。

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
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM