SQL select non null value - mysql

Customer_name Customer_street Customer_city
Lucy Spring Pittsfield
Lucy Spring Pittsfield
Brooks Senator Brooklyn
James North Rye
Vicky Sand Hill Woodside
Lutz 3399 Rice St null
I am practicing on some command of mysql. I have a customer table above. I want to list customer city names from that table but list only those non-null city names. I tried
select customer_city
from customer
WHERE customer_city IS NOT NULL;
The result still list the null city. Am I missing something? Thank you for your help;

Related

SQL: how to select where one column does not match another column for ALL records within a given group

I have a table named sales in a MySQL database that looks like this:
company manufactured shipped
Mercedes Germany United States
Mercedes Germany Germany
Mercedes Germany United States
Toyota Japan Canada
Toyota Japan England
Audi Germany United States
Audi Germany France
Audi Germany Canada
Tesla United States Mexico
Tesla United States Canada
Tesla United States United States
Here is a Fiddle: http://www.sqlfiddle.com/#!17/145ff/3
I would like to return the list of companies that ship ALL of their products internationally (that is, where the value in the manufactured column differs from the value in the shipped column for ALL records of a particular company).
Using the example above, the desired result set would be:
company
Toyota
Audi
Here is my (hackish) attempt:
WITH temp_table AS (
SELECT
s.company AS company
, SUM(CASE
WHEN s.manufactured != s.shipped THEN 1
ELSE 0
END
) AS count_international
, COUNT(s.company) AS total_within_company
FROM
sales s
GROUP BY
s.company
)
SELECT
company
FROM
temp_table
WHERE count_international = total_within_company
Essentially, I count the instances where the columns do not match. Then I check whether the sum of those mismatched instances matches the number of records within a given group.
This approach works, but it's far from an elegant solution!
Can anyone offer advice as to a more idiomatic way to implement this query?
Thanks!
We can GROUP BY company and use a HAVING clause to say all countries in shipped must differ to the country in manufactured:
SELECT company
FROM sales
GROUP BY company
HAVING COUNT(CASE WHEN manufactured = shipped THEN 1 END) = 0;
Try out here: db<>fiddle
The fiddle linked in the question is a Postgres DB, but MySQL is taged as DBMS.
In a MySQL DB, the above query can be simplified to:
SELECT company
FROM sales
GROUP BY company
HAVING SUM(manufactured = shipped) = 0;
In a Postgres DB, this is not possible.
You have to think in sets... you want to display all without a match -- find the matches display the rest
SELECT DISTINCT company
FROM sales
WHERE company NOT IN (
SELECT company
FROM sales
WHERE manufactured = shipped
)

Assign correct Responsible person from JSON format column based on Service Date

