I'm using SQLite3 and the following query, It seems like there should be a better performing way to write this query; Is there?
Select Name,
Sum(Case
When
Used
Is Null
Then
ItemQty
Else
0
End) As QtyNotUsed
From
ContainersReceived
Group By
Name
Basically my thought (that I can't realize) is something like:
Select
Distinct Name
From
ContainersReceived
combined with
Select
Sum(ItemQty)
From
ContainersReceived
Where
Used Is Null
The result I want is a list of all Name
s and how much is in stock from the table ContainersReceived
.
UPDATE:
Possible solution is to use:
Select
Name,
Sum(ItemQty)
From ContainersReceived
Where Used Is Null
Group By Name
UNION ALL
Select
Name,
0
From ContainersReceived
Where Used Is Not Null
Group By Name
ItemQty
for every group (of rows with same Name)?Used
column (and how many not null)?