简体   繁体   中英

?Useing COUNT from Table and SUM from anther Table with related data

Table #1 Name: 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

> 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

how i can count how many user will pay me in '2014-07-14' and how much?,

i need result will be for '2014-07-14' Like this

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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