WITH SOS_BASE AS
(
    SELECT
        sm.CountryName,
        sc.MID,
        sm.RegionName,
        sm.StateName,
        sm.CityName,
        Em.SupervisorName,
        em.EmpId,
        Em.EmpName AS EmployeeName,
        Em.DesignationName AS Designation,
        sm.StoreId,
        CONVERT(varchar, sc.VisitDate, 101) AS VisitDate,
        sm.StoreCode,
        sm.StoreName,
        sm.Address,
        sm.StoreTypeid,
        sm.ChannelId,
        sm.ChainName,

        MSD.SOSDefinitionName,

        CASE
            WHEN ISNULL(TS.SOSHeaderTable,'')='Master_Category' THEN 'Category'
            WHEN ISNULL(TS.SOSHeaderTable,'')='Master_SubCategory' THEN 'SubCategory'
            WHEN ISNULL(TS.SOSHeaderTable,'')='Master_Brand' THEN 'Brand'
            WHEN ISNULL(TS.SOSHeaderTable,'')='Master_SubBrand' THEN 'SubBrand'
        END AS SOSHeaderDeatils,

        TS.SOSHeaderName,
        TS.SOSHeaderValue AS SOSHeaderID,

        'Header_Image' AS HDR1,

        CASE
            WHEN ISNULL(TSC.SOSChildTable,'')='Master_Category' THEN 'Category'
            WHEN ISNULL(TSC.SOSChildTable,'')='Master_SubCategory' THEN 'SubCategory'
            WHEN ISNULL(TSC.SOSChildTable,'')='Master_Brand' THEN 'Brand'
            WHEN ISNULL(TSC.SOSChildTable,'')='Master_SubBrand' THEN 'SubBrand'
        END AS SOSChildDeatils,

        TSC.SOSChildName,
        TSC.SOSChildValue AS SOSChildID,
        TSC.ChildTotalFacing,
        TS.SOSHeaderFacing,
        TSC.ChildSelfFacing,

        (
            SELECT TOP 1 SOSTarget
            FROM OneApp_KelloggsMT.dbo.Mapping_StoreShareOfShelfTarget a
            WHERE a.SOSDefinitionId = MSD.SOSDefinitionId
              AND a.StoreId = sm.StoreId
              AND a.FromDate <= sc.VisitDate
              AND a.ToDate >= sc.VisitDate
        ) AS SOSTarget,

        CASE
            WHEN ISNULL(SHI.SOSHeaderImage,'') = ''
                THEN ''
            ELSE
                'https://kimt1.parinaam.in/Upload/SOSImages/'
                + SHI.SOSHeaderImage
        END AS SOSHeaderImg

    FROM OneApp_KelloggsMT.dbo.T_ShareOfShelfHeader ts

    INNER JOIN OneApp_KelloggsMT.dbo.T_StoreCoverage sc
        ON ts.MID = sc.MID

    INNER JOIN OneApp_KelloggsMT.dbo.vw_StoreDetail sm
        ON sc.StoreId = sm.StoreId

    INNER JOIN OneApp_KelloggsMT.dbo.vw_Employee_Detail Em
        ON sc.EmpId = Em.EmpId

    INNER JOIN OneApp_KelloggsMT.dbo.T_ShareOfShelfChild tsc
        ON ts.SOSId = tsc.SOSId

    INNER JOIN OneApp_KelloggsMT.dbo.Master_ShareOfShelfDefinition msd
        ON msd.SOSDefinitionId = tsc.SOSDefinitionId

    LEFT JOIN OneApp_KelloggsMT.dbo.T_ShareOfShelfHeaderImages SHI
        ON ts.SOSId = SHI.SOSId
       AND ts.SOSHeaderValue = SHI.SOSHeaderValue

    LEFT JOIN OneApp_KelloggsMT.dbo.T_ShareOfShelfChildImages SCI
        ON tsc.SOSId = SCI.SOSId
       AND tsc.SOSChildValue = SCI.SOSChildValue

    WHERE Em.EmpName NOT LIKE 'test%'
      AND sc.MID IN ({mid_list})
),

SOS_PIVOT AS
(
    SELECT *
    FROM SOS_BASE
    PIVOT
    (
        MIN(SOSHeaderImg)
        FOR HDR1 IN ([Header_Image])
    ) pvt
)

SELECT
    '40148' AS ProjectId,
    MID,
    EmpId AS employee_id,
    StoreId AS store_id,
    VisitDate AS visit_date,
    StoreTypeid AS storetype_id,
    ChannelId AS channel_id,
    SOSDefinitionName,
    SOSHeaderDeatils,
    SOSHeaderName,
    SOSHeaderID,
    SOSChildDeatils,
    SOSChildName,
    SOSChildID,
    SOSHeaderFacing,
    ChildTotalFacing,
    ChildSelfFacing,
    SOSTarget,
    GETDATE() AS update_date,
    'SP-Pius' AS update_by

FROM SOS_PIVOT

GROUP BY
    CountryName,
    MID,
    RegionName,
    StateName,
    CityName,
    SupervisorName,
    EmpId,
    EmployeeName,
    Designation,
    StoreId,
    VisitDate,
    StoreCode,
    StoreName,
    Address,
    StoreTypeid,
    ChannelId,
    ChainName,
    SOSDefinitionName,
    SOSHeaderDeatils,
    SOSHeaderName,
    SOSHeaderID,
    SOSChildDeatils,
    SOSChildName,
    SOSChildID,
    ChildTotalFacing,
    SOSHeaderFacing,
    ChildSelfFacing,
    SOSTarget

ORDER BY
    RegionName,
    StateName,
    CityName,
    VisitDate;