Using two case statements in SQL Server - sql-server

I want to create a separate column from 1st case statement (let's say A) and 2nd separate column from 2nd case statement (say B). Then use these columns to perform division. But this query gives the result in 1 single column renamed as pym.
select
WFMGrade,
case
when WFMGrade IN ('P/PA','PAT','A')
then sum(TotalFTE)
when WFMGrade IN ('SA','M','SM','AD','D','SD','AVP','VP','SVP','EVP')
then sum(TotalFTE)
else 0
end as pym
from
dw_UtilizationPredictionReport
group by
(WFMGrade)
How do I create separate columns for these two case statements?

Wrap your query in two separate case statements as below:
select
WFMGrade,
case
when WFMGrade IN ('P/PA','PAT','A') then sum(TotalFTE)
else 0
end as pym1,
case
when WFMGrade IN ('SA','M','SM','AD','D','SD','AVP','VP','SVP','EVP') then sum(TotalFTE)
else 0
end as pym2
from
dw_UtilizationPredictionReport
group by
(WFMGrade)

thanks advance
its work for me :
UPDATE products
SET product_price = (
CASE `product_home_id`
WHEN 368794 THEN 3853070
WHEN 368769 THEN 938000
ELSE product_home_id END
),
product_old_price = (
CASE product_home_id
WHEN 368794 THEN 4000000
WHEN 368769 THEN 1100000
ELSE product_old_price END
)
WHERE `product_home_id` IN (368794,368769)
AND `product_vendor_id` =105

Related

Using isnull() function and subquery

I have 2 separate columns, 'ATFTE' and 'SATFTE'. Using these two columns, I have to perform division and store the result in a separate column (say Pyramid). I am trying to use isnull function, because in many cases, the denominator is 0 and bringing it in a subquery. It shows error - 'Invalid Column name' - ATFTE and SATFTE. How can I rectify it?
select isnull(ATFTE/SATFTE,0) as Pyramid
from dw_UtilizationPredictionReport
where WFMGrade in (
select WFMGrade,
case
when WFMGrade IN ('P/PA','PAT','A') then sum(TotalFTE)
else 0
END as ATFTE,
case
when WFMGrade IN ('SA','M','SM','AD','D','SD','AVP','VP','SVP','EVP')
and WFMGrade NOT IN ('Cont') then sum(TotalFTE)
else 0
END as SATFTE
from dw_UtilizationPredictionReport
group by (WFMGrade)
)
select a.WFMGrade, sum(b.TotalFTE) / sum(c.(TotalFTE)
from dw_UtilizationPredictionReport a
left join dw_UtilizationPredictionReport b
on b.WFMGrade = a.WFMGrade
and b.WFMGrade IN ('P/PA','PAT','A')
left join dw_UtilizationPredictionReport c
on c.WFMGrade = a.WFMGrade
and c.WFMGrade IN ('SA','M','SM','AD','D','SD','AVP','VP','SVP','EVP')
group by a.WFMGrade
Try if this is working for you, you dont have to include not in for condition "Cont" since you are having the in condition.
Try this for the first bit
SELECT
CASE
WHEN ISNULL(SATFTE,0) = 0 THEN 0 -- Or whatever you want
ELSE ISNULL(ATFTE/SATFTE,0)
END AS Pyramid
Try this:
select CASE WHEN ISNULL(SATFTE,0) = 0 THEN 0
ELSE ISNULL(ATFTE/SATFTE,0)
END AS Pyramid FROM
(select WFMGrade,
case
when WFMGrade IN ('P/PA','PAT','A') then sum(TotalFTE)
else 0
END as ATFTE,
case
when WFMGrade IN ('SA','M','SM','AD','D','SD','AVP','VP','SVP','EVP')
and WFMGrade NOT IN ('Cont') then sum(TotalFTE)
else 0
END as SATFTE
from dw_UtilizationPredictionReport
group by (WFMGrade)) as tbl
In your query it seems that you have done below mistakes:
You can not use the IN statement with multiple columns in the WHERE
Clause.
ATFTE/SATFTE are not the columns of the table
"dw_UtilizationPredictionReport", so you can not use it in the
select.

Second Order By overrides first in SQL Server

In the following code the purchase_price overrides the case order by statement.
SELECT
i.products_mpn, i.active,
i.distributor_code, i.sales_price
FROM
#duplicates d
LEFT JOIN
import i ON i.products_mpn = d.products_mpn
AND i.distributor_code = (SELECT TOP 1 distributor_code
FROM dbo.kting_ICECAT_import
WHERE products_mpn = d.products_mpn
ORDER BY
(SELECT
CASE
WHEN (i.distributor_code = 'x' or i.distributor_code = 'y') AND CAST(stock as int) > 0
THEN 2
WHEN CAST(stock as int) > 0
THEN 1
ELSE 0
END) DESC,
CAST(purchase_price AS decimal(28,12)))
Is there any way to get it to work with ordering by the case statement and then the purchase_price?
If you want the ordering of the CASE statement to be applied first followed by the ordering of the purchase_price, then your ORDER BY already has the terms in the right place. I don't know what SELECT is doing in your CASE expression, or even if your current query runs. Something like this may be what you had in mind:
ORDER BY CASE WHEN (i.distributor_code = 'x' or i.distributor_code = 'y') AND
CAST(stock as int) > 0 THEN 2
WHEN CAST(stock as int) > 0 THEN 1
ELSE 0
END DESC,
CAST(purchase_price AS DECIMAL(28,12))
The problem was in the ORDER BY SELECT. I had used columns from the left join "i.distributor_code" should just have been distributor_code

SQL Server Remove unwanted rows from a CASE statement

I do a SELECT with a CASE statement with this following:
SELECT DISTINCT
n.NiveauId, n.Description,
CASE WHEN n.NiveauId NOT IN (SELECT ccs.idNiveau WHERE ccs.centreCout = 60001) THEN 0 ELSE 1 END AS attribue
FROM pa.dbo.Niveau n
JOIN BDC.dbo.CentreCoutSecteur ccs ON n.NiveauId = ccs.idNiveau
Explication :
In case "NiveauId" is not present in the other table, the value of "attribue" is 0. Else, if it's present, the value is 1.
This works, but every rows that contains a 1 also shows the same row with a 0.
Exemple:
How would I change the SELECT query to remove the unwanted duplicate rows that contain 0?
Thanks in advance!
Try wrapping your select in a max (if you only want the rows with the highest value for attribue.
SELECT b.NiveauID, b.Description, MAX(b.attribue)
FROM
(SELECT DISTINCT
n.NiveauId, n.Description,
CASE WHEN n.NiveauId NOT IN (SELECT ccs.idNiveau WHERE ccs.centreCout = 60001) THEN 0 ELSE 1 END AS attribue
FROM pa.dbo.Niveau n
JOIN BDC.dbo.CentreCoutSecteur ccs ON n.NiveauId = ccs.idNiveau) b
Group By b.NiveauID, b.Description

Add ORDER BY statement to SELECT statement

I have the following SELECT statement and I need to add ORDER BY [UserName].
SELECT *
FROM [UserInfo]
WHERE ([AWSAccountID] = CASE WHEN #AWSAccountID = -1 THEN [AWSAccountID]
ELSE #AWSAccountID END)
I'm having some trouble figuring out where the ORDER BY needs to go, assuming it can be added at all.
Unless I am missing something you just add your ORDER BY to the end of the query:
SELECT *
FROM [UserInfo]
WHERE ([AWSAccountID] = CASE WHEN #AWSAccountID = -1 THEN [AWSAccountID]
ELSE #AWSAccountID END)
ORDER BY [UserName]

MS SQL - is there a quick way to refer to the result of a case statement without re-doing the case statement?

...In other words...
If I have a case statement that does this...
Case
when Type = 'payment' then net *-1
else net
end as 'pos_neg'
How can I use the result of that in another case statement that split payments and receipts by > or < that 0.
What I want to do is this....
Case
when 'pos_neg' >0 then 'pos_neg'
else 0 end as 'Receipts'
....and the opposite for payments.
the only way I know to acheive this is to nest the first case in the second....which is a lot of repetition.
You could select from a nested select statement.
select case when 'pos_neg' > 0 then 'pos_neg' else 0 end as 'Receipts'
from
(Select case when Type = 'payment' then net*-1 else net end as 'pos_neg'
From Table) T
or use a Common Table Expression
with T(pos_neg) as (
Select case when Type = 'payment' then net*-1 else net end as pos_neg
From Table)
select case when pos_neg > 0 then pos_neg else 0 end as Receipts
from T
select
case
when Type = 'payment' and net * -1 > 0
then 'pos_neg'
when Type = 'payment' and net * -1 <= 0
then ...
...
else 0 end as [Receipts]

Resources