每月和城市的组数据

时间:2016-02-22 12:02:11

标签: mysql sql database

我已经编写了一个查询来获取此数据,但此数据基于今天至2016年1月22日。

我想以一种方式写它,它应该显示1月份的值,然后是2月份,因为我想知道每月增加多少TotalCredit。

请将数据视为图像

1 个答案:

答案 0 :(得分:0)

SELECT
    City,
    Sum(Violations) AS TotalViolations,
    SUM(USDSpent) AS 'TotalCredit(USD)'
FROM
    (
        SELECT
            city. NAME AS City,
            count(user_credit.booking_id) AS Violations,
            Round(SUM(amount / usd_rate_to), 1) AS USDSpent,
            user_credit.creation_date AS Date
        FROM
            user_credit
        JOIN booking ON user_credit.booking_id = booking.id
        JOIN booking_detail ON booking.id = booking_detail.booking_id
        JOIN drivers ON booking_detail.assigned_driver_id = drivers.id
        JOIN limo_company ON drivers.limo_company_id = limo_company.id
        JOIN city ON limo_company.city_id = city.id
        JOIN customer_car_type cct ON booking.customer_car_type_id = cct.id
        JOIN service_area ON cct.service_area_id = service_area.id
        JOIN currency_exchange ON user_credit.base_currency_exchange_id = currency_exchange.id
        WHERE
            user_credit.creation_date BETWEEN SUBDATE(CURDATE(), INTERVAL 1 MONTH)
        AND NOW()
        GROUP BY
            Date
    ) AS tableA
GROUP BY    City,month(creation_date)