Skip to main content
Removed unused options
Source Link
Paul White
  • 90.3k
  • 30
  • 424
  • 663

###Option 1

Using the deprecated GROUP BY ALL syntax:

SELECT 
    AODAR.fkSiteID,
    NumChecks =
        ISNULL
        (
            SUM(AODAR.SearchTypePerson) + 
            SUM(AODAR.SearchTypeLocker) + 
            SUM(AODAR.SearchTypeSpotRandom) + 
            SUM(AODAR.SearchTypePersVehicle) + 
            SUM(AODAR.SearchTypeVisitorContractorVehicle) + 
            SUM(AODAR.SearchTypeCompanyVehicle) + 
            SUM(AODAR.SearchTypeToilet) + 
            SUM(AODAR.PatrolExternal) + 
            SUM(AODAR.PatrolCarPark) + 
            SUM(AODAR.PatrolPerimeter) + 
            SUM(AODAR.PatrolInternal) + 
            SUM(AODAR.VehicleCheckAmbientLine) + 
            SUM(AODAR.VehicleCheckFridgeLine) + 
            SUM(AODAR.VehicleCheckSealChecks) + 
            SUM(AODAR.OtherChecksIDCards) + 
            SUM(AODAR.OtherChecksIncidentReports) + 
            SUM(AODAR.OtherChecksColdStoreChecks)
            , 0
        )
FROM dbo.AAHOfficerDailyActivityReport AS AODAR
WHERE 
    AODAR.MonthOfReport = 6 
    AND AODAR.RecordIsDeletedYN = 0
GROUP BY ALL 
    AODAR.fkSiteID
HAVING 
    AODAR.fkSiteID IN (945,947,948,949,950,951,952)
ORDER BY
    AODAR.fkSiteID;

###Option 2

Or equivalently, withoutThe following code produces the deprecated syntax:

SELECT 
    AODAR.fkSiteID,
    NumChecks =
        SUM(AODAR.SearchTypePerson) + 
        SUM(AODAR.SearchTypeLocker) + 
        SUM(AODAR.SearchTypeSpotRandom) + 
        SUM(AODAR.SearchTypePersVehicle) + 
        SUM(AODAR.SearchTypeVisitorContractorVehicle) + 
        SUM(AODAR.SearchTypeCompanyVehicle) + 
        SUM(AODAR.SearchTypeToilet) + 
        SUM(AODAR.PatrolExternal) + 
        SUM(AODAR.PatrolCarPark) + 
        SUM(AODAR.PatrolPerimeter) + 
        SUM(AODAR.PatrolInternal) + 
        SUM(AODAR.VehicleCheckAmbientLine) + 
        SUM(AODAR.VehicleCheckFridgeLine) + 
        SUM(AODAR.VehicleCheckSealChecks) + 
        SUM(AODAR.OtherChecksIDCards) + 
        SUM(AODAR.OtherChecksIncidentReports) + 
        SUM(AODAR.OtherChecksColdStoreChecks)
FROM
(
    SELECT
        AODAR.fkSiteID,
        AODAR.SearchTypePerson,
        AODAR.SearchTypeLocker,
        AODAR.SearchTypeSpotRandom,
        AODAR.SearchTypePersVehicle,
        AODAR.SearchTypeVisitorContractorVehicle,
        AODAR.SearchTypeCompanyVehicle,
        AODAR.SearchTypeToilet,
        AODAR.PatrolExternal,
        AODAR.PatrolCarPark,
        AODAR.PatrolPerimeter,
        AODAR.PatrolInternal,
        AODAR.VehicleCheckAmbientLine,
        AODAR.VehicleCheckFridgeLine,
        AODAR.VehicleCheckSealChecks,
        AODAR.OtherChecksIDCards,
        AODAR.OtherChecksIncidentReports,
        AODAR.OtherChecksColdStoreChecks
    FROM dbo.AAHOfficerDailyActivityReport AS AODAR
    WHERE 
        AODAR.MonthOfReport = 6 
        AND AODAR.RecordIsDeletedYN = 0
        AND AODAR.fkSiteID IN (945,947,948,949,950,951,952)

    UNION ALL

    SELECT
        AODAR.fkSiteID,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    FROM dbo.AAHOfficerDailyActivityReport AS AODAR
    WHERE 
        AODAR.fkSiteID IN (945,947,948,949,950,951,952)
) AS AODAR
GROUP BY
    AODAR.fkSiteID
