For a dummy test, I want to show a list of employees in a web form.
There is a drop down on the web form that contains a short list of departments, like this:
All Depts
Sales Dept
Marketing Dept
Communication Dept
HR Dept
Finance Dept
IT Dept
The drop down item of All Depts has a value of 0.
The following fiddle shows you what I am trying to do:
http://sqlfiddle.com/#!4/59d1f/2
I know I can do this:
IF (deptid = 0) THEN
select firstname, lastname from employees;
ELSE
select firstname, lastname from employees where deptid = :p_deptid
END IF;
But my real situation has a much more convoluted select query that involves joins of multiple tables. So, I don't wanna clutter up my script with repetitive codes.
Can I achieve my goal using CASE WHEN? Or do I have to use dynamic SQL?
Thanks.
SELECT firstname, lastname
FROM employees
WHERE 0 = :p_deptid
OR dept_id = :p_deptid
I have been coding this last problem all day and the file is due by tomorrow for my SQL Server database class.
I'm supposed to create a CTE that will create a temporary table of all female employees’ businessentityids and genders from the Employee table. Then return the businessentityid & gender from the CTE along with the employee first name, employee last name and person type from the person table where the person type is ‘EM’
My code I have simply converted all gender labels to F and changes PersonType to EM, this is almost becoming comical in how many times I have started over from scratch.
WITH cte_name2 AS
(
SELECT
FirstName, LastName, PersonType
FROM
Person.Person
), cte_name1 AS
(
SELECT
Gender, BusinessEntityID
FROM
HumanResources.Employee
)
SELECT
cte_name1_1.BusinessEntityID, cte_name1_1.Gender,
cte_name2_1.FirstName, cte_name2_1.LastName, cte_name2_1.PersonType
FROM
cte_name1 AS cte_name1_1
CROSS JOIN
cte_name2 AS cte_name2_1
WHERE
(NOT (cte_name1_1.Gender LIKE N'M'))
AND (cte_name2_1.PersonType = N'EM')
Can anyone tell me what I did wrong? Thank you.
The correct code is as follows...
WITH cte_name2 AS
(
SELECT
BusinessEntityID, FirstName, LastName, PersonType
FROM
Person.Person
), cte_name1 AS
(
SELECT
Gender, BusinessEntityID
FROM
HumanResources.Employee
)
SELECT
cte_name1_1.BusinessEntityID, cte_name1_1.Gender,
cte_name2_1.FirstName, cte_name2_1.LastName, cte_name2_1.PersonType
FROM
cte_name1 AS cte_name1_1
INNER JOIN
cte_name2 AS cte_name2_1 ON cte_name2_1.BusinessEntityID = cte_name1_1.BusinessEntityID
WHERE
(cte_name1_1.Gender NOT LIKE N'M')
AND (cte_name2_1.PersonType = N'EM')
Turns out I was very close and Jayvee helped me out quite a bit!
Assume we have loaded a flat file with patient diagnosis data into a table called “Data”. The table structure is:
Create table Data (
Firstname varchar(50),
Lastname varchar(50),
Date_of_birth datetime,
Medical_record_number varchar(20),
Diagnosis_date datetime,
Diagnosis_code varchar(20))
The data in the flat file looks like this:
'jane','jones','2/2/2001','MRN-11111','3/3/2009','diabetes'
'jane','jones','2/2/2001','MRN-11111','1/3/2009','asthma'
'jane','jones','5/5/1975','MRN-88888','2/17/2009','flu'
'tom','smith','4/12/2002','MRN-22222','3/3/2009','diabetes'
'tom','smith','4/12/2002','MRN-33333','1/3/2009','asthma'
'tom','smith','4/12/2002','MRN-33333','2/7/2009','asthma'
'jack','thomas','8/10/1991','MRN-44444','3/7/2009','asthma'
You can assume that no two patients have the same firstname, lastname, and date of birth combination. However one patient might have several visits on different days. These should all have the same medical record number.
The problem is this: Tom Smith has 2 different medical record numbers. Write a query that would always show all the patients
who are like Tom Smith – patients with more than one medical record number.
I came up with below query. It works perfectly fine, but wanted to know if there is a better way to write this query using Oracle Analytical function's. Thank you in advance
SELECT a.firstname,
a.lastname,
a.date_of_birth,
a.medical_record_number
FROM data a, data b
WHERE a.firstname = b.firstname
AND a.lastname = b.lastname
AND a.date_of_birth = b.date_of_birth
AND a.medical_record_number <> .medical_record_number
GROUP BY a.firstname,
a.lastname,
a.date_of_birth,
a.medical_record_number
It is possible to do via analytic functions, but whether it's faster than doing the join in your query* or not depends on what data you have. You'd need to test.
with data (firstname, lastname, date_of_birth, medical_record_number, diagnosis_date, diagnosis_code)
as (select 'jane','jones','2/2/2001','MRN-11111',to_date('3/3/2009', 'mm/dd/yyyy'),'diabetes' from dual union all
select 'jane','jones','2/2/2001','MRN-11111',to_date('1/3/2009', 'mm/dd/yyyy'),'asthma' from dual union all
select 'jane','jones','5/5/1975','MRN-88888',to_date('2/17/2009', 'mm/dd/yyyy'),'flu' from dual union all
select 'tom','smith','4/12/2002','MRN-22222',to_date('3/3/2009', 'mm/dd/yyyy'),'diabetes' from dual union all
select 'tom','smith','4/12/2002','MRN-33333',to_date('1/3/2009', 'mm/dd/yyyy'),'asthma' from dual union all
select 'tom','smith','4/12/2002','MRN-33333',to_date('2/7/2009', 'mm/dd/yyyy'),'asthma' from dual union all
select 'jack','thomas','8/10/1991','MRN-44444',to_date('3/7/2009', 'mm/dd/yyyy'),'asthma' from dual),
-- end of mimicking your table and its data
res as (select firstname,
lastname,
date_of_birth,
medical_record_number,
count(distinct medical_record_number) over (partition by firstname, lastname, date_of_birth) cnt_med_rec_nums
from data)
select distinct firstname,
lastname,
date_of_birth,
medical_record_number
from res
where cnt_med_rec_nums > 1;
*btw, the group by in your example query is not necessary; it would make much more sense to switch it out for a distinct - it makes your intent much clearer, since you're wanting to get a distinct set of records.
You can probably simplify the query a bit using a HAVING clause rather than doing a self-join
SELECT a.firstname,
a.lastname,
a.date_of_birth,
MIN(a.medical_record_number) lowest_medical_record_number,
MAX(a.medical_record_number) highest_medical_record_number
FROM data a
GROUP BY a.firstname,
a.lastname,
a.date_of_birth
HAVING COUNT( DISTINCT a.medical_record_number ) > 1
I'm returning the smallest and largest medical record number for each patient here (that's what I'd do if most of the patients with this problem have just two numbers rather than having dozens). You could return just one or you could return a comma-separated list of all the medical record numbers if you'd rather (which would probably make more sense if most of the bad folks have dozens of numbers).
I've a table EMPLOYEE which has columns like these
EmpId FName LName
I have another table ADDRESS which has columns like these
EmpId AddressType Address Phone Email
AddressType column has 2 possible types, Residential and Official and an Emp can have both types of address. I need a query which will join these 2 tables using EmpId. It also needs to fetch one address which has phone not null. If both addresses has phone, then fetch any one, if none has phone, still fetch any one. Please help.
The trick is to first decide which Address would be best for the Employee, based on your Phone-rule. After the prefered Address has been found, indicated by PhonePreference = 1, you can JOIN the correct Address on the Employee.
WITH AddressCTE AS (
SELECT *
, ROW_NUMBER() OVER (
PARTITION BY EmpId
ORDER BY CASE WHEN Phone IS NOT NULL THEN 1 ELSE 2 END, Phone
) PhonePreference
FROM Address
)
SELECT *
FROM Employee E
JOIN AddressCTE A ON E.EmpId = A.EmpId AND A.PhonePreference = 1
I have 3 tables on my SQLite db as follow:
TABLE first_names
id
name
gender
TABLE last_names
id
name
TABLE names
id
first
last
gender
birthday (null)
used (default 0)
Where first and last are unique and also have a index with both being unique on the names table.
In order to fed names table I have used the below query, which worked the first time but after I had added a new name to the last_names and tried to use it again it failed.
INSERT INTO names (first, last, gender)
SELECT f.name AS first, l.name AS last, f.gender AS gender
FROM first_names f
LEFT JOIN last_names l
WHERE f.name NOT IN (SELECT first FROM names) AND
l.name NOT IN (SELECT last FROM names)
Have also tried using (first, last, gender, [birthday], [used]) with values as null AS [birthday], 0 AS [used] but since it birthday is set to null on the table design and used to 0 as default and it worked on the first time didnt think it would be needed nor it made any difference anyway.
I would think it is because of the unique fields however I do have a WHERE to make sure the names are not already in the table names.
I have tried the query using C# and SQLiteAdmin both executing without requesting result or else it doesnt work.
What is wrong with my query and why it only works once ?
Try using:
WHERE NOT EXISTS ( SELECT * FROM names n WHERE n.first=f.name AND n.last=l.name)
You're not checking that the first and last names are on the same record
I haven't run this through a sql command line, but it should work:
INSERT INTO names (first, last, gender)
SELECT f.name AS first, l.name AS last, f.gender AS gender
FROM first_names f
LEFT JOIN last_names l
WHERE f.name NOT IN (SELECT subquery.first FROM (SELECT * FROM names WHERE l.name = last AND f.gender=gender) subquery)
Your query only works once because even if you have added another name to last_names, there is no value in first_names.name which is not present in names.first:
WHERE
f.name NOT IN (SELECT first FROM names) AND
l.name NOT IN (SELECT last FROM names)
All the posible values for f.name are included in SELECT first FROM names
Perhaps you want to join these clauses with an OR, rather than an AND.