我如何按不同的日期分组,并对组日期的每个数据求和

时间:2017-07-11 15:33:05

标签: php mysql sql

我正在做广告跟踪小组。

我的数据库上有3个表,

1-广告系列

id
orderID
name
startDate
endDate
goal
type
status
unitPrice
total

2-行项目

lineItem_id
id
lineItemOrderID
lineItemName
lineItemStartDate
lineItemEndDate
lineItemStatus
lineItemType
lineItemGoal

3-行项目静态

statics_id
lineItem_id
inventoryID
inventoryName
impression
click
staticsDate

我的表格示例:

line item statics table example (data coming per 10 min)

我想要这样的结果:

this is i want photo

如何按不同日期分组并对每个组日期数据求和

我的示例代码:(它不能正常运行,值为false)

SELECT inventoryNameGI, SUM(impressionGI), SUM(clickGI) 
FROM 
( 
    SELECT DISTINCT inventoryName AS inventoryNameGI, 
           MAX(impression) AS impressionGI, 
           MAX(click) AS clickGI 
    FROM   `line_items_statics` 
    WHERE  `lineItemID` = '5' 
    AND    staticsDate BETWEEN '2017-07-01 00:00:00' 
                           AND '2017-07-03 23:59:59' 
    GROUP BY `inventoryName` 
) AS toplamTablo

1 个答案:

答案 0 :(得分:0)

试试这个 - 在嵌套查询中,使用CONVERT仅将DATETIME转换为DATE

SELECT INVENTORYID, SUM(IMPRESSION), SUM(CLICK), CONVERT(DATE,STATICSDATE)
FROM `line_items_statics` 
WHERE `lineItemID` = '5' 
  AND normalDate BETWEEN '2017-07-01' AND '2017-07-03' 
GROUP BY INVENTORYID,
         CONVERT(DATE,STATICSDATE)