The problem is that when I run the query I got an empty table which is wrong base on the data I have, so is my logic wrong or I'm missing something?
Based on the provided tables write a query in HIV SQL to answer question 1
employees ( employee_num , last_name , first_name , status , hire_date , last_date_worked , job_title , job_code , home_branch )
transactions ( branch_num , contract_num , customer_num , invoice_date ,invoice_num , product_num , sales_amount , employee_num , service_date , system_period )
Question 1:
Show the employees who serviced customers before 1/1/2005. Employee
names must be concatenated to produce this format: LastName,
FirstName. For example, “Doe, John”. The result should have two
columns: employee name and service date.
My answer:
SELECT concat(e.first_name, ', ', e.last_name) AS employee_name, t.service_date FROM employees e
FULL OUTER JOIN transactions t
ON e.employee_num = t.employee_num WHERE t.service_date < '1/1/2005';
Related
I have 3 different tables where I want to be able to combine them and then filter them based off of 4 different inputs OR LESS on a search. I also would like to have it work when some filters are none.. Here is the breakdown what I mean:
So above there are three tables with column names and some data. NOTE: Table 1 has a comma in the location and I will want to separate those as two separate column for searching purposes which I figured out how to do.
Now What the final table I want is:
Table 1
------------------------------------------------
date city state title summary
jan 1 stl MO Book
jan 1 ATL MO Comp
Jan 1 Maine Phone box device
March2 MO Howey weird name
JUL2 cheese
Now My goal is trying to do a text search such as what follows: The 4 criteria of filtering will be State, Title, Date, OR a keyword
I think the solution has involves joining the tables, splitting the comma, using "where true" and having some type of IF statement.
select
date1,
split_part(location1, ',', 1) as city,
split_part(location1, ',', 2) as state,
title1
from
table1
Now I use join or union or something to add other tables?
Note: Null means user didn't have input at all
Example 1: If Date is Jan 1, state is null, title is null, Keyword is null (it should show everything with jan1st)
Example 2: If Date is Jan 1 AND City is stl (show first row), title and keyword is null
Example 3: If Date = Jan 1 and State is MO (Show row 1,2, and 4), title and keyword is null
Last example: if Keyword is Phone and everything else is null it shows row 3.
Is there a way to do this in Postgressql? It also would be convenient if the user didnt spell a state right but still pulled up similar results. Basically I will have a interface where a user will select a date, or title and/or state, or maybe just use a key word to filter the results
Your first task is to build a combined view of the tables involved supplying null for each that does not exit in a particular table. The following uses a CTE consisting of union of the 3 tables to accomplish that.
with combined as
(Select date1 as con_date
, split_part(location1, ',', 1) as city
, split_part(location1, ',', 2) as state
, title1 as title
, null as summary
from table_1
union all
select date2
, state
, null
, title2
, null
from table_2
union all
select date3
, null
, null
, title
, null
from table_3
)
select * from combined;
From there you can build a VIEW using the above then construct an appropriate query for the entered parameters. An alternative is a function that returns a table as below.
create or replace function search_combined(
search_date_in date default null
, search_title_in text default null
, search_state_in text default null
, search_word_in text default null
)
returns table (con_date date
, city text
, state text
, title text
, summary text
)
language sql
as $$
with combined as
(Select date1 as con_date
, split_part(location1, ',', 1) as city
, split_part(location1, ',', 2) as state
, title1 as title
, null as summary
from table_1
union all
select date2
, state
, null
, title2
, null
from table_2
union all
select date3
, null
, null
, title
, null
from table_3
)
select *
from combined
where 1=1
and (con_date = search_date_in or search_date_in is null)
and (state = search_state_in or search_state_in is null)
and (title = search_title_in or search_title_in is null)
and (title = search_word_in or search_word_in is null);
$$;
-- Test
select search_combined(search_date_in => date '2019-01-01');
or
select *
from search_combined(search_date_in => date '2019-01-01');
select *
from search_combned(search_date_in => date '2019-01-01'
,search_state_in => 'Mo'
);
select *
from search_combined( search_word_in => 'Phone');
The above should work as is, but I have not tested it as your sample data is a picture. In the future please provide sample data as text; as that allows copy/past to build it locally.
Need to get order qty of the minimum ADATE
Im using below query and getting 12 records. Now I want to select orderqty of minimum ADATE which is 06-NOV-2018(2018-11-06).
For every customer(will get multiple records), i need to get the Order_Qty of minimum ADATE column.
select
Customer ,
OrderID ,
LocationID ,
Order_Qty,Sent_date ,ADATE
from
(
select
OrderID ,
LocationID ,
Sent_date ,
Order_Qty ,
Customer ,
TimeStampA
from ARC_TBL
)
obn
inner join
(
select
ADATE ,TimeStampA
from trackTBL snt
)snt
on obn.TimeStampA = snt.TimeStampA
where Customer='ABC' and OrderID='XYZ100' and Sent_date='2018-11-18' and LocationID='250';
SELECT QTY, ADATE
FROM table
ORDER BY ADATE
FETCH FIRST 1 ROW ONLY
Explain your question in more detail and you will get better answers.
I have a table employees with columns:
company_id,
id,
opt_state (ceased_membership, ignition, opted_out, opted_in),
opt_out_on.
I want to query all companies where all employees opt-state is in ('ceased_membership', 'ignition', 'opted_out') and the date opt_out_on when last employee left.
I have tried this but it didn't work
select company_id from employees where id=all(select id from
employees
where opt_state in ('ceased_membership', 'ignition','opted_out')
Then I wrote this query below, which worked very well and gave me the resolution I was looking for. However, I'd like to ask here if this can be done differently, more elegantly.
SELECT
e.company_id
, max_opt_out
FROM (
SELECT DISTINCT
company_id
, count(id)
OVER (
PARTITION BY company_id ) opt_out
FROM employees
WHERE opt_state IN ('ceased_membership', 'ignition', 'opted_out')) e
LEFT JOIN (
SELECT
company_id
, count(id) opt_in
, max(opt_out_on) max_opt_out
FROM employees
GROUP BY company_id) S
ON e.company_id = s.company_id
WHERE e.opt_out = s.opt_in;
This seems like a good time to use the HAVING clause
SELECT company_id, max(opt_out_on)
FROM employees e
GROUP BY company_id
HAVING bool_and( opt_state in ('ceased_membership', 'ignition','opted_out'));
HAVING in a bit like a WHERE but the condition apples to whole GROUPS
bool_and is an agregate function that is only true when all the records in the group are result in true.
I'd say that you want to query a maximum out_out_on for each company that only have employees in a set of states, which means that do not have any employee not in a set of states.
So, translated to SQL:
select company_id, max(opt_out_on)
from employees e
where not exists(
select 1 from employees
where company_id=e.company_id
and opt_state not in ('ceased_membership', 'ignition','opted_out')
)
group by company_id;
I have a table Employee with primary key as Employee_id and other table for email address which has multiple email address for one employee. I need to get all employees from Table 1 with logic that if one email address exists then it is pulled from Table "Email" , if more than one then null.
Table 1 data
I tried to write it as below query and it is not working. Can anyone please help. ALl your inputs are greatly appreciated.
SELECT employee_id,
CASE WHEN count(adr.email_addr) =1 and count(dupes.email_addr)=1 and adr.email_addr=dupes.email_addr
then adr.email_addr
else null END email_address
FROM Employee Emp
LEFT OUTER JOIN
(SELECT employee_id, email_addr, count(*) qty
FROM Emailadress
HAVING count(*) > 1)
dupes
ON EMP.employee_id = dupes.employee_id
group by emp.employee_id, adr.email_addr
I am using impala interface to execute.
Thanks,
Manju
I have a query like
UPDATE PRD_PRODUCT_L10N ppl
SET ( CATCHING_PHRASE
, GENERIC_NAME
, INGREDIENTS
, QUANTITY
, DOSE
, NUTRITION_FACTS
, PRODUCT_DESCRIPTION
, PROMOTION_MESSAGE
, MESSAGE
) = (
SELECT distinct CATCHING_PHRASE
, GENERIC_NAME
, INGREDIENTS
, QUANTITY
, DOSE
, NUTRITION_FACTS
, PRODUCT_DESCRIPTION
, PROMOTION_MESSAGE
, MESSAGE
FROM TEMP_UPLOAD_PRODUCT_ATTRIBUTES tupa
INNER JOIN
PRD_PRODUCT pp
ON pp .EISIDENTIFIER = tupa.EISIDENTIFIER
WHERE ppl.PRODUCTGUID = pp.GUID
AND ppl.LOCALEGUID = tupa.LOCALEGUID
)
WHERE EXISTS (
SELECT 0
FROM TEMP_UPLOAD_PRODUCT_ATTRIBUTES tupa
INNER JOIN
PRD_PRODUCT pp
ON pp .EISIDENTIFIER = tupa.EISIDENTIFIER
WHERE ppl.PRODUCTGUID = pp .GUID
AND ppl.LOCALEGUID = tupa.LOCALEGUID
)
the subquery returns more than 1 row and I would like to insert the first selected. How do I do that in DB2 database?
Please advice.
Thanks
Depending on your DB2 version (i think 8 upwards) you can use fetch at your subquery
(select * from table fetch first 1 rows only)
http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2.doc.admin%2Ffrstnrw.htm
Add FETCH FIRST ROW ONLY to your subquery. Search for fetch-first-clause on the page linked for more info. This is for DB2 on Linux/Unix/Windows.
If you're on the Mainframe (v9), then you want this page for more info (or version 10).