SQL, iif excel cell is null then fill another excel cell - sql

I have the following part of my query within excel that is not working.
iif(master.[Canada] is null or master.[USA] is null ,'USER','' ) as [Stackoverflow]
Am I doing the nulls correctly?
The logic should
1) If there is No Canada or No Usa data, put "USER" in Stackoverflow column.
2) If either Canada OR USA has data then Stackoverflow should be empty.
Currently what Im getting:
+-----------+--------------+---------------+
| Canada | USA | Stackoverflow |
+-----------+--------------+---------------+
| | | |
| | | |
| 912796NZ8 | | |
| | | |
| | US912796NZ81 | |
| | | |
| 912796NZ8 | US912796NZ81 | |
| 912796NZ8 | US912796NZ81 | |
| 912796qd4 | US912796QD43 | |
| 298785HB5 | US298785HB50 | |
+-----------+--------------+---------------+
What I am expecting:
+-----------+--------------+---------------+
| Canada | USA | Stackoverflow |
+-----------+--------------+---------------+
| | | USER |
| | | USER |
| 912796NZ8 | | |
| | | USER |
| | US912796NZ81 | |
| | | USER |
| 912796NZ8 | US912796NZ81 | |
| 912796NZ8 | US912796NZ81 | |
| 912796qd4 | US912796QD43 | |
| 298785HB5 | US298785HB50 | |
+-----------+--------------+---------------+
After changing query to iif(TRIM(master.[Camada]) = '' OR TRIM(master.[USA]) = '','USER', '') as [Stackoverflow]
It does a good job except now I still have some canada and USA data that gives me USER.
+-----------+-----+---------------+
| Canada | USA | Stackoverflow |
+-----------+-----+---------------+
| 62941ZPA6 | | USER |
| 62943Z4R0 | | USER |
| 62945ZLQ1 | | USER |
| 62950ZZE5 | | USER |
| 75585RLK9 | | USER |
| 00433JAA3 | | USER |
| 13509PEV1 | | USER |
| 13509PEZ2 | | USER |
| 62931ZLX2 | | USER |
| 62941Z8M9 | | USER |
| 62941ZYK4 | | USER |
| 62942ZV42 | | USER |
| 62943Z6T4 | | USER |
| 62946Z6Y0 | | USER |
| 62947ZWC8 | | USER |
| 62948ZTJ6 | | USER |
| 62949ZE51 | | USER |
| 75585RLK9 | | USER |
| 75585RMB8 | | USER |
| 75585RMW2 | | USER |
+-----------+-----+---------------+
Should not have USER for these 20 records.
Any help would be appreciated, thanks.

The Jet/ACE SQL dialect does support IS NULL. However, as your current results suggest, empty strings ('') are not the same as the NULL entity. This is especially true in Excel (a non-database application where empty cells may not default to NULL). In fact, you are actually assigning empty strings in the falsepart of your IIF() call where records without 'USER' value in [Stackoverflow] will be empty string and not NULL.
Consider extending your IIF expressions to account for zero-length strings and assigning NULL to non-matches:
IIF((master.[Canada] IS NULL AND master.[USA] IS NULL) OR
(master.[Canada] = '' AND master.[USA] IS NULL) OR
(master.[Canada] IS NULL AND master.[USA] = '') OR
(master.[Canada] = '' AND master.[USA] = ''), 'USER', NULL) As [Stackoverflow]
Even account for invisible whitespace by using TRIM():
IIF((master.[Canada] IS NULL AND master.[USA] IS NULL) OR
(TRIM(master.[Canada]) = '' AND master.[USA] IS NULL) OR
(master.[Canada] IS NULL AND TRIM(master.[USA]) = '') OR
(TRIM(master.[Canada]) = '' AND TRIM(master.[USA]) = ''), 'USER', NULL) As [Stackoverflow]

I think, Jet uses the IsNull() function instead of the IS NULL operator:
iif(IsNull(master.[Canada]) or IsNull(master.[USA]),'USER','' ) as [Stackoverflow]

Related

Sql server : select members having 2 or more records on different date of same class

I am trying to find out all the member ids who have more than 2 records on a different dates in the same class.
+----------+------------+-------+-----------+
| MemberId | Date | Class | |
+----------+------------+-------+-----------+
| 118111 | 2/18/2020 | A | Valid |
| 118111 | 10/15/2020 | A | Valid |
| 118216 | 1/31/2020 | B | Valid |
| 118216 | 5/16/1981 | B | Valid |
| 118291 | 6/9/2020 | A | Valid |
| 118291 | 12/5/2020 | A | Valid |
| 118533 | 4/9/2020 | A | Not valid |
| 118533 | 11/11/2020 | B | Not valid |
| 118533 | 7/22/2020 | C | Valid |
| 118533 | 10/25/2020 | C | Valid |
| 118293 | 3/30/2020 | A | Not valid |
| 118293 | 3/30/2020 | A | Not valid |
| 118499 | 4/16/2020 | B | Valid |
| 118499 | 7/26/2020 | B | Valid |
| 118499 | 3/25/2020 | A | Not valid |
+----------+------------+-------+-----------+
I have made a query which checks only 2 records but unable to find a solution for checking more than 2 records.
select mc.*
FROM table1 AS mc
JOIN table1 AS ma ON ma.memberid = mc.memberid
AND ma.date != mc.date
AND ma.class = mc.class
Assuming you just want the memberid you can us a HAVING:
SELECT memberid
FROM dbo.YourTable
GROUP BY memberid
HAVING COUNT(DISTINCT [Date]) > 2;

