Delete rows if some cells are empty in BigQuery - google-bigquery

How can I can delete rows that have empty cells in a specific field, I used the following query but did not work :(
DELETE `my.table` WHERE TC = NULL or HDL = NULL or TG = NULL or LDL = NULL

Use IS instead of =
DELETE `my.table`
WHERE TC IS NULL or HDL IS NULL or TG IS NULL or LDL IS NULL
or you can use
DELETE `my.table`
WHERE TC || HDL || TG || LDL IS NULL

Related

Join two tables on multiple conditions Using Oracle SQL

I have a 2 Tables with below structures
Table 1-- Containing Values like this.
OTHER_CODE
CAPACITY_CODE
Result
A
1
A
5
A
9
A
(null)
B
2
B
6
B
2
Table_2- With Values Like
OTHER_CODE
CAPACITY_CODE
Result
A
1
A
A
5
B
A
(null)
C
A
ELSE
D
B
ALL
E
(null)
ALL
F
I need to Join Table_1 with Table_2 on basis of columns OTHERCODE and CAPACITYCODE and update values in Column Result of Table**1 **using a Merge statement.
I need to handle and match Values based on ELSE and ALL values too.
Check for Direct Match
Check if ALL or ELSE condition
The Final TABLE_1 must look like
OTHER_CODE
CAPACITY_CODE
Result
Explanation
A
1
A
Direct Join
A
5
B
Direct Join
A
9
D
Satsifying ELSE condition
A
(null)
C
Direct join with NVL handling
B
2
E
As Value for CapacityCode in TableB is ALL
B
6
E
As Value for CapacityCode in TableB is ALL
B
2
E
As Value for CapacityCode in TableB is ALL
I Tried Joining both the tables but the was unable to satisfy Else and ALL conditions. Hope if someone can help me on this.
There are Several **Result ** Columns like , Result 1 ,2 in both tables which needs to be updated using the same logic.
Thanks in Advance.
here is a fiddle to work on https://dbfiddle.uk/FMKdWzQT
I got the query working. by using a case statement and assigning a number so I could use max then I just remove the number.
SELECT a.other_code,
a.capacity_code,
(
SELECT SUBSTR(max(
CASE WHEN b.other_code = a.other_code AND a.capacity_code = b.capacity_code THEN concat('3',b.myresult)
WHEN b.other_code = a.other_code AND a.capacity_code is null and b.capacity_code is null THEN concat('2',b.myresult)
WHEN b.other_code = a.other_code AND b.capacity_code in ('ELSE', 'ALL') THEN concat('1',b.myresult)
else null end),2)
FROM table2 b ) as myresult
FROM table1 a
however I can not get the update to work. I tried a merge it is give me the unstable row error and I tried an update select but that is giving me single row subquery error so maybe someone else can take a look at the fiddle. here was my attempt at the update.
UPDATE table1
SET myresult = (
SELECT myresult
FROM (
SELECT a.other_code,
a.capacity_code,
(
SELECT SUBSTR(max(
CASE WHEN b.other_code = a.other_code AND a.capacity_code = b.capacity_code THEN concat('3',b.myresult)
WHEN b.other_code = a.other_code AND a.capacity_code is null and b.capacity_code is null THEN concat('2',b.myresult)
WHEN b.other_code = a.other_code AND b.capacity_code in ('ELSE', 'ALL') THEN concat('1',b.myresult)
else null end),2)
FROM table2 b ) as myresult
FROM table1 a
)t2
WHERE table1.other_code = t2.other_code and nvl(table1.capacity_code,'x') = nvl(t2.capacity_code,'x')
);

getting ORA-01407:cannot update to null error

i am getting ORA-01407:cannot update to null error for below query, Please help on this.
UPDATE PS_CT_IQN_ACC_STG stg
SET (CTS_WO_ID,CTS_WO_END_DATE,CTS_WO_REG_RATE,CTS_WO_OT_RATE,VENDOR_ID) = (select CT_WORK_ORDER_ID,END_DATE,CT_WO_RATEREGULAR,CT_WO_RATEOVERTIME,CT_WO_VENDOR_ID from ps_cts_iqn_empl_wo WO1
where WO1.CT_WORK_ORDER_ID= (select max(CT_WORK_ORDER_ID) from ps_cts_iqn_empl_wo WO where WO.cts_peoplesoft_id = STG.EMPLID
AND WO.ct_wo_project_id = STG.project_id and stg.report_due_date between WO.start_date and WO.end_date )
and WO1.lastupddttm = (select max(lastupddttm) from ps_cts_iqn_empl_wo WO2 where WO2.cts_peoplesoft_id = STG.EMPLID
AND WO2.ct_wo_project_id = STG.project_id and stg.report_due_date between WO2.start_date and WO2.end_date ))
Your query is updating the following 5 columns of the table PS_CT_IQN_ACC_STG:
CTS_WO_ID
CTS_WO_END_DATE
CTS_WO_REG_RATE
CTS_WO_OT_RATE
VENDOR_ID
SELECT Query to update the column values must be returning NULL for one of the columns for which NOT NULL constraint OR check of not null is applied.
You can check this from the following dictionary views:
USER_TAB_COLS -- NULLABLE column will be N for not null columns
USER_CONSTRAINTS -- Type will be C with Search_condition like <COLUMN_NAME> IS NOT NULL for not null columns.
Cheers!!

Is there any way to show the records using same query?

I want to list out notices which are not sent. So I tried the query like below. But its showing wrong result. Is there any way to show notices which are not sent using the following query.
SELECT
vtn.*,
vn.id as notice_id,
vn.vnotice_datetime as sent_notice_time
FROM
vtemplates vt
LEFT JOIN vtemplate_notices vtn ON( vtn.vtemplate_id = vt.id)
LEFT JOIN vnotices vn ON(vn.vtemplate_notice_id = vtn.id AND vn.vnotice_datetime IS nULL)
LEFT JOIN violations v ON ( v.vtemplate_id = vt.id)
WHERE
v.id = 1
Records in a violation_notices table are as follows:
--------------------------------------------------------------
id vtemplate_notice_id desc vnotice_datetime created_on
---------------------------------------------------------------
1 1 test1 22/12/2018 05:30 22/12/2018
Expected Result:
id vtemplate_id created_on notice_id sent_notice_time
---------------------------------------------------------------
2 1 23/12/2018 NULL NULL
3 1 24/12/2018 NULL NULL
4 1 24/12/2018 NULL NULL
Actual Result:
id vtemplate_id created_on notice_id sent_notice_time
---------------------------------------------------------------
1 1 22/12/2018 NULL NULL
2 1 23/12/2018 NULL NULL
3 1 24/12/2018 NULL NULL
4 1 24/12/2018 NULL NULL
In actual result, it shows first record (which should not come) for which vnotice_datetime is NOT NULL but still it's showing.
Well, left joins don't remove non matching rows. Shifting the IS NULL check from the ON to the WHERE clause might work.
SELECT vtn.*,
vn.id notice_id,
vn.vnotice_datetime sent_notice_time
FROM vtemplates vt
LEFT JOIN vtemplate_notices vtn
ON vtn.vtemplate_id = vt.id
LEFT JOIN vnotices vn
ON vn.vtemplate_notice_id = vtn.id
LEFT JOIN violations v
ON v.vtemplate_id = vt.id
WHERE v.id = 1
AND vn.vnotice_datetime IS NULL;
You can use NOT EXISTS or test the joined column IS NULL in the WHERE clause
https://dev.mysql.com/doc/refman/8.0/en/exists-and-not-exists-subqueries.html
e.g
SELECT * FROM violations
WHERE NOT EXISTS(
SELECT * FROM notifications
WHERE violation_id = violations.id
)
SELECT v.*, n.* FROM violations v
LEFT JOIN notifications n
ON n.violation_id = v.id
WHERE n.violation_id IS NULL

SQL Update query on query result

I have 2 tables like:
Table 1
SID Sdefinition CValue
4057 s1 32
4058 s2
4059 s3 6
4060 s4
Mapping_tbl
SID SINID ECFID SID-SINID
4057 1099 4027e 1099_4057
4058 1099 4027e 1099_4058
4059 1121 4003e 1121_4059
4060 1121 4003e 1121_4060
Query1
SELECT Mapping_tbl.SID, Table1.Sdefinition, Table1.CValue
FROM Table1 INNER JOIN Mapping_tbl ON Table1.SID= Mapping_tbl.SID;
Query1(Result)
SID Sdefinition CValue
4057 s1 32
4058 s2
4059 s3 6
4060 s4
I have a situation that I wanted to update query table (Query1) i.e
set field(Cvalue) to 0 if it contains null. I am using update query like
Update Query1 Set CValue = 0 Where CValue Is Null;
The query table (query1) gets updated and sets Cvalue to 0 if it
contains nulls, and it also updates(set 0) Table1 where Cvalues are
null.
How can I avoid updating Table1? Any suggestions.
It seems you don't want to change the values stored in your table, and you want the query to display zero when CValue is Null. So you can have the query substitute zero for Null in its result set only (without changing values in the table) ...
SELECT m.SID, t.Sdefinition, IIf(t.CValue Is Null, 0, t.CValue) AS [CValue]
FROM Table1 AS t INNER JOIN Mapping_tbl AS m
ON t.SID= m.SID;
For a query in an Access session, you could use the VBA Nz() function ...
SELECT m.SID, t.Sdefinition, Nz(t.CValue Is Null, 0) AS [CValue]
FROM Table1 AS t INNER JOIN Mapping_tbl AS m
ON t.SID= m.SID;
I used aliases for the table names. But you don't need them for Null substitution to work. I just prefer aliases.

Oracle SQL join with counts returning null

I'm having a problem with the following query in Oracle SQL:
SELECT t.diif, t.mlf_response, v.total
FROM t_temp_rows t
LEFT OUTER JOIN
(SELECT a.diif, s.mlf_response, COUNT(a.customer_terid) total
FROM ter_details a
INNER JOIN lieu_details s ON a.lieu_id = s.lieu_id
WHERE a.customer_name = 'CUSTOMER_A' AND mlf_response IS NOT NULL
GROUP BY a.diif, s.mlf_response) v ON v.diif = t.diif AND v.mlf_response = t.mlf_response;
t_temp_rows contains all possible combinations for diif and mlf_response, regardless of whether they actually return counts in the subquery or not. I am hoping to get a count for every row in t_temp_rows, including 0 for rows with no count (I will add the NVL once it actually works).
If I run the query, rather than returning the counts in 'total' where there is a value and null elsewhere, I get null for every row.
Expected:
diif mlf_response total
---- ------------ -----
ABCD YES 12
ABCD NO 32
ABCE YES 54
ABCE NO 01
ABCF YES null
ABCF NO null
Actual:
diif mlf_response total
---- ------------ -----
ABCD YES null
ABCD NO null
ABCE YES null
ABCE NO null
ABCF YES null
ABCF NO null
What have I done wrong?
You need to add a.customer_name = 'CUSTOMER_A' to the left outer join because by adding it to the where, you are making it an inner join.
SELECT t.diif, t.mlf_response, v.total
FROM t_temp_rows t
LEFT OUTER JOIN
(SELECT a.diif, s.mlf_response, COUNT(a.customer_terid) total
FROM ter_details a ON a.customer_name = 'CUSTOMER_A'
LEFT OUTER JOIN lieu_details s ON a.lieu_id = s.lieu_id
WHERE mlf_response IS NOT NULL
GROUP BY a.diif, s.mlf_response) v ON v.diif = t.diif AND v.mlf_response = t.mlf_response;