Complicated daily automatic insert query in 2 tables - sql

I have the following tables with their columns (only the relevant columns are listed):
Entry
EntryID (int/auto-inc)
EmployeeNumber (int)
JustifyDate (datetime)
EntryDate (datetime)
Employee
EmployeeNumber (int)
CoreHourID (int)
WorkingFromHome (bit/bool)
Hour
EntryID (int)
InHour (datetime)
OutHour (datetime)
CoreHour
CoreHourID (int) - not unique
InHour (int)
OutHour (int)
EDIT: I had forgotten to mention that CoreHourID isn't a unique field, table can look like this:
+-----------+-------+-------+----------+
|CoreHourId |InHour |OutHour|Identifier|
+-----------+-------+-------+----------+
| 2 | 9 | 12 | 1 |
| 2 | 14 | 17 | 2 |
| 3 | 7 | 11 | 3 |
| 3 | 15 | 18 | 4 |
+-----------+-------+-------+----------+
Sorry for the big layout, I really don't know how to properly post table information.
Now here's an attempt at explaining what I'm trying to do:
Every day, a row should be inserted in Entry and Hour for all employees who are WorkingFromHome. In the Entry table it should place the corresponding EmployeeNumber, and for JustifyDate it should add whatever day it is when the job is running. For the EntryDate field it should add that day's date, but the time part should be the InHour from the first corresponding CoreHour row.
For the Hour table it should add the EntryID that just got inserted in the Entry table, and the InHour should be the same as the EntryDate and for the OutHour field it should add a DateTime based on the last OutHour corresponding for the employee's CoreHourID.
I'm struggling a lot, so any help is appreciated.
PS: Any comments/questions regarding my explanation are welcome and I will gladly respond.

The following could be encapsulated into a stored procedure that can be executed via a scheduled job. I'm not exactly sure what you meant by for JustifyDate it should add whatever day it is when the job is running.
Declare #NewEntries Table (
EntryId int not null
, EmployeeNumber int not null
, JustifyDate datetime
, EntryDate datetime
)
Insert [Entry]( EmployeeNumber, JustifyDate, EntryDate )
Output Inserted.EntryId, Inserted.EmployeeNumber, Inserted.JustifyDate, Inserted.EntryDate
Into #NewEntries
Select EmployeeNumber
, CURRENT_TIMESTAMP
, DateAdd(hh, CoreHour.InHour, DateAdd(d, 0, CURRENT_TIMESTAMP))
From Employee
Join CoreHour
On CoreHour.CoreHourId = Employee.CoreHourId
Where WorkingFromHome = 1
Insert [Hour]( EntryId, InHour, OutHour )
Select NE.EntryId, DatePart(hh, NE.EntryDate), CoreHour.OutHour
From #NewEntries As NE
Join Employee
On Employee.EmployeeNumber = NE.EmployeeNumber
Join CoreHour
On CoreHour.CoreHourId = Employee.CoreHourId
(Revised to use Output clause).

Related

Compare one row of a table to every rows of a second table

I am trying to retrieve the number of days between a random date and the next known date for a holiday. Let's say my first table looks like this :
date | is_holiday | zone
9/11/18 | 0 | A
22/12/18 | 1 | A
and my holidays table looks like this
start_date | end_date | zone
20/12/18 | 04/01/18 | A
21/12/18 | 04/01/18 | B
...
I want to be able to know how many days are between an entry that is not a holiday in the first table and the next holiday date.
I have tried to get the next row with a later date in a join clause but the join isn't the tool for this task. I also have tried grouping by date and comparing the date with the next row but I can have multiple entries with the same date in the first table so it doesn't work.
This is the join clause I have tried :
SELECT mai.*, vac.start_date, datediff(vac.start_date, mai.date)
FROM (SELECT *
FROM MAIN
WHERE is_holiday = 0
) mai LEFT JOIN
(SELECT start_date, zone
FROM VACATIONS_UPDATED
ORDER BY start_date
) vac
ON mai.date < vac.start_date AND mai.zone = vac.zone
I expect to get a table looking like this :
date | is_holiday | zone | next_holiday
9/11/18 | 0 | A | 11
22/12/18 | 1 | A | 0
Any lead on how to achieve this ?
It might get messy to do it in SQL but if in case you are open to doing it from code, here is what it should look like. You basically need a crossJoin
Dataset<Row> table1 = <readData>
Dataset<Row> holidays = <readData>
//then cache the small table to get the best performance
table1.crossJoin( holidays ).filter("table1.zone == holidays.zone AND table1.date < holidays.start_date").select( "table1.*", "holidays.start_date").withColumn("nextHoliday", *calc diff*)
In scenarios where one row from table1 matches multiple holidays, then you can add an id column to table1 and then group the crossJoin.
// add unique id to the rows
table1 = table1.withColumn("id", functions.monotonically_increasing_id() )
Some details on crossJoins:
http://kirillpavlov.com/blog/2016/04/23/beyond-traditional-join-with-apache-spark/

