Compare with Dates and ID? - sql

Using SQL Server 2000
I want to get Table2.TimeIn Table2.TimeOut according to Table1.personid and also If Table1.Date = Table3.Date then it should take a Table3.TimeIn, Table3.TimeOut.
3 Tables
Table1
ID Date
001 20090503
001 20090504
001 20090506
002 20090505
002 20090506
So on…,
Table2
ID TimeIn TimeOut
001 08:00:00 18:00:00
002 08:00:00 21:00:00
So on…,
Table3
ID Date TimeIn TimeOut
001 20090504 10:00:00 22:00:00
001 20090505 17:00:00 23:00:00
002 20090505 12:00:00 21:00:00
So on…,
Select Table1.ID,
Table1.Date,
Table2.TimeIn,
Table2.TimeOut
from Table1
Inner Join Table2 on Table1.ID = Table2.ID
If Table1.Date = Table3.Date then it should take Table3.TimeIn, Table3.TimeOut else Table2.TimeIn, Table2.Timeout
Expected Output
ID Date TimeIn TimeOut
001 20090503 08:00:00 18:00:00
001 20090504 10:00:00 22:00:00
001 20090506 08:00:00 18:00:00
002 20090505 12:00:00 21:00:00
002 20090506 08:00:00 21:00:00
So on…,
How to write a query for this condition?

Employee time schedule fallback?:
SELECT Table1.ID
,Table1.Date
,COALESCE(Table3.TimeIn, Table2.TimeIn) AS TimeIn
,COALESCE(Table3.TimeOut, Table2.TimeOut) AS TimeOut
FROM Table1
INNER JOIN Table2 -- Always have an expected schedule for an employee
ON Table1.ID = Table2.ID
LEFT JOIN Table3 -- May.may not have an actual schedule for an employee
ON Table3.ID = Table1.ID
AND Table3.Date = Table1.Date
/*
ORDER BY Table1.ID
,Table1.Date
*/

Related

SQL how to "join" two tables

can't imagine simple solution. I've two tables.
table 1 (about 300 rows)
id
name
time
ID1
peter
12:00:00
ID2
alice
12:33:00
ID3
tom
08:00:00
table 2 (about 3'000'000 rows)
id
time
arg1
ID1
12:00:00
23
ID1
11:00:00
34
ID2
12:45:00
21
ID2
12:33:00
22
ID2
08:00:00
12
ID3
08:00:00
21
ID1
08:00:00
23
need output table like this
id
name
time
arg1
ID1
peter
12:00:00
23
ID2
alice
12:33:00
22
ID3
tom
08:00:00
21
Select t1.ID, t1.time, t1.name, t2.arg1 from table1 t1
Inner join table2 t2 on t2.id = t1.id
Where t1.time=t2.time

Date and value conversion

I'm Inserting the ID's from table2 which is not exist in the table1. Table1 have perfect datatype in date(datetime), Gen(varchar(1)) but Table2 has different datatype for the same column date(varchar(255)), Gen(float) 1-M,2-F.
I share the problem in sample set.
Table1
ID date Gen
193 1996-03-26 00:00:00 M
446 1997-09-20 00:00:00 F
689 1997-02-21 00:00:00 F
612 1993-10-19 00:00:00 M
Table2
ID date Gen
123 1993-03-02 00:00:00 1
456 2019-10-19 11:50:13.913 2
689 1997-02-21 00:00:00 2
789 2019-11-04 08:06:36.71 1
012 2000-10-02 07:11:19 1
I need to append the new ID's in table1. while using the insert query how can I convert the date and Gen variable like table1 format.
Result:
Table1
ID date Gen
193 1996-03-26 00:00:00 M
446 1997-09-20 00:00:00 F
689 1997-02-21 00:00:00 F
612 1993-10-19 00:00:00 M
123 1993-03-02 00:00:00 M
456 2019-10-19 00:00:00 F
789 2019-11-04 00:00:00 M
012 2000-10-02 00:00:00 M
If you want to insert the rows in table2 that are not in table1, you can use insert with filtering logic:
insert into table1 (id, date, gen)
select t2.id, t2.date, (case when gen = 1 then 'M' else 'F' end)
from table2 t2
where not exists (select 1 from table1 t1 where t2.id = t1.id);

Access query to return records not found between date range

I have the following sample data
Table1
REF_NO SUPP_START_DATE SUPP_END_DATE
123 01/01/2018 31/12/2018
456 01/01/2017 31/12/2017
789 01/01/2016 31/12/2016
Table2
REF_NO CHG_START_DATE CHG_END_DATE
123 01/03/2018 31/03/2018
123 01/04/2018 30/04/2018
456 01/02/2018 28/02/2018
456 01/01/2017 31/01/2017
789 01/07/2016 31/07/2016
I'd like to know if it is possible to in Access SQL to return all charges (table2) that do not fall between the start and end dates of table1. So, with the sample data above, the following would be returned :-
Results
REF_NO CHG_START_DATE CHG_END_DATE
456 01/02/2018 28/02/2018
I know how to join the 2 tables by using
SELECT table1.ref_no, table2.CHG_START_DATE, table2.CHG_END_DATE
FROM table1
LEFT JOIN table2 ON table1.ref_no = table2.ref_no
but I'm not sure how to cater for the date mismatches
If it is sufficient to just look at the start and end dates:
select t2.*
from table2 t2
where not exists (select 1
from table1 as t1
where t1.refno = t2.refno and
t2.CHG_START_DATE between UPP_START_DATE and SUPP_END_DATE
) or
not exists (select 1
from table1 as t1
where t1.refno = t2.refno and
t2.CHG_END_DATE between UPP_START_DATE and SUPP_END_DATE
) ;
Here's an alternative approach using joins instead of correlated subqueries:
select t2.* from table1 t1 inner join table2 t2 on t1.ref_no = t2.ref_no
where not
(
(t2.chg_start_date between t1.supp_start_date and t1.supp_end_date) and
(t2.chg_end_date between t1.supp_start_date and t1.supp_end_date)
)