P.S. previous post was deleted, I hope this time it will reach more people.
I have huge history dataset and I need to assign value (ResponsibleName - from JSON format column) to a new 'Responsible' column based on Service Date for all rows associate with Client.
Each Client can have unique Responsible person (JSON column) for the specific date range:
If Codes Column contains Supervisor, then assign this Employee to only associated row as Responsible.
And finally, if Codes Column contains Employee, than assign associated Responsible person from the JSON column (for specific date range) to Responsible
I don't have issues with the first step, however I can't find a solution to implement the 2nd (last) statement.
Original History Table Joined with Client Table:
SELECT h.Id,
h.ServiceDate
h.ClientId,
cl.ClientName,
h.EmployeeName,
cl.ResponsibleJSON,
h.Codes
FROM History AS h
JOIN ClientTable AS cl
ON (h.ClientId = cl.ClientId)
Output of that table:
Id
ServiceDate
ClientId
ClientName
EmployeeName
ResponsibleJSON
Codes
1
2020-05-06
123
John Smith
Chris Evans
[{"ResponsibleName":"Kevin Costner","ResponsibleStartDate":"2019-02-14","ResponsibleEndDate":"2020-05-31"},{"ResponsibleName":"Tom Cruise", "ResponsibleStartDate":"2020-06-01","ResponsibleEndDate":null}]
Employee, Office
2
2020-05-08
123
John Smith
Tom Holland
[{"ResponsibleName":"Kevin Costner","ResponsibleStartDate":"2019-02-14","ResponsibleEndDate":"2020-05-31"},{"ResponsibleName":"Tom Cruise", "ResponsibleStartDate":"2020-06-01","ResponsibleEndDate":null}]
Supervisor, Remote
3
2020-05-11
123
John Smith
Chris Evans
[{"ResponsibleName":"Kevin Costner","ResponsibleStartDate":"2019-02-14","ResponsibleEndDate":"2020-05-31"},{"ResponsibleName":"Tom Cruise", "ResponsibleStartDate":"2020-06-01","ResponsibleEndDate":null}]
Employee, Office
4
2020-05-15
123
John Smith
Thomas Anderson
[{"ResponsibleName":"Kevin Costner","ResponsibleStartDate":"2019-02-14","ResponsibleEndDate":"2020-05-31"},{"ResponsibleName":"Tom Cruise", "ResponsibleStartDate":"2020-06-01","ResponsibleEndDate":null}]
Employee, Office
5
2020-06-10
123
John Smith
Tom Holland
[{"ResponsibleName":"Kevin Costner","ResponsibleStartDate":"2019-02-14","ResponsibleEndDate":"2020-05-31"},{"ResponsibleName":"Tom Cruise", "ResponsibleStartDate":"2020-06-01","ResponsibleEndDate":null}]
Supervisor, Office
6
2020-06-17
123
John Smith
Thomas Anderson
[{"ResponsibleName":"Kevin Costner","ResponsibleStartDate":"2019-02-14","ResponsibleEndDate":"2020-05-31"},{"ResponsibleName":"Tom Cruise", "ResponsibleStartDate":"2020-06-01","ResponsibleEndDate":null}]
Employee, Remote
7
2020-06-22
123
John Smith
Elon Mask
[{"ResponsibleName":"Kevin Costner","ResponsibleStartDate":"2019-02-14","ResponsibleEndDate":"2020-05-31"},{"ResponsibleName":"Tom Cruise", "ResponsibleStartDate":"2020-06-01","ResponsibleEndDate":null}]
Employee, Remote
8
2020-07-01
123
John Smith
Tom Holland
[{"ResponsibleName":"Kevin Costner","ResponsibleStartDate":"2019-02-14","ResponsibleEndDate":"2020-05-31"},{"ResponsibleName":"Tom Cruise", "ResponsibleStartDate":"2020-06-01","ResponsibleEndDate":null}]
Supervisor, Remote
I've created the table with all JSON data for all Clients:
SELECT c.ClientId,
c.ClientFullName,
c.ResponsibleJSON,
JSON_VALUE(X.VALUE,'$.ResponsibleName') AS ResponsibleName,
JSON_VALUE(X.VALUE,'$.ResponsibleStartDate') AS ResponsibleStartDate,
ISNULL(JSON_VALUE(X.VALUE,'$.ResponsibleEndDate'), '2999-12-31') AS ResponsibleEndDate
FROM ClientTable AS c
CROSS APPLY OPENJSON(c.ResponsibleJSON) AS X
ORDER BY c.ClientFullName, ResponsibleStartDate
And now I need to combine them somehow to produce column below:
Desired Output:
Responsible
Kevin Costner
Tom Holland
Kevin Costner
Kevin Costner
Tom Holland
Tom Cruise
Tom Cruise
Tom Holland
Code I need help with you can find below.
I don't know if I can OUTER APPLY in CASE statement AND I think JOIN should be ON ClientId and check if ServiceDate between ResponsibleStartDate AND ResponsibleEndDate.
SELECT h.Id,
h.ServiceDate
h.ClientId,
cl.ClientName,
h.EmployeeName,
cl.ResponsibleJSON,
h.Codes,
CASE
WHEN h.Codes LIKE '%Supervisor%' THEN h.EmployeeName --- 1st statement
WHEN --- here should be the second statement. I don't know how to implement it.
END AS Responsible
FROM History AS h
JOIN ClientTable AS cl
ON (h.ClientId = cl.ClientId)
It seems you only want to lookup the JSON when the Codes column does not contain Supervisor. The following query should do what you want:
SELECT
c.Id,
c.ServiceDate,
c.ClientId,
c.ClientName,
c.EmployeeName,
c.Codes,
Supervisor =
CASE WHEN c.Codes LIKE '%Supervisor%' THEN c.EmployeeName
ELSE (
SELECT TOP (1)
j.ResponsibleName
FROM OPENJSON(c.ResponsibleJSON)
WITH (
ResponsibleName nvarchar(100),
ResponsibleStartDate date,
ResponsibleEndDate date
) j
WHERE c.ServiceDate >= j.ResponsibleStartDate
AND (j.ResponsibleEndDate IS NULL OR c.ServiceDate <= j.ResponsibleEndDate)
)
END
FROM ClientTable c;
db<>fiddle

