How to select twice the same column on join query? - google-bigquery

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

Related

How to update this Select Query to Update? - So far, it just hangs

I eventually managed to get what I was after by grouping and then applying an additional filter with 'HAVING' which is probably not the best way to go about it, but I am new to SQL and this is the only way I could achieve what I was after... which was a list of 'simple' items where the 'qty' is 0 and the range status (attribute_id = 168) is Discontinued (ID 96) and the 'status' (attribute_id = 97) is enabled (id = 1). This is shown below:
SELECT
`mgic_catalog_product_entity`.`sku` AS `sku`,
`mgic_catalog_product_entity`.`type_id` AS `type_id`,
`mgic_cataloginventory_stock_item`.`qty` AS `qty`,
MAX(Case WHEN `mgic_eav_attribute`.`attribute_id` = 97 THEN `mgic_catalog_product_entity_int`.`value` END) AS status,
MAX(Case WHEN `mgic_eav_attribute`.`attribute_id` = 168 THEN `mgic_catalog_product_entity_int`.`value` END) AS range_status
FROM (((`mgic_eav_attribute`
join `mgic_catalog_product_entity_int` on ((`mgic_eav_attribute`.`attribute_id` = `mgic_catalog_product_entity_int`.`attribute_id`)))
join `mgic_catalog_product_entity` on ((`mgic_catalog_product_entity_int`.`entity_id` = `mgic_catalog_product_entity`.`entity_id`)))
join `mgic_cataloginventory_stock_item` on ((`mgic_catalog_product_entity_int`.`entity_id` = `mgic_cataloginventory_stock_item`.`product_id`)))
WHERE `mgic_catalog_product_entity`.`type_id` = 'simple' AND `mgic_cataloginventory_stock_item`.`qty` = 0
GROUP BY `mgic_catalog_product_entity`.`sku`
HAVING status = 1 and range_status = 96;
I am now trying to amend this to an update instruction to set the 'status' (attribute_id = 97) to disabled (id = 2) for the results list of the below query.
update mgic_eav_attribute ea join
mgic_catalog_product_entity_int cpei on ea.attribute_id = cpei.attribute_id join
mgic_catalog_product_entity cpe on cpei.entity_id = cpe.entity_id join
mgic_cataloginventory_stock_item cisi on cpei.entity_id = cisi.product_id
set cpei.value = 2
WHERE cpe.type_id = 'simple' AND ((ea.attribute_code = 'status') and
(cpei.value = 1)) AND cisi.qty = 0 AND EXISTS(
SELECT
cpe.sku,
cpe.type_id,
cisi.qty,
MAX(Case WHEN ea.attribute_id = 97 THEN cpei.value END) AS status,
MAX(Case WHEN ea.attribute_id = 168 THEN cpei.value END) AS range_status
FROM (((`mgic_eav_attribute`
join `mgic_catalog_product_entity_int` on ((`mgic_eav_attribute`.`attribute_id` = `mgic_catalog_product_entity_int`.`attribute_id`)))
join `mgic_catalog_product_entity` on ((`mgic_catalog_product_entity_int`.`entity_id` = `mgic_catalog_product_entity`.`entity_id`)))
join `mgic_cataloginventory_stock_item` on ((`mgic_catalog_product_entity_int`.`entity_id` = `mgic_cataloginventory_stock_item`.`product_id`)))
WHERE `mgic_catalog_product_entity`.`type_id` = 'simple' AND `mgic_cataloginventory_stock_item`.`qty` = 0
GROUP BY cpei.entity_id
HAVING status = 1 and range_status = 96);
The above query just seems to hang and doesn't execute. Can anyone help?

CASE expression in WHERE clause for diferent and

Can I use case expression to build where like this?
select *
from table
where
case
when x=y then z= j and t=v
when x=k then q= p and s=l
end
;
I need change where clause depending on the value of x variable.
Use or:
select *
from table
where (x = y and z = j and t = v) or (x = k and q = p and s = l);
An alternative to using OR is to use nested CASE statements:
SELECT *
FROM table_name
WHERE CASE
WHEN x = y THEN CASE WHEN z = j AND t = v THEN 1 ELSE 0 END
WHEN x = k THEN CASE WHEN q = p AND s = l THEN 1 ELSE 0 END
ELSE 0
END = 1;
or you could simplify it to:
SELECT *
FROM table_name
WHERE CASE
WHEN x = y AND z = j AND t = v THEN 1
WHEN x = k AND q = p AND s = l THEN 1
ELSE 0
END = 1;
However, you should check whether Oracle can use column indexes or if a separate function-based index is required with these solutions.

Filter only get one condition

I have a validation in WHERE clause like:
...
AND (#BDOnly = 0
OR [DT].[Abbreviation] = #IsBDChecked)
AND (#CDOnly = 0
OR [DT].[Abbreviation] = #IsCDChecked)
AND (#PPOOnly = 0
OR [DT].[Abbreviation] = #IsPPOChecked)
AND (#FBOMOnly = 0
OR [DT].[Abbreviation] = #IsFBOMChecked)
AND (#APOnly = 0
OR [DT].[Abbreviation] = #IsAPChecked)
AND (#COOnly = 0
OR [DT].[Abbreviation] = #IsCOChecked)
So each AND clause check if boolean is bit value is 0 or 1, if it's 0 just do any but if it's 1 it do a filter. My problem is
if I'm sending two with value 1, I mean
#BDOnly = 1 and #CDOnly = 1 it only filter one of them instead get two filters I mean:
[DT].[Abbreviation] = #IsBDChecked
and
[DT].[Abbreviation] = #IsCDChecked
What am I doing wrong? Regards
I'm going to take a guess here because there's not enough data. If you want to return a row where Abbreviation matched #IsBDChecked only when #BDOnly is set, and also another row where Abbreviation matches #IsCDChecked when #CDOnly is set, try this:
(#BDOnly = 1
AND [DT].[Abbreviation] = #IsBDChecked)
OR (#CDOnly = 1
AND [DT].[Abbreviation] = #IsCDChecked)
OR (#PPOOnly = 1
AND [DT].[Abbreviation] = #IsPPOChecked)
OR (#FBOMOnly = 1
AND [DT].[Abbreviation] = #IsFBOMChecked)
OR (#APOnly = 1
AND [DT].[Abbreviation] = #IsAPChecked)
OR (#COOnly = 1
AND [DT].[Abbreviation] = #IsCOChecked)
Small representative case:
DECLARE #ADOnly BIT = 0
, #BDOnly BIT = 1
, #CDOnly BIT = 1
, #IsADChecked NVARCHAR(100) = 'A'
, #IsCDChecked NVARCHAR(100) = 'C'
, #IsBDChecked NVARCHAR(100) = 'B'
;WITH myTable AS
(
SELECT * FROM (VALUES ('A'), ('B'), ('C')) X(Abbreviation)
)
SELECT *
FROM myTable
WHERE (#ADOnly = 1 AND Abbreviation = #IsADChecked)
OR (#BDOnly = 1 AND Abbreviation = #IsBDChecked)
OR (#CDOnly = 1 AND Abbreviation = #IsCDChecked)
Yields:
Abbreviation
------------
B
C

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

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

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