ORDER BY
    AODAR.fkSiteID;

Results:

╔══════════╦═══════════╗
║ fkSiteID ║ NumChecks ║
╠══════════╬═══════════╣
║      945 ║       228 ║
║      947 ║        27 ║
║      948 ║         0 ║
║      949 ║        58 ║
║      951 ║        67 ║
║      952 ║      1015 ║
╚══════════╩═══════════╝

db<>fiddle demo

###OPtion 3

Or, ifresults you really intended 950 to be inwant by outer joining the results even though it does notlist of sites that must appear in the output, with the results of your data set:query grouped by site id.

SELECT
    Sites.fkSiteID,
    NumChecks = ISNULL(Totals.NumChecks, 0)
FROM 
 (
    VALUES
        (945),
        (947),
        (948),
        (949),
        (950),
        (951),
        (952)
 ) AS Sites (fkSiteID)
LEFT JOIN
(
    SELECT
        AODAR.fkSiteID,
        NumChecks =
            SUM(AODAR.SearchTypePerson) + 
            SUM(AODAR.SearchTypeLocker) + 
            SUM(AODAR.SearchTypeSpotRandom) + 
            SUM(AODAR.SearchTypePersVehicle) + 
            SUM(AODAR.SearchTypeVisitorContractorVehicle) + 
            SUM(AODAR.SearchTypeCompanyVehicle) + 
            SUM(AODAR.SearchTypeToilet) + 
            SUM(AODAR.PatrolExternal) + 
            SUM(AODAR.PatrolCarPark) + 
            SUM(AODAR.PatrolPerimeter) + 
            SUM(AODAR.PatrolInternal) + 
            SUM(AODAR.VehicleCheckAmbientLine) + 
            SUM(AODAR.VehicleCheckFridgeLine) + 
            SUM(AODAR.VehicleCheckSealChecks) + 
            SUM(AODAR.OtherChecksIDCards) + 
            SUM(AODAR.OtherChecksIncidentReports) + 
            SUM(AODAR.OtherChecksColdStoreChecks)
    FROM dbo.AAHOfficerDailyActivityReport AS AODAR
    WHERE
        AODAR.MonthOfReport = 6 
        AND AODAR.RecordIsDeletedYN = 0
    GROUP BY 
        AODAR.fkSiteID
) AS Totals
    ON Totals.fkSiteID = Sites.fkSiteID
ORDER BY
    Sites.fkSiteID;

###Option 1

Using the deprecated GROUP BY ALL syntax:

SELECT 
    AODAR.fkSiteID,
    NumChecks =
        ISNULL
        (
            SUM(AODAR.SearchTypePerson) + 
            SUM(AODAR.SearchTypeLocker) + 
            SUM(AODAR.SearchTypeSpotRandom) + 
            SUM(AODAR.SearchTypePersVehicle) + 
            SUM(AODAR.SearchTypeVisitorContractorVehicle) + 
            SUM(AODAR.SearchTypeCompanyVehicle) + 
            SUM(AODAR.SearchTypeToilet) + 
            SUM(AODAR.PatrolExternal) + 
            SUM(AODAR.PatrolCarPark) + 
            SUM(AODAR.PatrolPerimeter) + 
            SUM(AODAR.PatrolInternal) + 
            SUM(AODAR.VehicleCheckAmbientLine) + 
            SUM(AODAR.VehicleCheckFridgeLine) + 
            SUM(AODAR.VehicleCheckSealChecks) + 
            SUM(AODAR.OtherChecksIDCards) + 
            SUM(AODAR.OtherChecksIncidentReports) + 
            SUM(AODAR.OtherChecksColdStoreChecks)
            , 0
        )
FROM dbo.AAHOfficerDailyActivityReport AS AODAR
WHERE 
    AODAR.MonthOfReport = 6 
    AND AODAR.RecordIsDeletedYN = 0