How to get the number of empty cells for each column in spark dataframe

I'd like to get the number of each column's empty value, so I tried
ele_df.where(ele_df['Shipment_ID'].isNotNull()).select('Shipment_ID').show()
But it returns me the empty value, it seems it consider the empty value as a non-null value.
+------------------+
|Shipment_ID|
+------------------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
+------------------+
Could you guys help me with this?

Splitting a table on comma separated emails in Big Query

I have a table with following columns (The email address are comma separated):
+---------+----------+------------+---------------------------------------------+---------+
| Sr. No. | Domain | Store Name | Email | Country |
+---------+----------+------------+---------------------------------------------+---------+
| 1. | kkp.com | KKP | den#kkp.com, info#kkp.com, reno#kkp.com | US |
| 2. | lln.com | LLN | silo#lln.com | UK |
| 3. | ddr.com | DDR | info#ddr.com, dave#ddr.com | US |
| 4. | cpp.com | CPP | hello#ccp.com, info#ccp.com, stelo#ccp.com | CN |
+---------+----------+------------+---------------------------------------------+---------+
I want the output with Email in separate Columns:
+---------+----------+------------+---------------+---------------+---------------+---------+---------+
| Sr. No. | Domain | Store Name | Email 1 | Email 2 | Email 3 | Email N | Country |
|---------+----------+------------+---------------+---------------+---------------+---------+---------+
| 1. | kkp.com | KKP | den#kkp.com | info#kkp.com | reno#kkp.com | ....... | US |
| 2. | lln.com | LLN | silo#lln.com | | | ....... | UK |
| 3. | ddr.com | DDR | info#ddr.com | dave#ddr.com | | ....... | US |
| 4. | cpp.com | CPP | hello#ccp.com | info#ccp.com | stelo#ccp.com | ....... | CN |
+---------+----------+------------+---------------+---------------+---------------+---------+---------+
Can someone please help a beginner in SQL and BigQuery.

Need to shift the data to next column, unfortunately added data in wrong column

I have a table test
+----+--+------+--+--+--------------+--+--------------+
| ID | | Name1 | | | Name2 |
+----+--+------+--+--+--------------+--+--------------+
| 1 | | Andy | | | NULL |
| 2 | | Kevin | | | NULL |
| 3 | | Phil | | | NULL |
| 4 | | Maria | | | NULL |
| 5 | | Jackson | | | NULL |
+----+--+------+--+--+----------+--+--
I am expecting output like
+----+--+------+--+--+----------+--
| ID | | Name1 | | | Name2 |
+----+--+------+--+--+----------+--
| 1 | | NULL | | | Andy |
| 2 | | NULL | | | Kevin |
| 3 | | NULL | | | Phil |
| 4 | | NULL | | | Maria |
| 5 | | NULL | | | Jackson |
+----+--+------+--+--+----------+--
I unfortunately inserted data in wrong column and now I want to shift the data to the next column.
You can use an UPDATE statement with no WHERE condition, to cover the entire table.
UPDATE test
SET Name2 = Name1,
Name1 = NULL

SQL Merge multiple columns into one column

I have a SQL statement that is combining two tables, but I've recently been asked to add case conditions. The conditions are working but the problem I'm running into is that each condition creates a duplicate column.
case when s.Department = 'Aero' then '(OA)' else '' end as Blah,
case when s.Department = 'Terrent' then '(OT)' else '' end as Blah,
case when s.Department = 'Vertigo' then '(OMG)' else '' end as Blah
This causes me to end up with
a| b | c | d | Blah | Blah | Blah|
| | | | (OT) | (OA) | (OT)|
| | | | (OT) | | |
| | | | (OT) | (OA) | |
| | | | (OT) | (OA) | (OT)|
| | | | | | (OT)|
How can I use the "case" cmd and have all the results if applicable show up under 1 column?
a| b | c | d | Blah |
| | | | (OT) |
| | | | (OT) |
| | | | (OT) |
| | | | (OT) |
| | | | (OA) |
| | | | (OA) |
| | | | (OA) |
| | | | (OT) |
| | | | (OT) |
| | | | (OT) |
You would use one case statement instead of three:
(case when s.Department = 'Aero' then '(OA)'
when s.Department = 'Terrent' then '(OT)'
when s.Department = 'Vertigo' then '(OMG)'
else ''
end) as Blah