How to handle with NULL values - sql

I have the following code:
`SELECT nt.pl,
o.cat,
o.coun,
COUNT (DISTINCT nt.im) Sub,
SUM (nt.DOWN + nt.UP) Vol,
COUNT (*) AS counter,
CASE
WHEN nt.tele = '5' THEN 'Vo'
WHEN nt.tele = ('N/A') AND nt.rat = ('G') THEN 'G'
ELSE 'Unknown'
END
AS Service
FROM tmp nt, description o
WHERE nt.pl = o.pl AND nt.time LIKE '201605%'
GROUP BY nt.pl,
o.cat,
o.coun,
nt.tele,
nt.rat`
The problem is that the column rat contains NULL values and in the result I receive more rows than expected. How I could handle with these NULL values.
Thanks

I suspect that rather than having the nt.tele and nt.rat columns in the group by list, you want the case expression instead, e.g.:
SELECT nt.pl,
o.cat,
o.coun,
COUNT (DISTINCT nt.im) Sub,
SUM (nt.DOWN + nt.UP) Vol,
COUNT (*) AS counter,
CASE
WHEN nt.tele = '5' THEN 'Vo'
WHEN nt.tele = ('N/A') AND nt.rat = ('G') THEN 'G'
ELSE 'Unknown'
END
AS Service
FROM tmp nt, description o
WHERE nt.pl = o.pl AND nt.time LIKE '201605%'
GROUP BY nt.pl,
o.cat,
o.coun,
CASE
WHEN nt.tele = '5' THEN 'Vo'
WHEN nt.tele = ('N/A') AND nt.rat = ('G') THEN 'G'
ELSE 'Unknown'
END;

You are trying to filter the null value?
WHERE nt.pl = o.pl AND nt.time LIKE '201605%' AND nt.rat is not null
??

Related

Nested CASE statements in SQL

I am running the below SQL and I need to add a case statement for the svcState column.
I have a value defined for each number in that column which I need to have in my query. For instance 7 is OK, 4 is down etc. I tried adding this in the CASE statement as below and it seems, the syntax is incorrect. Any help will be greatly appreciated.
SELECT * FROM
(
SELECT
A.NodeName AS NodeName,
MAX(CASE WHEN Poller_Name='svcServiceName' THEN CAST(Status AS varchar) ELSE ''END) svcServiceName,
MAX(CASE (CASE WHEN Poller_Name='svcState' AND Status ='7' THEN 'OK'
WHEN Poller_Name='svcstate' AND Status ='4' THEN 'OUT OF SERVICE' END)
THEN CAST(Status AS bigint) ELSE '' END) svcState
FROM
(
SELECT
Nodes.Caption AS NodeName, CustomNodePollers_CustomPollers.UniqueName AS Poller_Name, CustomNodePollerStatus_CustomPollerStatus.Status AS Status, CustomNodePollerStatus_CustomPollerStatus.rowid as row, CustomNodePollerStatus_CustomPollerStatus.RawStatus as RawStatus
FROM
((Nodes INNER JOIN CustomPollerAssignment CustomNodePollerAssignment_CustomPollerAssignment ON (Nodes.NodeID = CustomNodePollerAssignment_CustomPollerAssignment.NodeID)) INNER JOIN CustomPollers CustomNodePollers_CustomPollers ON (CustomNodePollerAssignment_CustomPollerAssignment.CustomPollerID = CustomNodePollers_CustomPollers.CustomPollerID)) INNER JOIN CustomPollerStatus CustomNodePollerStatus_CustomPollerStatus ON (CustomNodePollerAssignment_CustomPollerAssignment.CustomPollerAssignmentID = CustomNodePollerStatus_CustomPollerStatus.CustomPollerAssignmentID)
WHERE
(
(CustomNodePollers_CustomPollers.UniqueName = 'svcServiceName') OR
(CustomNodePollers_CustomPollers.UniqueName = 'svcState')
)
AND
(
(CustomNodePollerAssignment_CustomPollerAssignment.InterfaceID = 0)
)
and Nodes.Caption = '101'
)A
GROUP BY NodeName, row
--ORDER BY svcServiceName
) B
Desired Output
MAX(CASE WHEN Poller_Name = 'svcState' THEN (CASE WHEN status = '7' THEN 'OK' ELSE 'DOWN' END) END)
Or...
MAX(CASE WHEN Poller_Name = 'svcState' AND status = '7' THEN 'OK'
WHEN Poller_Name = 'svcState' AND status = '4' THEN 'DOWN' END)
Or...
MAX(CASE WHEN Poller_Name != 'svcState' THEN NULL -- Assumes the poller_name is never NULL
WHEN status = '7' THEN 'OK'
WHEN status = '4' THEN 'DOWN'
END)
Where there is no ELSE specified, it is implicitly ELSE NULL, and NULL values are skipped by the MAX().