GROUP BY ALL 
    AODAR.fkSiteID
HAVING 
    AODAR.fkSiteID IN (945,947,948,949,950,951,952)
ORDER BY
    AODAR.fkSiteID;

###Option 2

Or equivalently, without the deprecated syntax:

SELECT 
    AODAR.fkSiteID,
    NumChecks =
        SUM(AODAR.SearchTypePerson) + 
        SUM(AODAR.SearchTypeLocker) + 
        SUM(AODAR.SearchTypeSpotRandom) + 
        SUM(AODAR.SearchTypePersVehicle) + 
        SUM(AODAR.SearchTypeVisitorContractorVehicle) + 
        SUM(AODAR.SearchTypeCompanyVehicle) + 
        SUM(AODAR.SearchTypeToilet) + 
        SUM(AODAR.PatrolExternal) + 
        SUM(AODAR.PatrolCarPark) + 
        SUM(AODAR.PatrolPerimeter) + 
        SUM(AODAR.PatrolInternal) + 
        SUM(AODAR.VehicleCheckAmbientLine) + 
        SUM(AODAR.VehicleCheckFridgeLine) + 
        SUM(AODAR.VehicleCheckSealChecks) + 
        SUM(AODAR.OtherChecksIDCards) + 
        SUM(AODAR.OtherChecksIncidentReports) + 
        SUM(AODAR.OtherChecksColdStoreChecks)
FROM
(
    SELECT
        AODAR.fkSiteID,
        AODAR.SearchTypePerson,
        AODAR.SearchTypeLocker,
        AODAR.SearchTypeSpotRandom,
        AODAR.SearchTypePersVehicle,
        AODAR.SearchTypeVisitorContractorVehicle,
        AODAR.SearchTypeCompanyVehicle,
        AODAR.SearchTypeToilet,
        AODAR.PatrolExternal,
        AODAR.PatrolCarPark,
        AODAR.PatrolPerimeter,
        AODAR.PatrolInternal,
        AODAR.VehicleCheckAmbientLine,
        AODAR.VehicleCheckFridgeLine,
        AODAR.VehicleCheckSealChecks,
        AODAR.OtherChecksIDCards,
        AODAR.OtherChecksIncidentReports,
        AODAR.OtherChecksColdStoreChecks
    FROM dbo.AAHOfficerDailyActivityReport AS AODAR
    WHERE 
        AODAR.MonthOfReport = 6 
        AND AODAR.RecordIsDeletedYN = 0
        AND AODAR.fkSiteID IN (945,947,948,949,950,951,952)

    UNION ALL

    SELECT
        AODAR.fkSiteID,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    FROM dbo.AAHOfficerDailyActivityReport AS AODAR
    WHERE 
        AODAR.fkSiteID IN (945,947,948,949,950,951,952)
) AS AODAR
GROUP BY
    AODAR.fkSiteID
ORDER BY
    AODAR.fkSiteID;

Results:

╔══════════╦═══════════╗
║ fkSiteID ║ NumChecks ║
╠══════════╬═══════════╣
║      945 ║       228 ║
║      947 ║        27 ║
║      948 ║         0 ║
║      949 ║        58 ║
║      951 ║        67 ║
║      952 ║      1015 ║
╚══════════╩═══════════╝

db<>fiddle demo

###OPtion 3

Or, if you really intended 950 to be in the results even though it does not appear in the data set:

SELECT
    Sites.fkSiteID,
    NumChecks = ISNULL(Totals.NumChecks, 0)
FROM 
 (
    VALUES
        (945),
        (947),
        (948),
        (949),
        (950),
        (951),
        (952)
 ) AS Sites (fkSiteID)