T-SQL max date and min date between two date

First, thanks for your time and your help!
I have two tables:
Table 1
PersId name lastName city
---------------------------------------
1 John Smith Tirana
2 Leri Nice Tirana
3 Adam fortsan Tirana
Table 2
Id PersId salesDate
--------------------------------------------
1 1 2017-01-22 08:00:40 000
2 2 2017-01-22 09:00:00 000
3 1 2017-01-22 10:00:00 000
4 1 2017-01-22 20:00:00 000
5 3 2017-01-15 09:00:00 000
6 1 2017-01-21 09:00:00 000
7 1 2017-01-21 10:00:00 000
8 1 2017-01-21 18:55:00 000
I would like to see the first recent sales between two dates according to each city for each day I want to bring it empty if I do not have a sale
SalesDate > '2017-01-17 09:00:00 000'
and SalesDate < '2017-01-23 09:00:00 000'
Table 2, id = 5 because the record is not in the specified date range
If I wanted my results to look like
Id PersId MinSalesDate MaxSalesDate City
-----------------------------------------------------------------------------
1 1 2017-01-22 08:00:40 000 2017-01-22 20:00:00 000 Tirana
2 2 2017-01-22 09:00:00 000 null Tirana
3 3 null null Tirana
4 1 2017-01-21 09:00:00 000 2017-01-21 18:55:00 000 Tirana
You dont identify how to get ID in the result. You appear to just want Row_Number(). I will leave that out, but this should get you started. You may have to work out conversion issues in the data range check, and I havent checked the query for syntax errors, I will leave that to you.
Select T1.PersId, City
, Min(T2.salesDate) MinSalesDate
, Max(T2.salesDate) MaxSalesDate
From Table1 T1
Left Join Table2 T2
On T1.PersId = T2.PersId
And T2.salesDate Between '2017-01-17 09:00:00 000' And < '2017-01-23 09:00:00 000'
Group BY T1.PersId, T2.City
Try the following using row_number to get min and max sale dates:
SELECT
T2.Id, T1.PersId, T2.MIN_salesDate, T2.MAX_salesDate, T1.City
FROM Table1 T1
LEFT JOIN
(
SELECT MIN(Id) as Id, PersId, MIN(salesDate) as MIN_salesDate, MAX(salesDate) as MAX_salesDate
FROM
(
SELECT
*
,ROW_NUMBER() OVER (PARTITION BY PersId ORDER BY salesDate ASC) as RNKMIN
,ROW_NUMBER() OVER (PARTITION BY PersId ORDER BY salesDate DESC) as RNKMAX
FROM Table2 T2
WHERE salesDate Between '2017-01-17 09:00:00 000' And '2017-01-23 09:00:00 000'
) temp
WHERE RNKMIN = 1 or RNKMAX = 1
GROUP BY PersId
) T2
on T1.PersId = T2.PersId

Compare with Date and ID problem

Table1
ID Date Intime Outtime
A001 20000501 12:00:00 22:00:00
A001 20000502 14:00:00 22:00:00
A001 20000503 12:00:00 23:00:00
A002 20000501 11:00:00 20:00:00
A002 20000502 13:00:00 21:00:00
So on…,
Table2
ID Date Intime Outtime
A001 20050501 14:00:00 23:00:00
A002 20050501 08:00:00 16:00:00
From the above table
I want to take Table1.ID, Table1.Date, Table2.Intime, Table2.Outtime from Table1 Inner Join Table2 on Table1.ID = Table2.ID and Table1.Date = Table2.Date
Getting Duplicated values
ID Date Intime Outtime
A001 20000501 14:00:00 23:00:00
A001 20000501 18:00:00 16:00:00
A002 20000501 14:00:00 23:00:00
A002 20000501 18:00:00 16:00:00
I tried Left outer Join also. It was showing a same. How to compare the id and date.
Need query Help?
If you do an inner join, you'll only get those rows that are present in both tables (in terms of their ID and Date):
SELECT
Table1.ID, Table1.Date,
Table2.Intime, Table2.Outtime
FROM Table1
INNER JOIN Table2 ON Table1.ID = Table2.ID AND Table1.Date = Table2.Date
ID Date InTime OutTime
A001 20000501 14:00:00 23:00:00
A002 20050501 08:00:00 16:00:00
If you're not getting this, then there's a problem in your data - as I've already mentioned in a previous answer to a previous question.
Check the output of this query:
SELECT * FROM Table2 WHERE ID = 'A001' AND Date = '20000501'
I bet you get more than one row.....
Marc
Trying to explain further - I still think you're misunderstanding the INNER JOIN or you're trying to accomplish something that can't really be done easily.
Your output is something like this:
ID Date Intime Outtime
A001 20000501 14:00:00 23:00:00
A001 20000501 18:00:00 16:00:00
A002 20000501 14:00:00 23:00:00
A002 20000501 18:00:00 16:00:00
If you truly get this output from your INNER JOIN, then this means:
you most like have one row with ID=A001 and Date=20000501 in your Table1
you have two (or more) rows in your Table2 with ID=A001 and Date=20000501
What the INNER JOIN will do is combine row#1 from Table2 with the single row from Table1, and then row#2 from Table2 with the single row from Table1, and so on.
If you have multiple entries with the same (ID,Date) values in Table2, you will get duplicates with an INNER JOIN - this is by design, and is not an error - but just the way the INNER JOIN works.