Using isnull() function and subquery - sql-server

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.

Related

Using two case statements in 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

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

Compare two columns ftp_start_time and ftp_stop_time and return the count of columns based on conditions

[Compare two columns ftp_start_time and ftp_stop_time and return the count of columns based on conditions :3 columns are 1.Tried(count(ftp_start_time)) 2.Success(count of ftp_start_time and ftp_stop_time when not null) 3. Failed(count of ftp_stop_time must be null).. please help
Screenshot
SELECT
a.Branch_code,
b.DevCount AS total,
COUNT(a.ftp_start_time) AS tried
FROM
ftplogview AS a
LEFT JOIN
palmtecsetup AS b ON a.Branch_code = b.Branch_code
WHERE
ftp_start_date = '2016-03-31'
GROUP BY
a.Branch_code, b.DevCount
ORDER BY
Branch_code
This is the query I used so far ...can anyone help me out?
Hope this one will help you, use the sub select query like below -
SELECT
q.Branch_code,
q.DevCount AS total,
COUNT(q.ftp_start_time) AS tried,
SUM(Success) AS SuccessCount,
SUM(Failed) AS FailedCount,
SUM(NOTTRIED) AS NOTTRIEDCount
FROM
(
SELECT
a.Branch_code,
b.DevCount AS total,
a.ftp_start_time AS tried,
CASE WHEN a.ftp_start_time IS NOT NULL AND a.ftp_stop_time IS NOT NULL THEN 1 ELSE 0 END AS Success,
CASE WHEN a.ftp_start_time IS NOT NULL AND a.ftp_stop_time IS NULL THEN 1 ELSE 0 END AS Failed,
CASE WHEN a.ftp_start_time IS NULL AND a.ftp_stop_time IS NULL THEN 1 ELSE 0 END AS NOTTRIED
FROM
ftplogview AS a
LEFT JOIN
palmtecsetup AS b ON a.Branch_code = b.Branch_code
WHERE
ftp_start_date = '2016-03-31'
) Q
GROUP BY
Q.Branch_code, Q.Total
ORDER BY
Q.Branch_code
I would use Common table expression
with cte(Branch_code, DevCount, total,ftp_start_time,ftp_stop_date,ftp_stop_time )
as (
SELECT
a.Branch_code,
b.DevCount,
a.ftp_start_time,
a.ftp_stop_date,
a.ftp_stop_time
FROM
ftplogview AS a
LEFT JOIN
palmtecsetup AS b ON a.Branch_code = b.Branch_code
GROUP BY
a.Branch_code, b.DevCount
)
SELECT Branch_code,
DevCount AS total,
COUNT(ftp_start_time) AS tried,
SUM(
case
when ftp_start_time is not null and ftp_stop_time is not null then 1
else 0 end
) as success,
SUM(
case
when ftp_stop_time is null then 1
else 0 end
) as failed
FROM CTE
WHERE ftp_start_time = '2016-03-31'

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

SQL Percentage calculation

Is it possible in SQL to calculate the percentage of the 'StaffEntered' column's "Yes" values (case when calculated column) out of the grand total number of orders by that user (RequestedBy)? I'm basically doing this function now myself in Excel with a Pivot table, but thought it may be easier to build it into the query. Here is the existing sample SQL code:
Select
Distinct
RequestedBy = HStaff.Name,
AccountID = isnull(pv.AccountID, ''),
StaffEntered = Case When DictionaryItem2.Name like '%PLB%' Then 'Yes' Else 'No' end
FROM
[dbo].[HOrd] HOrd WITH ( NOLOCK )
left outer join HStaff HStaff with (nolock)
on HOrd.Requestedby_oid = HStaff.ObjectID
and HStaff.Active = 1
left outer join DictionaryItem DictionaryItem2 WITH (NOLOCK)
ON HSUser1.PreferenceGroup_oid = DictionaryItem2.ObjectID
AND DictionaryItem2.ItemType_oid = 98
Here is what I am doing in Excel currently with the query results, I have a pivot table and I am dividing the "Yes" values of the "StaffEntered" field out of the Grand Total number of entries for that specific "RequestedBy" user. Essentially Excel is doing the summarization and then I am doing a simple division calculation to obtain the percentage.
Thanks in advance!
You didn't provide a lot in the way of details but I think this should be pretty close to what you are looking for.
select HStaff.Name as RequestedBy
, isnull(pv.AccountID, '') as AccountID
, Case When DictionaryItem2.Name like '%PLB%' Then 'Yes' Else 'No' end as StaffEntered
, sum(Case When DictionaryItem2.Name like '%PLB%' Then 1 Else 0 end) / GrandTotal
From SomeTable
group by HStaff.Name
, isnull(pv.AccountID, '')
, GrandTotal
Giving the FROM part of your SQL Statement would allow us to create a more correct answer. This statement will get the totals of yes/no per HStaff name and add it to each detail record in your SQL statement:
WITH cte
AS ( SELECT HStaff.Name ,
SUM(CASE WHEN dictionaryItem2.Name LIKE '%PLB%' THEN 1
ELSE 0
END) AS YesCount ,
SUM(CASE WHEN dictionaryItem2.Name NOT LIKE '%PLB%'
THEN 1
ELSE 0
END) AS NotCount
FROM YourTable
GROUP BY HStaff.Name
)
SELECT HStaff.Name AS requestedBy ,
ISNULL(pv.AccountID, '') AS AccountID ,
CASE WHEN DictionaryItem2.Name LIKE '%PLB%' THEN 'Yes'
ELSE 'No'
END AS StaffEntered ,
cte.YesCount / ( cte.YesCount + cte.NotCount ) AS PLB_Percentage
FROM yourtable
INNER JOIN cte ON yourtable.Hstaff.Name = cte.NAME

Resources