LEFT JOIN
(
    SELECT
        AODAR.fkSiteID,
        NumChecks =
            SUM(AODAR.SearchTypePerson) + 
            SUM(AODAR.SearchTypeLocker) + 
            SUM(AODAR.SearchTypeSpotRandom) + 
            SUM(AODAR.SearchTypePersVehicle) + 
            SUM(AODAR.SearchTypeVisitorContractorVehicle) + 
            SUM(AODAR.SearchTypeCompanyVehicle) + 
            SUM(AODAR.SearchTypeToilet) + 
            SUM(AODAR.PatrolExternal) + 
            SUM(AODAR.PatrolCarPark) + 
            SUM(AODAR.PatrolPerimeter) + 
            SUM(AODAR.PatrolInternal) + 
            SUM(AODAR.VehicleCheckAmbientLine) + 
            SUM(AODAR.VehicleCheckFridgeLine) + 
            SUM(AODAR.VehicleCheckSealChecks) + 
            SUM(AODAR.OtherChecksIDCards) + 
            SUM(AODAR.OtherChecksIncidentReports) + 
            SUM(AODAR.OtherChecksColdStoreChecks)
    FROM dbo.AAHOfficerDailyActivityReport AS AODAR
    WHERE
        AODAR.MonthOfReport = 6 
        AND AODAR.RecordIsDeletedYN = 0
    GROUP BY 
        AODAR.fkSiteID
) AS Totals
    ON Totals.fkSiteID = Sites.fkSiteID
ORDER BY
    Sites.fkSiteID;

The following code produces the results you want by outer joining the list of sites that must appear in the output, with the results of your data query grouped by site id.

SELECT
    Sites.fkSiteID,
    NumChecks = ISNULL(Totals.NumChecks, 0)
FROM (VALUES (945),(947),(948),(949),(950),(951),(952)) AS Sites (fkSiteID)
LEFT JOIN
(
    SELECT
        AODAR.fkSiteID,
        NumChecks =
            SUM(AODAR.SearchTypePerson) + 
            SUM(AODAR.SearchTypeLocker) + 
            SUM(AODAR.SearchTypeSpotRandom) + 
            SUM(AODAR.SearchTypePersVehicle) + 
            SUM(AODAR.SearchTypeVisitorContractorVehicle) + 
            SUM(AODAR.SearchTypeCompanyVehicle) + 
            SUM(AODAR.SearchTypeToilet) + 
            SUM(AODAR.PatrolExternal) + 
            SUM(AODAR.PatrolCarPark) + 
            SUM(AODAR.PatrolPerimeter) + 
            SUM(AODAR.PatrolInternal) + 
            SUM(AODAR.VehicleCheckAmbientLine) + 
            SUM(AODAR.VehicleCheckFridgeLine) + 
            SUM(AODAR.VehicleCheckSealChecks) + 
            SUM(AODAR.OtherChecksIDCards) + 
            SUM(AODAR.OtherChecksIncidentReports) + 
            SUM(AODAR.OtherChecksColdStoreChecks)
    FROM dbo.AAHOfficerDailyActivityReport AS AODAR
    WHERE
        AODAR.MonthOfReport = 6 
        AND AODAR.RecordIsDeletedYN = 0
    GROUP BY 
        AODAR.fkSiteID
) AS Totals
    ON Totals.fkSiteID = Sites.fkSiteID
ORDER BY
    Sites.fkSiteID;
added 76 characters in body
Source Link
Paul White
  • 90.3k
  • 30
  • 424
  • 663

###Option 1

Or equivalently:###Option 2

Or equivalently, without the deprecated syntax:

###OPtion 3

Or equivalently:

###Option 1

###Option 2

Or equivalently, without the deprecated syntax:

###OPtion 3

Added 950
Source Link
Paul White
  • 90.3k
  • 30
  • 424
  • 663

Or, if you really intended 950 to be in the results even though it does not appear in the data set:

SELECT
    Sites.fkSiteID,
    NumChecks = ISNULL(Totals.NumChecks, 0)
