I am trying to split a 'grand total' value into two columns in my query but doing so by maintaining one row in the results. I am currently using two different CASE statements to do so but it ends up creating two rows for each result - each one containing a NULL. Is there anyway to do this and only return one one row of results with the grand total split out by the conditions specified?
Example is I have a material that has Quantity 'Blank Qty' of 38 units and an 'Uncovered Qty' of 8 units for a grand total of 46. I would like to see this in one row - but with the code below I get two rows for this material - one for each type of qty. I have looked into PIVOT but doesn't seem like it would do the trick.
CASE
WHEN MRP.STOCK_TYPE = 'A'
THEN SUM(MRP.QUANTITY)
END AS 'Uncovered Qty',
CASE WHEN MRP.STOCK_TYPE <> 'A'
THEN SUM(MRP.QUANTITY)
END AS 'Blank Qty'
returns:
one row with a value of 'uncovered qty' = NULL and 'blank qty' = 38
one row with a value of 'uncovered qty = 8 and 'blank qty = NULL
Is there some way to get this to return in a single row and avoid the NULL
You can bring the condition into the SUMs and that will give you both values in one row:
SUM (CASE WHEN MRP.STOCK_TYPE = 'A' THEN MRP.QUANTITY ELSE 0 END) AS 'Uncovered Qty',
SUM (CASE WHEN MRP.STOCK_TYPE <> 'A' THEN MRP.QUANTITY ELSE 0 END) AS 'Blank Qty'
add isnull() on your subquery to return 0 for nulls and sum() the results again.
select sum(t1.[Uncovered Qty]) as [Uncovered Qty], sum(t1.[Blank Qty]) as [Blank Qty]
from (select
CASE WHEN MRP.STOCK_TYPE = 'A'
THEN ISNULL(SUM(MRP.QUANTITY), 0)
END AS 'Uncovered Qty',
CASE WHEN MRP.STOCK_TYPE <> 'A'
THEN ISNULL(SUM(MRP.QUANTITY), 0)
END AS 'Blank Qty'
from table) t1
Related
So I have this code already.
select
Item,
count(WORK_TYPE) AS 'Capacity Replen'
from WORK_INSTRUCTION
where WORK_TYPE = 'Replen - Capacity'
Group by ITEM
Which outputs this:
Item Capacity Replen
E000191208 3
E000191904 2
E000328017 2
E000397711 2
I need to be able to count a different Work_Type as well and output that count to the associated item.
I think you're looking for conditional aggregation.
SELECT
Item
,SUM( CASE WHEN WORK_TYPE= 'Replen - Capacity' THEN 1 ELSE 0 END) AS 'Capacity Replen'
,SUM( CASE WHEN WORK_TYPE= 'Some Other Criteria' THEN 1 ELSE 0 END) AS 'Some Other Column Name'
FROM WORK_INSTRUCTION
WHERE WORK_TYPE IN ('Replen - Capacity','Some Other Criteria')
GROUP BY ITEM
I am trying to get two new columns (stock type in this case) and their respective quantities. I have tried to use PIVOT but it seems rather limited in SQL.
Tried to use PIVOT
This is part of a larger query but this is the piece I would like to have return as two columns - one for stock type 'A' and one for ' ' - blank. As it is now this returns two rows - one for each stock type.
SELECT MATERIAL,
CASE
WHEN STOCK_TYPE = 'A'
THEN 'UNCOVERED QTY'
ELSE 'BLANK QTY'
END AS [STOCK TYPE],
SUM(QUANTITY) AS 'QUANITTY'
FROM VW_MRP_ALLOCATION
WHERE STOCK_TYPE IN ('A','')
AND MATERIAL = '011040'
GROUP BY STOCK_TYPE,
MATERIAL
This returns:
MATERIAL STOCK TYPE QUANITTY
------------------ ------------- ---------------------------------------
011040 BLANK QTY 67
011040 UNCOVERED QTY 1301
(2 rows affected)
I would like to return one row for the material with two columns - one for 'Uncovered Quantity' and one for ' Blank Quantity'.
Just use conditional aggregation:
SELECT MATERIAL,
SUM(CASE WHEN STOCK_TYPE = 'A' THEN QUANTITY END) as uncovered_qty,
SUM(CASE WHEN STOCK_TYPE <> 'A' THEN QUANTITY END) as blank_qty
FROM VW_MRP_ALLOCATION
WHERE STOCK_TYPE IN ('A', '') AND MATERIAL = '011040'
GROUP BY MATERIAL;
i would like to change code below by using SUM(NVL). any suggestion.
sum(CASE WHEN D.CURRENCY ='MYR' OR D.CURRENCY IS NULL THEN 1 ELSE 0 END) as Cur
previously i create this, but i cannot sum the 'CURRENCY' column which is 'MYR'. this column have null value.
NVL(D.CURRENCY,'MYR') CUR4
the red one i bold must display 'MYR'
First you have sum() function which actually counting the no of MYR in CURRENCY column rather than doing the sum
So, you just filter the NULLs by either COALESCE() or NVL()
SUM(CASE WHEN COALESCE(D.CURRENCY,'MYR') = 'MYR' THEN 1 ELSE 0 END) CUR4
select req.code ,res.code,
case
(
when (req.code==res.code) then 'pass'
when (req.code<>res.code) then 'fail'
/*....2 more case 'when' 'then' statements here*/
end ) result,
req.country ,res.country,
case (when then staments as above)result,
/*.......case stmts upto 70 statemnts*/
from requesttable req full outer join responsetable res on
req.id=res.id
and ....some conditions.
Can anyone tell me how can I sum every column and display the sum as well as the count of records in every column of both tables simultaneously and display count in my query?
My result should be of this sort
code code1 result sum sum1 equivalence country country1 result1 sum sum1
100 100 pass 200000 25000 fail ind aus fail 800000 800000
equivalence
pass
I am trying to prepare a report joining two tables. I am using multiple case statements to accomplish this. I want to display sum of each column and count of each column of both the tables together in a single report. The query that I have is of the following type.
I think this is kind of what you're looking for. For the code and country values displayed on the line it would give you the pass and fail accounts for the combinations displayed and you would have uniqueness on the columns the aggregate is defined on.
Select req.code,
res.code,
Sum(Case When req.code = res.code) Then 1 Else 0 End) As [Pass],
Sum(Case When req.code <> res.code) Then 1 Else 0 End) As [Fail],
req.country,
res.country,
Sum(Case When req.country = res.country) Then 1 Else 0 End) As [Pass],
Sum(Case When req.country <> res.country) Then 1 Else 0 End) As [Fail]
From requesttable req
Full Outer Join responsetable res
On req.id = res.id
Where ...
Group By req.code,
res.code,
req.country,
res.country
I want to provide 5 counts to SSRS from a conditional count on a column. For instance, suppose the column held the color of a product -- green ,blue, red and yellow. What I would like to do is return the count of each in a single query.
Although I can accomplish getting this done using a case statement:
Select
COUNT(*) 'Count',
case
When Color = 'BL' then 'Blue
When Color = 'RD' then 'Red
When Color = 'YL' then 'Yellow
When Color = 'GR' then 'Green
Else 'All Others'
End as Payment
From COLORS(NoLock)
Group by
case
When Color = 'BL' then 'Blue
When Color = 'RD' then 'Red
When Color = 'YL' then 'Yellow
When Color = 'GRthen ‘Green’
Else 'All Others'
End
When I use the dataset is SSRS, all I get is the a single count. I don't want to create 4 dataset queries as I'm actually selection the records by the parameters start and end date and I would end up having 5 sets of date parameters.
This should do the trick
select count (*) as Total,
sum (case when color='BL' then 1 else 0 end) as BlueTotal,
sum (case when color='RD' then 1 else 0 end) as RedTotal,
sum (case when color='YL' then 1 else 0 end) as YellowTotal,
sum (case when color='GR' then 1 else 0 end) as GreenTotal
from Colors