Sql out put which come into multiple row converted to single row

This query give multiple row which needs to be shown in single row. Please help.
SELECT blng_serv_code, (COUNT (blng_serv_code)) AS total ,
DECODE (package_trx_yn, 'Y', 'PKG', 'N', 'NPKG') pkg_status FROM bl_patient_charges_folio
WHERE operating_facility_id = 'MC'
AND trx_date >= TO_DATE ('10/10/2019 00:00:00', 'MM/DD/YYYY HH24:MI:SS')AND blng_serv_code = 'LBSB000015'
GROUP BY blng_serv_code, package_trx_yn
If you want the value in a single row, leave out the package status:
SELECT blng_serv_code, COUNT(*) AS total
FROM bl_patient_charges_folio
WHERE operating_facility_id = 'MC' AND
trx_date >= DATE '2019-10-10' AND
blng_serv_code = 'LBSB000015'
GROUP BY blng_serv_code;
If you do want the package status, then you need to explain the logic for including it "on a single row".
EDIT:
It sounds like you want the values in separate columns:
SELECT blng_serv_code, COUNT(*) AS total,
SUM(CASE WHEN package_trx_yn = 'Y' THEN 1 ELSE 0 END) as pkg_cnt,
SUM(CASE WHEN package_trx_yn = 'N' THEN 1 ELSE 0 END) as npkg_cnt
FROM bl_patient_charges_folio
WHERE operating_facility_id = 'MC' AND
trx_date >= DATE '2019-10-10' AND
blng_serv_code = 'LBSB000015'
GROUP BY blng_serv_code;

SQL CASE Logic Issue