How to find the Employee names and their supervisor names if the table doesn't have common numeric column like Employee_id or employee number in Mysql

If the table have only two columns with employee name and their supervisor column and if it doesn't have any other numeric or number column with employee_number or employee_id, then how the results can be produced. I'm not getting logic to show the results.
Code for creating table in Mysql:
CREATE TABLE DATABASE_TABLE
(
Employee_Name nvarchar(255) PRIMARY KEY,
Supervisor_Name nvarchar(255) NOT NULL
);
CREATE INDEX ix_database_table_supervisor
ON DATABASE_TABLE (Supervisor_Name);
INSERT INTO DATABASE_TABLE
(Employee_Name, Supervisor_Name) VALUES
('Alice','Dave'), ('Olive','Dave'), ('Barton','Dave')
, ('Almira','Jacob'), ('Charles','Jacob'), ('Davis','Jacob')
, ('Robert','Risha'), ('Peter','Risha'), ('Ethel','Risha')
, ('Isaac','Jospeh'), ('Sophia','Jospeh'), ('Rosa','Jospeh')
, ('Joshua','Dandy'), ('Silas','Dandy'), ('Fred','Dandy')
, ('Frank','Andrew'), ('Howard','Andrew'), ('Ralph','Andrew')
, ('Dennis','Henry'), ('Alex','Henry'), ('Floyd','Henry')
, ('Carlos','Nelson'), ('Homer','Nelson'), ('Harold','Nelson')
, ('Leo','Simon'), ('Warren','Simon'), ('Clifford','Simon')
, ('Martha','Casper'), ('Hazel','Casper'), ('Irene','Casper')
, ('Dave','Betsy'), ('Jacob','Betsy'), ('Risha','David')
, ('Jospeh','David'), ('Dandy','Phillip'), ('Andrew','Phillip')
, ('Henry','Harvey'), ('Nelson','Harvey'), ('Simon','Paul')
, ('Casper','Paul'), ('Betsy','Joe'), ('David','Joe')
, ('Phillip','Joe'), ('Harvey','Joe'), ('Paul','Joe')
It's output is:
Employee_name Supervisor_name
Frank Andrew
Howard Andrew
Ralph Andrew
Dave Betsy
Jacob Betsy
Hazel Casper
Irene Casper
Martha Casper
Fred Dandy
Joshua Dandy
Silas Dandy
Alice Dave
Barton Dave
Olive Dave
Jospeh David
Risha David
Henry Harvey
Nelson Harvey
Alex Henry
Dennis Henry
Floyd Henry
Almira Jacob
Charles Jacob
Davis Jacob
Betsy Joe
David Joe
....
The result should be in the lower to higher level of hierarchy like:
Employee_Name Supervisor_Name Higher_Supervisor Next_higher_Supervisor
Frank Andrew Phillip Joe
Howard Andrew Phillip Joe
Ralph Andrew Phillip Joe
Dave Betsy Joe no_supervisor
Jacob Betsy Joe no_supervisor
Hazel Casper Paul Joe
Irene Casper Paul Joe
Martha Casper Paul Joe
For Eg: Frank's supervisor is Andrew, Andrew's supervisor is Phillip, Phillip's supervisor is Joe
For Eg: Dave's supervisor is Betsy, Betsy's supervisor is Joe, and Joe doesn't have any supervisor so no_supervisor should be displayed.
For Eg: Hazel's supervisor is Casper, Casper's supervisor's is Paul, and Paul's Supervisor is Joe should be displayed in the order format
For this particular set of data, it can LEFT JOIN the table itself to get the expected results
SELECT a.Employee_Name, a.Supervisor_Name, b.Supervisor_Name, c.Supervisor_Name
FROM DATABASE_TABLE a
LEFT JOIN DATABASE_TABLE b ON a.Supervisor_Name = b.Employee_Name
LEFT JOIN DATABASE_TABLE c ON b.Supervisor_Name = c.Employee_Name
If hierarchy depth is unknown, which means the number of columns is unknown, it's more complicated. It is still possible by using recursive CTE to find the depth and generate dynamic SQL.

