How to add a column in a sql query with selection of the result - sql

I have a sql statment like this:
SELECT a.link_id, a.Speed_Limit_1, a.Speed_Limit_2, a.speed_limit_source, g.autos, g.trucks, g.motorcycles, 'vehicleType' as vehicleType
FROM g, f, d, a
WHERE f.access_id = g.access_id
AND d.condition_id = f.condition_id
AND g.access_id = a.access_id
result:
link_id......autos...trucks...motorcycles.........vehicleTypes
1..........|.......Y....|....Y......|.....N.................|.............?
2..........|.......Y....|....Y......|.....Y.................|.............?
3..........|.......Y....|....Y......|.....N.................|.............?
4..........|.......Y....|....N......|.....Y.................|.............?
I will add the column "vehicleTypes. Inside this column should be:
0 if autos, trucks and motorcycles are allowed (e.g. row 2)
1 if only cars are allowed
2 if only trucks are allowed
3 if only motorcycles are allowed
How can I do this?

This should do it.
I added an ELSE -1 to indicate where it didnt match any of the criteria.
SELECT
a.link_id,
a.Speed_Limit_1,
a.Speed_Limit_2,
a.speed_limit_source,
g.autos, g.trucks,
g.motorcycles,
CASE
WHEN g.autos = 'Y' AND g.trucks = 'Y' AND g.motorcycles = 'Y' THEN 0
WHEN g.autos = 'Y' AND g.trucks = 'N' AND g.motorcycles = 'N' THEN 1
WHEN g.autos = 'N' AND g.trucks = 'Y' AND g.motorcycles = 'N' THEN 2
WHEN g.autos = 'N' AND g.trucks = 'N' AND g.motorcycles = 'Y' THEN 3
ELSE -1
END as vehicleTypes
FROM g, f, d, a
WHERE f.access_id = g.access_id
AND d.condition_id = f.condition_id
AND g.access_id = a.access_id

You should use CASE WHEN block
for example in Oracle:
https://www.techonthenet.com/oracle/functions/case.php

Related

How to select twice the same column on join query?

How Can I to count the gender when is M or F, somehting like
SELECT count(N.gender)
FROM `DATABASE_T` as T
LEFT JOIN `DATABASE_N` as N
ON
T.ENCUESTA = N.ENCUESTA AND
T.P_DEPTO = N.P_DEPTO AND
T.P_MUNIC = N.P_MUNIC AND
T.COD_VEREDA = N.COD_VEREDA AND
T.PAIS = N.PAIS and
T.UC_UO = N.UC_UO
WHERE N.ID_PROD=1 and N.gender="M"
SELECT countif(N.gender = 'M') as M, countif(N.gender = 'F') as F
FROM `DATABASE_T` as T
LEFT JOIN `DATABASE_N` as N
ON
T.ENCUESTA = N.ENCUESTA AND
T.P_DEPTO = N.P_DEPTO AND
T.P_MUNIC = N.P_MUNIC AND
T.COD_VEREDA = N.COD_VEREDA AND
T.PAIS = N.PAIS and
T.UC_UO = N.UC_UO
WHERE N.ID_PROD=1
Since you did not describe the structure of your table and use Spanish-looking identifiers, I will use a clearer example with my own schema:
SELECT
SUM( CASE WHEN Sex = 'M' THEN 1 ELSE 0 END ) AS M,
SUM( CASE WHEN Sex = 'F' THEN 1 ELSE 0 END ) AS F
FROM People
WHERE People.Dept = 5

CASE WHEN THEN Cannot perform an aggregate function on an expression containing an aggregate or a subquery

this is my code:
SELECT SUM
(CASE
WHEN (dbo.EMBARQUE.EmbEst) = 0 THEN
0
WHEN (dbo.EMBARQUE.EmbEst) = 3 THEN
0
WHEN (dbo.EMBARQUE.EmbEst) = 6 THEN
0
WHEN (dbo.EMBARQUE.EmbEst) = 7 THEN
CASE
WHEN (SELECT COUNT (dbo.CUMPLIDO.CumpCod) from dbo.CUMPLIDO where dbo.CUMPLIDO.EmbCod = dbo.EMBARQUE.EmbCod and dbo.CUMPLIDO.CumpVol = 0) > 0 THEN
0
ELSE
dbo.EMBARQUE.EmbVol
END
ELSE
dbo.EMBARQUE.EmbVol
END) FROM dbo.EMBARQUE
You have a subquery inside sum(). That isn't allowed. This version uses a subquery to calculate the flag you want and then moves the filtering logic to the WHERE clause:
SELECT SUM(e.EmbVol)
FROM (SELECT e.*,
(CASE WHEN EXISTS (SELECT 1 FROM dbo.CUMPLIDO c WHERE c.EmbCod = e.EmbCod AND c.CumpVol = 0)
THEN 1 ELSE 0
END) as is_CumpVol_0
FROM dbo.EMBARQUE e
WHERE e.EmbEst NOT IN (0, 3, 6)
) e
WHERE e.EmbEst <> 7 OR is_CumpVol_0 = 0;

SQL Server: update five columns with one condition

In a table, I have a column for currency. I have to calculate a value from two columns (ex: a, b) and update one of the five columns (ex: e, f, g, h, i, j) based on the currency.
I tried with CASE like
CASE when currency = 'INR'
then a+b
else 0
end as e
CASE when currency = 'USD'
then a+b
else 0
end as f
CASE when currency = 'CAN'
then a+b
else 0
end as g
CASE when currency = 'EUR'
then a+b
else 0
end as h
CASE when currency = 'AUD'
then a+b
else 0
end as i
Is there any other way to do this?
Update myTable
set e = CASE when currency = 'INR' then a+b ELSE 0 end,
f = CASE when currency = 'USD' then a+b ELSE 0 end,
g = CASE when currency = 'CAN' then a+b ELSE 0 end,
h = CASE when currency = 'EUR' then a+b ELSE 0 end,
i = CASE when currency = 'AUD' then a+b ELSE 0 end;
Pseudo code would be like following.
NULL / 0 values are not covered.
var result = a+b;
var columnName;
CASE
WHEN currency = 'INR' THEN columnName = e,
WHEN currency = 'USD' THEN columnName = f,
.
.
WHEN currency = 'AUD' THEN columnName = j
END
**your update statement goes here with 'columnName' variable..**

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

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