technical skills grow

Responsive Ads Here

Wednesday, January 13, 2021

Pivot the table in mysql using case

 Table without Pivot :


  SELECT
    STATION_V1.ID,
    STATION_V1.STATE,
    STATION_V1.DISTRICT,
    STATION_V1.TEHSIL,
    STATION_V1.STATION_NAME,
    T2.*
FROM
    STATION_V1
        LEFT JOIN
    (SELECT
        IMEI_ID,
            SUM(CASE
                WHEN a.DATADATE = '2021-01-01' THEN a.data_packet_count
            END) '2021-01-01',
            SUM(CASE
                WHEN a.DATADATE = '2021-01-02' THEN a.data_packet_count
            END) '2021-01-02',
            SUM(CASE
                WHEN a.DATADATE = '2021-01-03' THEN a.data_packet_count
            END) '2021-01-03',
            SUM(CASE
                WHEN a.DATADATE = '2021-01-04' THEN a.data_packet_count
            END) '2021-01-04',
            SUM(CASE
                WHEN a.DATADATE = '2021-01-05' THEN a.data_packet_count
            END) '2021-01-05',
            SUM(CASE
                WHEN a.DATADATE = '2021-01-06' THEN a.data_packet_count
            END) '2021-01-06',
            SUM(CASE
                WHEN a.DATADATE = '2021-01-07' THEN a.data_packet_count
            END) '2021-01-07',
            SUM(CASE
                WHEN a.DATADATE = '2021-01-08' THEN a.data_packet_count
            END) '2021-01-08',
            SUM(CASE
                WHEN a.DATADATE = '2021-01-09' THEN a.data_packet_count
            END) '2021-01-09',
            SUM(CASE
                WHEN a.DATADATE = '2021-01-10' THEN a.data_packet_count
            END) '2021-01-10'
    FROM
        (SELECT
        IMEI_ID,
            DATE(DATA_DATE) DATADATE,
            COUNT(DATA_DATE) data_packet_count
    FROM
        STATION_DATA_V1
    WHERE
        DATA_DATE >= '2021-01-01 00:00:00'
            AND DATA_DATE < '2021-01-10 23:50:50'
            AND IS_INTERPOLATED = 0
            AND IS_UPLOADED = 0
    GROUP BY IMEI_ID , DATADATE) a
    GROUP BY IMEI_ID) T2 ON STATION_V1.ID = T2.IMEI_ID
WHERE
    STATION_V1.ID IN (SELECT
            ID
        FROM
            STATION_V1
        WHERE
            DISTRICT IN ('A' , 'B', 'J', 'L', 'S')
                AND ACTIVE = 1)

After Pivot Table :


 

No comments:

Post a Comment

Powered by Blogger.

Labels

Contact Form

Name

Email *

Message *

Search This Blog

Blog Archive

Ad Code

Responsive Advertisement

Recent Posts