case when using multiple conditions across multiple columns - sql

So quick example:
I need to change that 'imperial' to 'Metric' in the UnitsofMeterRemoved Column. However it has to be when meter model = g4 and MeterManufacturerRemoved = KROMSCHRODER.
I have tried this
SELECT *,
CASE WHEN MeterModelRemoved = 'G4'
AND MeterManufacturerRemoved = 'KROMSCHRODER'
AND UnitsofMeterRemoved = 'Imperial'
THEN UnitsofMeterRemoved = 'Metric'
END AS MeterModelRemoved
FROM CTE_2
But the Imperial is still showing up? Any ideas why this may be happening?

use below
SELECT * EXCEPT(UnitsofMeterRemoved),
CASE WHEN MeterModelRemoved = 'G4'
AND MeterManufacturerRemoved = 'KROMSCHRODER'
AND UnitsofMeterRemoved = 'Imperial'
THEN 'Metric'
ELSE UnitsofMeterRemoved
END AS UnitsofMeterRemoved
FROM CTE_2

Related

Using a CASE WHEN with multiple Whens that is only resulting in a BOOLEAN value

I am working on writing a SQL Query which is using a CASE line with multiple When's, and I was wondering why my result is only showing BOOLEAN (True/False) Values. Ideally I would like it to show whatever is after the THEN's in my code.
--with loan as (
SELECT DISTINCT
rtw.identifierdimsk
,lmmf.milestonesk
,lmd.groupname
,rtw.typecode
,rtw.campaignname
,rtw.partnername
,rtw.sourcename
,lmmf.date
,rtw.typecode = CASE WHEN rtw.typecode = 'PROS' THEN 'Prospect'
WHEN rtw.typecode = 'CARI' THEN 'Deluxe'
WHEN rtw.typecode = ('CARIC') OR rtw.typecode = ('CTE') THEN 'CSignal'
WHEN rtw.typecode = 'RMT' THEN 'Pre Pull'
WHEN rtw.typecode in ('90','90PLUS','C90','C90PLUS','BP','ELO','AUTO','POP') THEN 'In-process'
WHEN rtw.typecode = 'XU3%' THEN 'U3'
WHEN rtw.typecode = '%CT%' THEN 'Misc'
END AS Value
FROM rtal_winner rtw
INNER JOIN one_fact lmmf ON lmmf.identifierdimsk = rtw.identifierdimsk
INNER JOIN one_dim lmd ON lmd.milestonesk = lmmf.milestonesk
AND lmmf.date >= '2022-10-25'
Because you are comparing rtw.typecode to the result of the case expression:
rtw.typecode = CASE ...
remove the comparison part:
, lmmf.date
, CASE
WHEN rtw.typecode = 'PROS' THEN 'Prospect'
-- ...
END AS Value
FROM rtal_winner rtw

Rewrite subquery with calculation in SQL to CASE statement

I have, as a part of a bigger query, some subqueries that I would like to convert to CASE statements instead.
The subquery looks like this (and works):
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM [F0001].[dbo].[ProdTr] WHERE AcYrPr = '201601' AND ProdTr.TrTp = 1 AND [F0001].[dbo].[ProdTr].CustNo = '12773') AS dg_period_1
However, I don't seem to find any logical way to put this into a CASE-statement.
Any help would be appreciated!
(
SELECT CASE
WHEN SUM(t1.DAm) <> 0
THEN (SUM(t1.DAm) + SUM(t1.StcCst)) * 100 / SUM(t1.DAm)
ELSE 0 /* or whatever you want to have in this case */
END AS 'DG'
FROM [F0001].[dbo].[ProdTr] t1
WHERE t1.AcYrPr = '201601' AND
t1.TrTp = 1 AND
t1.CustNo = '12773'
) AS dg_period_1
I also removed some unneeded parentheses and simplified an operation (x - (y * -1) = x + y)
You could use the following statement with CASE provided you want to return a null when SUM(DAm) is null or 0.
(SELECT CASE
WHEN SUM(DAm) IS NOT NULL and SUM(DAm) <> 0 THEN (((SUM(DAm) - (SUM(StcCst) * -1)) * 100) /SUM(DAm))
ELSE NULL
END AS 'DG'
FROM [F0001].[dbo].[ProdTr]
WHERE AcYrPr = '201601'
AND ProdTr.TrTp = 1
AND [F0001].[dbo].[ProdTr].CustNo = '12773') AS dg_period_1