FROM 
(
    VALUES
        (945),
        (947),
        (948),
        (949),
        (950),
        (951),
        (952)
) AS Sites (fkSiteID)
LEFT JOIN
(
    SELECT
        AODAR.fkSiteID,
        NumChecks =
            SUM(AODAR.SearchTypePerson) + 
            SUM(AODAR.SearchTypeLocker) + 
            SUM(AODAR.SearchTypeSpotRandom) + 
            SUM(AODAR.SearchTypePersVehicle) + 
            SUM(AODAR.SearchTypeVisitorContractorVehicle) + 
            SUM(AODAR.SearchTypeCompanyVehicle) + 
            SUM(AODAR.SearchTypeToilet) + 
            SUM(AODAR.PatrolExternal) + 
            SUM(AODAR.PatrolCarPark) + 
            SUM(AODAR.PatrolPerimeter) + 
            SUM(AODAR.PatrolInternal) + 
            SUM(AODAR.VehicleCheckAmbientLine) + 
            SUM(AODAR.VehicleCheckFridgeLine) + 
            SUM(AODAR.VehicleCheckSealChecks) + 
            SUM(AODAR.OtherChecksIDCards) + 
            SUM(AODAR.OtherChecksIncidentReports) + 
            SUM(AODAR.OtherChecksColdStoreChecks)
    FROM dbo.AAHOfficerDailyActivityReport AS AODAR
    WHERE
        AODAR.MonthOfReport = 6 
        AND AODAR.RecordIsDeletedYN = 0
    GROUP BY 
        AODAR.fkSiteID
) AS Totals
    ON Totals.fkSiteID = Sites.fkSiteID
ORDER BY
    Sites.fkSiteID;

Results:

╔══════════╦═══════════╗
║ fkSiteID ║ NumChecks ║
╠══════════╬═══════════╣
║      945 ║       228 ║
║      947 ║        27 ║
║      948 ║         0 ║
║      949 ║        58 ║
║      950 ║         0 ║
║      951 ║        67 ║
║      952 ║      1015 ║
╚══════════╩═══════════╝

db<>fiddle demo

Or, if you really intended 950 to be in the results even though it does not appear in the data set:

SELECT
    Sites.fkSiteID,
    NumChecks = ISNULL(Totals.NumChecks, 0)
FROM 
(
    VALUES
        (945),
        (947),
        (948),
        (949),
        (950),
        (951),
        (952)
) AS Sites (fkSiteID)
LEFT JOIN
(
    SELECT
        AODAR.fkSiteID,
        NumChecks =
            SUM(AODAR.SearchTypePerson) + 
            SUM(AODAR.SearchTypeLocker) + 
            SUM(AODAR.SearchTypeSpotRandom) + 
            SUM(AODAR.SearchTypePersVehicle) + 
            SUM(AODAR.SearchTypeVisitorContractorVehicle) + 
            SUM(AODAR.SearchTypeCompanyVehicle) + 
            SUM(AODAR.SearchTypeToilet) + 
            SUM(AODAR.PatrolExternal) + 
            SUM(AODAR.PatrolCarPark) + 
            SUM(AODAR.PatrolPerimeter) + 
            SUM(AODAR.PatrolInternal) + 
            SUM(AODAR.VehicleCheckAmbientLine) + 
            SUM(AODAR.VehicleCheckFridgeLine) + 
            SUM(AODAR.VehicleCheckSealChecks) + 
            SUM(AODAR.OtherChecksIDCards) + 
            SUM(AODAR.OtherChecksIncidentReports) + 
            SUM(AODAR.OtherChecksColdStoreChecks)
    FROM dbo.AAHOfficerDailyActivityReport AS AODAR
    WHERE
        AODAR.MonthOfReport = 6 
        AND AODAR.RecordIsDeletedYN = 0
    GROUP BY 
        AODAR.fkSiteID
) AS Totals
    ON Totals.fkSiteID = Sites.fkSiteID
ORDER BY
    Sites.fkSiteID;

Results:

╔══════════╦═══════════╗
║ fkSiteID ║ NumChecks ║
╠══════════╬═══════════╣
║      945 ║       228 ║
║      947 ║        27 ║
║      948 ║         0 ║
║      949 ║        58 ║
║      950 ║         0 ║
║      951 ║        67 ║
║      952 ║      1015 ║
╚══════════╩═══════════╝

db<>fiddle demo

Added non-deprecated syntax version
Source Link
Paul White
  • 90.3k
  • 30
  • 424
  • 663
Loading
Source Link
Paul White
  • 90.3k
  • 30
  • 424
  • 663
Loading