Compare with Date and ID problem - sql

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.

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

Select Date with Specific Time SQL

I have the following table:
oDate oTime oAct
--------------------------------------
2017-06-01 00:00:00 A
2017-06-01 01:00:00 B
2017-06-01 02:00:00 C
ff.
2017-06-02 00:00:00 B
ff.
I want to select a day before (only after 21:00:00) and after.
Let say, If I Select '2017-06-02' then the result should be:
oDate oTime oAct
--------------------------------------
2017-06-01 22:00:00 A
2017-06-02 00:00:00 B
2017-06-02 01:00:00 C
ff.
2017-06-03 00:00:00 C
ff.
Also, for the query. I only have one parameter, which is #oDate date.
Please advise.
Thank you.
If I follow your question correctly I think you are after a where clause such as this:
select
*
from YourTable
where (
oDate > '20170602'
OR
(oDate = '20170602' AND oTime >= '21:00:00')
)
This would give you all dates after 2017-06-02 as well as times on that date at and after 21:00
To make best use of indexes that may exist on those columns I suggest you do not try to combine the date with time such as this dateadd(day,datediff(day,0,oDate),oTime) and then try to filter >= '20170602 21:00:00' as that would produce table scanning.
perhaps this will help
select
*
from YourTable
where (
oDate > #dateparameterhere
OR
(oDate = #dateparemterhere AND oTime >= '21:00:00')
)
Try this
concatenating fields to make a datetime field
select
* from table
where
cast (cast(odate as varchar(max)) + ' ' + cast(otime as varchar(max)) as datetime) < cast('2017-06-02 21:00:00' as datetime)

Oracle SQL query about Date

I have a database table named availableTimeslot with fields pk, startDate, endDate, e.g.
PK startDate endDate
1. 2017-03-07 09:00:00 2017-03-07 18:00:00
2. 2017-03-07 18:00:00 2017-03-07 21:00:00
3. 2017-03-08 09:00:00 2017-03-08 18:00:00
records starting from 09:00:00 to 18:00:00 indicate it is a morning time slot, while 18:00:00 to 23:00:00 indicating it is a afternoon time slot
storing available timeslot dates (e.g. 2017-03-06, 2017-03-08) which are available for the customer to choose one.
Can I use one query to get exactly 10 available time slots dates starting on the day after the order date?
e.g. if I order a product on 2016-03-07, then the query returns
2017-03-08 09:00:00
2017-03-08 18:00:00
2017-03-09 09:00:00
2017-03-09 18:00:00
2017-03-10 ...
2017-03-11 ...
2017-03-13 ...
as 12 is a public holiday and not in the table.
In short, it returns 10 dates (5 days with each day having am and pm sessions)
remark: the available time slot dates are in order, but may not be consecutive
select available_date
from ( select available_date, row_number() over (order by available_date) as rn
from your_table
where available_date > :order_date
)
where rn <= 5;
:order_date is a bind variable - the date entered by the user/customer through the interface.
Do you want 5 for a single customer?
select ts.*
from (select ts.*
from customer c join
timeslots ts
on ts.date > c.orderdate
where c.customerid = v_customerid
order by ts.date asc
) ts
where rownum <= 5

SQL "transform" query

I have these data on a table (using SQL Server 2005):
ID ParentID StartTime EndTime
77 62 08:00:00 11:00:00
78 62 12:00:00 15:00:00
79 62 18:00:00 22:00:00
and I want to transform it into this:
ParentID BreakfastStart BreakfastEnd LunchStart LunchEnd DinnerStart DinnerEnd
62 08:00:00 11:00:00 12:00:00 15:00:00 18:00:00 22:00:00
Now the hard part is: assume I have no other data field specifying which record is breakfast, lunch or dinner. I want to associate them with lowest start time, i.e., the lower start time will be breakfast, next lower will be lunch and the higher will be dinner (assume all three (and only three) records are always filled).
Any ideas?
WITH q AS
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY parentID ORDER BY StartTime) AS rn
FROM mytable
)
SELECT qb.ParentID,
qb.StartTime AS BreakfastStart, qb.EndTime AS BreakfastEnd,
ql.StartTime AS LunchStart, ql.EndTime AS LunchEnd,
qd.StartTime AS DinnerStart, qd.EndTime AS DinnerEnd
FROM q qb
LEFT JOIN
q ql
ON ql.parentID = qb.parentID
AND ql.rn = 2
LEFT JOIN
q qd
ON qd.parentID = qb.parentID
AND qd.rn = 3
WHERE qb.rn = 1

Compare with Dates and ID?

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
*/