Getting counts of total substring matches from a full-text field in one table to another full-text field in a different table

I have a table loaded with text strings that i need to do a substring match on another text string field in another table and then spit out the counts
CREATE TABLE `subdiv_names` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`names` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
)
and
CREATE TABLE 'property' (
...
'subdiv_name' varchar(70)
)
i tried
SELECT count(*) AS count,
SUBSTRING(subdiv_names.subdiv, wp_realty_listingsdb.Subdivision) AS mtch
FROM wp_realty_listingsdb,subdiv_names
GROUP BY mtch
ORDER BY count DESC
but no of course.
some data subdiv_names.name
700 Ocean Drive
Barclay Condo Apts
Bay Colony
Coral Bay
...
and property.subdiv_name
Palm Beach Barclay Condo Apts Est 2
Old Jupiter Beach Road 700 Ocean Drive
Old Jupiter Beach Road 700 Ocean Drive
Palm Beach Barclay Condo Apts Est 2
Golfview Hgts 8 In Pb 24 Pgs 103 And 104
Bay Colony Heights
Paradise Cove At Palm Beach Lakes Condo
Palm Beach Barclay Condo Apts Est 2
WESTWOOD GARDENS
...
results desired
2 Old Jupiter Beach Road 700 Ocean Drive
3 Palm Beach Barclay Condo Apts Est 2
1 Bay Colony Heights
0 Coral Bay
...
You can do this using like (or various other functions):
select p.subdivis, count(*)
from props p inner join
subdiv_names n
on concat(',', p.subdivis, ',') like concat('%,', n.subdiv, ',%')
group by p.subdivis;
I can't quite tell which set of data corresponds to which table. You might need to swap the table names.
If i understand the question correct then i wouldn't be needing substring. I think LIKE will do the trick.
IF you can put snapshot of your expectation we can try for you or elaborate the question further.

Count number of times value appears in column in MySQL

Tried looking everywhere for this but my searching hasn't given me any answers.
Looking to count how many times the word 'Sydney' comes up in the column 'Suburb'
CustomerId FirstName LastName StreetAddress Suburb PostCode
C001 James Smith 400 Kent St Sydney 2000
C002 Maria Carpenter 333 Smith St Westmead 2145
C003 Dennis Miller 214 Uni Rd Sydney 2000
I want it to provide an outcome like:
numCustomersFromSydney
2
Use this simple query:
select count(*) as numCustomersFromSydney from table where Suburb = "Sydney";
Use COUNT() and an alias name:
SELECT
COUNT(Suburb) AS numCustomersFromSydney
FROM
yourtable
WHERE
Suburb = 'Sydney'