Is there a way to ignore the AND in a CASE when something is true?

I have this Where clause
Select * From Student_Info si
Inner Join Certifications cc
Inner Join Cert_Earned ce
Where si.grad_date = #grad_date
AND cc.org_no = #org_no
but I need an additional AND that should be ignored if it turns out the value is false, I will want ALL certificates
AND cc.industrial = CASE WHEN #industrial = 0 THEN Do Nothing
Else #industrial
This would normally be expressed as:
AND (#industrial = 0 OR ccc.industrial = #industrial)
It sounds like you just want to add a predicate that does an OR between two different conditions
AND (#industrial = 0 or ccc.industrial = #industrial)
You can do something like this with CASE functions:
AND 1 = (CASE WHEN #industrial = 0 THEN 1
ELSE CASE WHEN cc.industrial = #industrial THEN 1 END
END)
or if cc.industrial is not nullable, then maybe:
AND cc.industrial = CASE WHEN #industrial = 0 THEN cc.industrial
Else #industrial END

Crystal Reports v14 not finding all null values

Alright, I've run into this before and have gotten everything to work correctly in the past. I have an SQL code that was created that now needs to be turned into a crystal report. The SQL shows 956 lines, but Crystal is only showing 886.
Here is the SQL code:
SELECT
I4240,
I4201,
I4202,
I4203,
I4204,
I4206,
I4213,
I4214,
I4225,
I4208,
I4299
FROM
MT.INVENTORY
WHERE
(
I4202 IN ('UNKNONWN','VERIFY MFR','OTHER','VARIOUS','TBD','NA','N/A') OR
(
I4203 IN ('UNKNONWN','VERIFY MODEL NUMBER','OTHER','VARIOUS','TBD','NA','N/A','MISCELLANEOUS')
OR I4203 IS NULL
OR LENGTH(I4203)=0
) OR
(
I4204 IN ('UNKNOWN DESCRIPTION','VERIFY DESCRIPTION','OTHER','VARIOUS','TBD','NONE - NO STD USED','NA','N/A','MISCELLANEOUS')
OR I4204 IS NULL
OR LENGTH(I4204)=0
)
) AND
I4240 NOT IN ('MT','STD','NESD')
ORDER BY I4240,I4202,I4203,I4204
and the record selection formula from CR:
(
{Inventory.I4240} <> 'mt' and
{Inventory.I4240} <> 'std' and
{Inventory.I4240} <> 'nesd'
)
AND
(
(
{Inventory.I4202} = 'UNKNONWN' OR
{Inventory.I4202} = 'VERIFY MFR' OR
{Inventory.I4202} = 'OTHER' OR
{Inventory.I4202} = 'VARIOUS' OR
{Inventory.I4202} = 'TBD' OR
{Inventory.I4202} = 'NA' OR
{Inventory.I4202} = 'N/A'
)
OR
(
{Inventory.I4203} = 'UNKNONWN' OR
{Inventory.I4203} = 'VERIFY MODEL NUMBER' OR
{Inventory.I4203} = 'OTHER' OR
{Inventory.I4203} = 'VARIOUS' OR
{Inventory.I4203} = 'TBD' OR
{Inventory.I4203} = 'NA' OR
{Inventory.I4203} = 'N/A' OR
{Inventory.I4203} = 'MISCELLANEOUS' OR
ISNULL({Inventory.I4203}) OR
LENGTH(trim({Inventory.I4203})) < 1 OR
INSTR(trim({Inventory.I4203}), "") = 0 OR
TRIM({Inventory.I4203}) = ""
)
OR
(
{Inventory.I4204} = 'UNKNOWN DESCRIPTION' OR
{Inventory.I4204} = 'VERIFY DESCRIPTION' OR
{Inventory.I4204} = 'OTHER' OR
{Inventory.I4204} = 'VARIOUS' OR
{Inventory.I4204} = 'TBD' OR
{Inventory.I4204} = 'NONE - NO STD USED' OR
{Inventory.I4204} = 'NA' OR
{Inventory.I4204} = 'N/A' OR
{Inventory.I4204} = 'MISCELLANEOUS' OR
ISNULL({Inventory.I4204}) OR
LENGTH(trim({Inventory.I4204})) < 1 OR
INSTR({Inventory.I4204}, "") = 0 OR
TRIM({Inventory.I4204}) = ""
)
)
Any help would be appreciated.
In Crystal, if a particular field can be null then you need to check for that condition as the very first thing you do with it, otherwise the entire formula will error out and nothing will be evaluated.
So in your case, fields I4203 and I4204 need the isnull() check moved to the top of their respective sections at the very least. If I4240 and I4202 can be null, then you should handle those conditions as well.
Also, you have a couple references to the word "UNKNONWN"; is that a typo?
The issue was that the report options were set to use null as default, but for some reason in the record selection formula it was set to exception. Once that was set to the default setting it worked without any issues and the counts were correct.

Oracle: an elegant way to extract record which share a column value

I've found two way to take every record, which shares one column value, among some distinct record set identified each one by a set of WHERE conditions.
(The example query are very more clear...)
Do you know a third way more sinthetic? maybe using analytical function?
select a.grup_gruppo_id
FROM conf_gruppi_delim a, conf_gruppi_delim b, conf_gruppi_delim c
WHERE a.ASTG_ASSE_TIPO_GRUPPO_ID = 'BASE_TSC'
AND a.TGAS_TIPGRUPDETT_ASSI_ID = 'C5JqJeruozekiQtN'
AND a.DELI_VALORE1 = '1RWOoegWqEdL9Vch'
AND a.DELI_FLAG_ANN = 'N'
--
AND b.ASTG_ASSE_TIPO_GRUPPO_ID = 'TIPI_MERCATO'
AND b.TGAS_TIPGRUPDETT_ASSI_ID = '_tgas_time_f'
AND b.DELI_VALORE1 = 'ZFLIB'
AND b.DELI_FLAG_ANN = 'N'
--
AND c.ASTG_ASSE_TIPO_GRUPPO_ID = 'SEQUENZA'
AND c.TGAS_TIPGRUPDETT_ASSI_ID = '_tgas_sefm_f'
AND c.DELI_VALORE1 = 'LE8IZjuiOHVtxAwi'
AND c.DELI_FLAG_ANN = 'N'
--
AND A.GRUP_GRUPPO_ID = b.GRUP_GRUPPO_ID
AND b.GRUP_GRUPPO_ID = c.GRUP_GRUPPO_ID
SELECT GRUP_GRUPPO_ID
FROM conf_gruppi_delim a
WHERE ASTG_ASSE_TIPO_GRUPPO_ID = 'BASE_TSC'
AND TGAS_TIPGRUPDETT_ASSI_ID = 'C5JqJeruozekiQtN'
AND DELI_VALORE1 = '1RWOoegWqEdL9Vch'
AND DELI_FLAG_ANN = 'N'
INTERSECT
SELECT GRUP_GRUPPO_ID
FROM conf_gruppi_delim a
WHERE ASTG_ASSE_TIPO_GRUPPO_ID = 'TIPI_MERCATO'
AND TGAS_TIPGRUPDETT_ASSI_ID = '_tgas_time_f'
AND DELI_VALORE1 = 'ZFLIB'
AND DELI_FLAG_ANN = 'N'
INTERSECT
SELECT GRUP_GRUPPO_ID
FROM conf_gruppi_delim a
WHERE ASTG_ASSE_TIPO_GRUPPO_ID = 'SEQUENZA'
AND TGAS_TIPGRUPDETT_ASSI_ID = '_tgas_sefm_f'
AND DELI_VALORE1 = 'LE8IZjuiOHVtxAwi'
AND DELI_FLAG_ANN = 'N'
In this case where you're just filtering on 3 different sets of values
SELECT GRUP_GRUPPO_ID
FROM conf_gruppi_delim a
WHERE (ASTG_ASSE_TIPO_GRUPPO_ID,
TGAS_TIPGRUPDETT_ASSI_ID,
DELI_VALORE1,
DELI_FLAG_ANN ) IN
( ('SEQUENZA','_tgas_sefm_f','LE8IZjuiOHVtxAwi','N'),
('TIPI_MERCATO','_tgas_time_f','ZFLIB','N'),
('BASE_TSC','C5JqJeruozekiQtN','1RWOoegWqEdL9Vch','N') )
GROUP BY GRUP_GRUPPO_ID
HAVING COUNT( distinct ASTG_ASSE_TIPO_GRUPPO_ID ) = 3