I want to insert an ID for every existing ROW with values

I have a table with different columns (all with data), the problem I have is that I want to assign an ID for every row and I don't know how to do it.
I tried to do with a cursor, but I do not have any condition to distinct the rows, cause in every row some data will be in other rows.
Example of what I want to do:
TABLE RACE
___________
COLUMNS
___________
Circuit | Date | Pilots
Montmelo| 10/12| 24
Montmelo| 10/13| 24
Montmelo| 10/14| 24
Japan | 10/17| 23
This is just an example of a table, and what I want is add an ID for every row, increased by 1
TABLE RACE
___________
COLUMNS
___________
Circuit | Date | Pilots | ID
Montmelo| 10/12| 24 | 1
Montmelo| 10/13| 24 | 2
Montmelo| 10/14| 24 | 3
Japan | 10/17| 23 | 4
Hope you understand me, and sorry for my English!
#Jeyem, You can easily add an ID column using ROW_NUMBER. Like this:
DECLARE #MyTable TABLE (Circuit varchar(20), Date VARCHAR(8), Pilots int)
INSERT INTO #MyTable
SELECT 'Montmelo', '10/12', 24 UNION ALL
SELECT 'Montmelo', '10/13', 24 UNION ALL
SELECT 'Montmelo', '10/14', 24 UNION ALL
SELECT 'Japan', '10/17', 23
SELECT *, ID=ROW_NUMBER()over(order by Date)
FROM
#MyTable
Here is what i've worked out. Turns out you can use the IDENTITY to count rows like that.
So thats:
ALTER TABLE RACE ADD id INT IDENTITY(1,1);
Try This :-
ALTER TABLE RACE ADD id INT IDENTITY(1,1);
OR
select Circuit,Date,Pilots,ROW_NUMBER()OVER(ORDER BY (SELECT 1 )) as ID From RACE

Is it possible to group by day, month or year with timestamp values?

I have a table ORDERS(idOrder, idProduct, Qty, OrderDate) where OrderDate is a varchar column with timestamp values, is it possible to get the Qty of each day, week, month or year ?
The table looks like this :
---------------------------------------
|idOrder | idProduct | Qty | OrderDate|
---------------------------------------
| 1 | 5 | 20 | 1504011790 |
| 2 | 5 | 50 | 1504015790 |
| 3 | 5 | 60 | 1504611790 |
| 4 | 5 | 90 | 1504911790 |
-----------------------------------------
and i want something like this
------------------------------
| idProduct | Qty | OrderDate|
-------------------------------
| 5 | 70 | 08/29/2017|
| 5 | 60 | 09/05/2017|
| 5 | 90 | 09/08/2017|
-------------------------------
looks like you want to do 2 things here: first group by your idProduct and OrderDate
select idProduct, sum(Qty), OrderDate from [yourtable] group by idProduct, OrderDate
This will get you the sums that you want. Next, you want to convert time formats. I assume that your stamps are in Epoch time (number of seconds from Jan 1, 1970) so converting them takes the form:
dateadd(s,[your time field],'19700101')
It also looks like you wanted your dates formatted as mm/dd/yyyy.
convert(NVARCHAR, [date],101) is the format for accomplishing that
Together:
select idProduct, sum(Qty), convert(NVARCHAR,dateadd(s,OrderDate,'19700101'), 101)
from [yourtable]
group by idProduct, OrderDate
Unfortunately, the TSQL TIMESTAMP data type isn't really a date. According to this SO question they're even changing the name because it's such a misnomer. You're much better off creating a DATETIME field with a DEFAULT = GETDATE() to keep an accurate record of when a line was created.
That being said, the most performant way I've seen to track dates down to the day/week/month/quarter/etc. is to use a date dimension table that just lists every date and has fields like WeekOfMonth and DayOfYearand. Once you join your new DateCreated field to it you can get all sorts of information about that date. You can google scripts that will create a date dimension table for you.
Yes its very simple:
TRUNC ( date [, format ] )
Format can be:
TRUNC(TO_DATE('22-AUG-03'), 'YEAR')
Result: '01-JAN-03'
TRUNC(TO_DATE('22-AUG-03'), 'MONTH')
Result: '01-AUG-03'
TRUNC(TO_DATE('22-AUG-03'), 'DDD')
Result: '22-AUG-03'
TRUNC(TO_DATE('22-AUG-03'), 'DAY')
Result: '17-AUG-03'

SQL : Getting data as well as count from a single table for a month