Could someone please assist me? Example: Problem I am having is when an account has AV.Query='PN4', AV.Response = 'Y', AV.Query='FL1' and AV.Response='Y', the result needs to be
PStatus IStatus
4 2
However, I am getting
PStatus IStatus
4 5
5 2
It picks "5" all the time on the opposite column.
SELECT distinct A.AcctNum,
CASE
WHEN O.Order = 'NEI2' THEN '1'
WHEN AV.Query IN ('PNE1','PNE2') AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'PN20' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query = 'PN4' AND AV.Response = 'Y' THEN '4'
ELSE '5'
END AS [PStatus],
CASE
WHEN O.Order IN ('DO2','FL25','VACHP') THEN '1'
WHEN AV.Query = 'FL1' AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'REF' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query IN ('FL2','FL6','NEU.G','HE.B') AND AV.Response = 'Y' THEN '4'
WHEN AV.Query = 'NOA' AND AV.Response = 'Y' THEN '6'
ELSE '5'
END AS [IStatus]
FROM AData AS AD
INNER JOIN AVisit AS AV
ON AD.Visit = AV.Visit
AND AV.QueryID IN ('PNE1','PNE2','PN20','PN4','FL1','REF','FL2','FL6','NEU.G','HE.B','NOA')
LEFT JOIN Order AS O
ON AD.Visit = O.Visit
AND O.Order IN ('NEI2','DO2','FL25','VACHP');
Your results appear to be correct for the query.
Consider first the row of the join that has
AV.Query='PN4', AV.Response = 'Y',
This matches the last WHEN condition in the CASE expression for [PStatus], but it does not match any of the WHEN conditions in the CASE expression for [IStatus]. Result:
4 5
Consider now the join row having
AV.Query='FL1' and AV.Response='Y'
This does not match any WHEN condition in the CASE expression for [PStatus], but it matches the second WHEN conditions in the CASE expression for [IStatus]. Result:
5 2
I could give you a variation on your query that produces the results you specified for the particular data you present, but I do not do so because there are many alternatives, and you haven't given us the information to determine which one correctly answers the question for other data.
UPDATE:
To combine multiple result rows into one, you require an aggregate query. One possible way to implement such a query to provide a single row having values 4, 2 for your given data would be
SELECT
AcctNum,
MIN(
CASE
WHEN O.Order = 'NEI2' THEN '1'
WHEN AV.Query IN ('PNE1','PNE2') AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'PN20' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query = 'PN4' AND AV.Response = 'Y' THEN '4'
ELSE NULL
END
) AS [Pstatus],
MIN(
CASE
WHEN O.Order IN ('DO2','FL25','VACHP') THEN '1'
WHEN AV.Query = 'FL1' AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'REF' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query IN ('FL2','FL6','NEU.G','HE.B') AND AV.Response = 'Y' THEN '4'
WHEN AV.Query = 'NOA' AND AV.Response = 'Y' THEN '6'
ELSE NULL
END
) AS [IStatus]
FROM
AData AS AD
INNER JOIN AVisit AS AV
ON AD.Visit = AV.Visit
AND AV.QueryID IN ('PNE1','PNE2','PN20','PN4','FL1','REF','FL2','FL6','NEU.G','HE.B','NOA')
LEFT JOIN Order AS O
ON AD.Visit = O.Visit
AND O.Order IN ('NEI2','DO2','FL25','VACHP');
GROUP BY AcctNum
This works because aggregate functions other than COUNT() ignore NULLs. It very likely will not give the desired answer for different data patterns, however -- especially patterns where there are more than two AVisits for the same account.
Results are correct.
Your values are:
First row - AV.Query='PN4', AV.Response = 'Y'
Second row - AV.Query='FL1', AV.Response='Y'
First CASE PStatus: Result is 4 as you expected.
Next Case, Istatus. Here is probably lying misunderstanding, Because SQL will process still the same values (First row). This CASE will check against values: AV.Query='PN4', AV.Response = 'Y' from first line. Not against your second values (AV.Query='FL1' and AV.Response='Y').
Values 'PN4' and 'FL1' can not be in same Column in a same line! One column can have only one value, not two. You are probably expecting that in this second case SQL will start fetching another (second) line of data. But it is not.
Second case result is correct Because in the CASE there is not condition for value PN4, so it is end up as ELSE = 5
OK Edit, do it like this, this brute force, but will work ;-)
SELECT A.AcctNum,
COALESCE(MAX( CASE
WHEN O.Order = 'NEI2' THEN '1'
WHEN AV.Query IN ('PNE1','PNE2') AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'PN20' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query = 'PN4' AND AV.Response = 'Y' THEN '4'
ELSE NULL
END),5) AS [PStatus],
COALESCE(MAX(CASE
WHEN O.Order IN ('DO2','FL25','VACHP') THEN '1'
WHEN AV.Query = 'FL1' AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'REF' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query IN ('FL2','FL6','NEU.G','HE.B') AND AV.Response = 'Y' THEN '4'
WHEN AV.Query = 'NOA' AND AV.Response = 'Y' THEN '6'
ELSE NULL
END),5) AS [IStatus]

Merging data SQL Query

