Select multiple counts from one database table in one sql command

13,694
SELECT   ItemScanPoint,
         SUM(CASE ItemType WHEN 1 THEN 1 ELSE 0 END) ,
         SUM(CASE ItemType WHEN 2 THEN 1 ELSE 0 END)   
FROM     ItemsScan 
GROUP BY ItemScanPoint
Share:
13,694
endorphin
Author by

endorphin

developer.

Updated on June 10, 2022

Comments

  • endorphin
    endorphin almost 2 years

    I have a Oracle database table like so, which records an item being scanned at a scanning point.

    ItemsScan
    ItemScanId
    ItemScanPoint
    ItemType
    ScanTime

    I want to return the ItemScanPoint along with the number of times a specific ItemType was scanned at that ItemScanPoint.

    Something along the lines of..

    SELECT ItemScanPoint,
           (SELECT COUNT(*) WHERE ItemType = 1),
           (SELECT COUNT(*) WHERE ItemType = 2)
    FROM   ItemsScan
    

    How do I do this in oracle?

    What is the most efficient way?