I am working on a SQL query where I have a rather huge data-set. I have the table data as mentioned below.
Existing table :
+---------+----------+----------------------+
| id(!PK) | name | Date |
+---------+----------+----------------------+
| 1 | abc | 21.03.2015 |
| 1 | def | 22.04.2015 |
| 1 | ajk | 22.03.2015 |
| 3 | ghi | 23.03.2015 |
+-------------------------------------------+
What I am looking for is an insert query into an empty table. The condition is like this :
Insert in an empty table where id is common, count of names common to an id for march.
Output for above table would be like
+---------+----------+------------------------+
| some_id | count | Date |
+---------+----------+----------------------+
| 1 | 2 | 21.03.2015 |
| 3 | 1 | 23.03.2015 |
+-------------------------------------------+
All I have is :
insert into empty_table values (some_id,count,date)
select id,count(*),date from existing_table where id=1;
Unfortunately above basic query doesn't suit this complex requirement.
Any suggestions or ideas? Thank you.
Udpated query
insert into empty_table
select id,count(*),min(date)
from existing_table where
date >= '2015-03-01' and
date < '2015-04-01'
group by id;
Seems you want the number of unique names per id:
insert into empty_table
select id
,count(distinct name)
,min(date)
from existing_table
where date >= DATE '2015-03-01'
and date < DATE '2015-04-01'
group by id;
If I understand correctly, you just need a date condition:
insert into empty_table(some_id, count, date)
select id, count(*), min(date)
from existing_table
where id = 1 and
date >= date '2015-03-01' and
date < date '2015-04-01'
group by id;
Note: the list after the table name contains the columns being inserted. There is no values keyword when using insert . . . select.
insert into empty_table
select id, count(*) as mycnt, min(date) as mydate
from existing_table
group by id, year_month(date);
Please use function provided by your RDBMS obtaining date part containing only year and month as far as you did not provide the RDBMS version and the date processing functionality varies wildly between them.

Write SQL script to insert data

In a database that contains many tables, I need to write a SQL script to insert data if it is not exist.
Table currency
| id | Code | lastupdate | rate |
+--------+---------+------------+-----------+
| 1 | USD | 05-11-2012 | 2 |
| 2 | EUR | 05-11-2012 | 3 |
Table client
| id | name | createdate | currencyId|
+--------+---------+------------+-----------+
| 4 | tony | 11-24-2010 | 1 |
| 5 | john | 09-14-2010 | 2 |
Table: account
| id | number | createdate | clientId |
+--------+---------+------------+-----------+
| 7 | 1234 | 12-24-2010 | 4 |
| 8 | 5648 | 12-14-2010 | 5 |
I need to insert to:
currency (id=3, Code=JPY, lastupdate=today, rate=4)
client (id=6, name=Joe, createdate=today, currencyId=Currency with Code 'USD')
account (id=9, number=0910, createdate=today, clientId=Client with name 'Joe')
Problem:
script must check if row exists or not before inserting new data
script must allow us to add a foreign key to the new row where this foreign related to a row already found in database (as currencyId in client table)
script must allow us to add the current datetime to the column in the insert statement (such as createdate in client table)
script must allow us to add a foreign key to the new row where this foreign related to a row inserted in the same script (such as clientId in account table)
Note: I tried the following SQL statement but it solved only the first problem
INSERT INTO Client (id, name, createdate, currencyId)
SELECT 6, 'Joe', '05-11-2012', 1
WHERE not exists (SELECT * FROM Client where id=6);
this query runs without any error but as you can see I wrote createdate and currencyid manually, I need to take currency id from a select statement with where clause (I tried to substitute 1 by select statement but query failed).
This is an example about what I need, in my database, I need this script to insert more than 30 rows in more than 10 tables.
any help
You wrote
I tried to substitute 1 by select statement but query failed
But I wonder why did it fail? What did you try? This should work:
INSERT INTO Client (id, name, createdate, currencyId)
SELECT
6,
'Joe',
current_date,
(select c.id from currency as c where c.code = 'USD') as currencyId
WHERE not exists (SELECT * FROM Client where id=6);
It looks like you can work out if the data exists.
Here is a quick bit of code written in SQL Server / Sybase that I think answers you basic questions:
create table currency(
id numeric(16,0) identity primary key,
code varchar(3) not null,
lastupdated datetime not null,
rate smallint
);
create table client(
id numeric(16,0) identity primary key,
createddate datetime not null,
currencyid numeric(16,0) foreign key references currency(id)
);
insert into currency (code, lastupdated, rate)
values('EUR',GETDATE(),3)
--inserts the date and last allocated identity into client
insert into client(createddate, currencyid)
values(GETDATE(), ##IDENTITY)
go