I have a query request where I have to show one customer activity for each web-site but it has to be only one row each, instead of one customer showing multiple times for each activity.
Following is the query I tried but brings lot more rows. please help me as how I can avoid duplicates and show only one customer by each row for each activity.
SELECT i.customer_id, i.SEGMENT AS Pistachio_segment,
(CASE when S.SUBSCRIPTION_TYPE = '5' then 'Y' else 'N' end ) PB_SUBS
(CASE WHEN S.SUBSCRIPTION_TYPE ='12' THEN 'Y' ELSE 'N' END) Daily_test,
(CASE when S.SUBSCRIPTION_TYPE ='8' then 'Y' else 'N' end) COOK_4_2
FROM IDEN_WITH_MAIL_ID i JOIN CUSTOMER_SUBSCRIPTION_FCT S
ON I.IDENTITY_ID = S.IDENTITY_ID and I.CUSTOMER_ID = S.CUSTOMER_ID
WHERE s.site_code ='PB' and s.subscription_end_date is null
Sounds like you need to group by customer_id and perform aggregations for the other columns you are selecting. For example:
sum(case when s.subscription_type = '5' then 1 else 0 end) as pb_subs_count
You could try one of two things:
Use a GROUP BY statement to combine all records with the same id, e.g.,
...
WHERE s.site_code ='PB' and s.subscription_end_date is null
GROUP BY i.customer_id
Use the DISTINCT command in your SELECT, e.g.,
SELECT DISTINCT i.customer_id, i.SEGMENT, ...
you could use a aggregation (SUM) on customer_id, but what do you expect to happen on the other fields? for example, if you have SUBSCRIPTION_TYPE 5 and 13 for the same customer (2 rows), which value do you want?
Perhaps you are looking for something like this:
SELECT i.customer_id, i.SEGMENT AS Pistachio_segment,
MAX(CASE when S.SUBSCRIPTION_TYPE = '5' then 'Y' else 'N' end ) PB_SUBS
MAX(CASE WHEN S.SUBSCRIPTION_TYPE ='12' THEN 'Y' ELSE 'N' END) Daily_test,
MAX(CASE when S.SUBSCRIPTION_TYPE ='8' then 'Y' else 'N' end) COOK_4_2
FROM IDEN_WITH_MAIL_ID i JOIN CUSTOMER_SUBSCRIPTION_FCT S
ON I.IDENTITY_ID = S.IDENTITY_ID and I.CUSTOMER_ID = S.CUSTOMER_ID
WHERE s.site_code ='PB' and s.subscription_end_date is null
GROUP BY i.customer_id, i.SEGMENT
I can't be sure, though, without knowing more about the tables involved.

SQL Select Sum Query

I need to complete the following but need to SUM on EntryValue where EntryType= 'C' - EntryType = 'D' but not sure where to include it.
SELECT SUM(EntryValue) AS EntryValue, Left(Warehouse,1) AS Branch
FROM GenJournalDetail INNER JOIN InvJournalCtl ON (GenJournalDetail.GlPeriod = InvJournalCtl.LedgPeriod) AND (GenJournalDetail.GlYear = InvJournalCtl.YearPostLedg) AND (GenJournalDetail.Journal = InvJournalCtl.GlJournal)
WHERE GenJournalDetail.GlCode = 'Z5207550' AND GlYear = '2011' AND GlPeriod = '10'
GROUP BY Left(InvJournalCtl.Warehouse,1)
Select it then group it;
SELECT SUM(EntryValue) AS EntryValueTotal, EntryType, Left(Warehouse,1) AS Branch
...
GROUP BY Left(InvJournalCtl.Warehouse,1), EntryType
Edit I didn't realise you wanted to deduct;
SELECT
SUM(CASE EntryType
WHEN 'C' THEN EntryValue
ELSE 0 END
)
-
SUM(CASE EntryType
WHEN 'D' THEN EntryValue
ELSE 0 END
) AS EntryValueTotal,
Left(Warehouse,1) AS Branch
FROM GenJournalDetail
INNER JOIN InvJournalCtl ON (GenJournalDetail.GlPeriod = InvJournalCtl.LedgPeriod) AND (GenJournalDetail.GlYear = InvJournalCtl.YearPostLedg) AND (GenJournalDetail.Journal = InvJournalCtl.GlJournal)
WHERE
GenJournalDetail.GlCode = 'Z5207550' AND GlYear = '2011' AND GlPeriod = '10'
GROUP BY Left(InvJournalCtl.Warehouse,1)
If you need to sum the column add it in the select and group:
SELECT SUM(EntryValue) AS TotEntry, ...
...
GROUP BY ...
you don't need to group an aggregated column; you need to filter query too?