简体   繁体   中英

Sql group by date issue

I have two tables as below,

------------------    --------------------
| leads          |    | leads_tracking    |
------------------    --------------------
| id             |    | tracking_id       |
| lead_id        |    | lead_id           |
| tix            |    | field_name        |
| order_number   |    | date              |
------------------    ---------------------

I need to get the sum of tix and group them by date (and order number is not empty)

I tried write sql:

SELECT DATE_FORMAT(tracking_leads.date, "%m.%d.%Y") as trackDate, SUM(l.tix) as sumValue 
FROM leads as l 
INNER JOIN tracking_leads ON l.lead_id=tracking_leads.lead_id 
WHERE tracking_leads.field_name='tix' 
  AND l.is_active = 1 
  AND l.is_archive = 0 
  AND l.dont_show_in_list=0 
  AND order_number <> '' 
  and transaktions_nr IS NOT NULL
GROUP BY DATE_FORMAT(tracking_leads.date, "%m.%d.%Y")

I don't have all groups that I need.

What's wrong with my code please?

Add order_number to your query and check the result. You might get some idea. Try this:

SELECT order_number,DATE_FORMAT(tracking_leads.date, "%m.%d.%Y") as trackDate, SUM(l.tix) as 
sumValue 

FROM leads as l 
INNER JOIN tracking_leads ON l.lead_id=tracking_leads.lead_id 

WHERE tracking_leads.field_name='tix' 
  AND l.is_active = 1 
  AND l.is_archive = 0 
  AND l.dont_show_in_list=0 
  AND order_number <> '' 
  and transaktions_nr IS NOT NULL

GROUP BY order_number,tracking_leads.date

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