Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select multiple counts from one database table in one sql command

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?

like image 483
endorphin Avatar asked Feb 08 '11 16:02

endorphin


1 Answers

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
like image 52
remi bourgarel Avatar answered Sep 23 '22 17:09

remi bourgarel