Get data from past 7 days DB2 - ssis

I have no experience working with DB2 before and I'm kind of stuck in something. I'm working on a project in SSIS reading from DB2 where I write into a flat file. I need to run the process weekly and get data from past 7 days.
My query works this way:
Select * From Table
Where ServiceDate >= 2200624 - 7
The above query brings data from the past 7 days, but this query don't work for me since I need to execute this process weekly. I need something like this:
Select * From Table
Where ServiceDate >= DATE(CURRENT_DATE - 7 DAY)
The second query throws an error, is there any other way to achieve this? I'm using ODBC source and I was thinking to use a dynamic query in SSIS but I'm not sure how this works in ODBC source, any suggestions or help will be appreciated.
EDIT:
This tables were created a long time ago, so I don't have any information about the data type of these tables.
The actual date 2200624 correspond to 20200624. This is the way that my date shows in the table.
Thanks in advance

For ServiceDate as YYYYMMDD INT:
Select * From Table
Where ServiceDate >= INT(TO_CHAR(CURRENT_DATE - 7 DAY, 'YYYYMMDD'));

If ServiceDate is CHAR(7) or equivalent, and if value 2200624 corresponds to YYYYMMDD date 20200624 as per your edited question, then the following examples might help.
It assumes ServiceDate values beginning with first character 1 are in the 20th century (19xx years), and dates with first character 2 are in the 21st century.
SELECT ... FROM ... WHERE ( TO_DATE(CASE SUBSTR(ServiceDate,1,1) WHEN '1' THEN '19'||SUBSTR(ServiceDate,2,6) WHEN '2' THEN '20'||SUBSTR(ServiceDate,2,6) END,'YYYYMMDD')) >= CURRENT DATE - 7 DAYS
This will perform badly, so don't use that!
An alternative that will perform better is to convert CURRENT DATE - 7 DAYS into a number that matches your storage-format like this:
...WHERE ServiceDate >= '2'||substr(TO_CHAR(CURRENT_DATE - 7 DAY, 'YYYYMMDD'),2,6)
and if ServiceDate is INTEGER column datatype then:
...WHERE ServiceDate >= int('2'||substr(TO_CHAR(CURRENT_DATE - 7 DAY, 'YYYYMMDD'),2,6))
Always state your Db2-server platform (Z/OS, i-series, Linux/Unix/Windows) when asking for help with Db2, because the answer may be different depending on the platform + version of your Db2-server.

Related

Isolating MySql records created today

I am just learning MySql (SQL in general) and I have a question. I ran a process to populate a table with 72 records. This was done, however, I needed to run the process again and this time it populated the table again with a second record for each user for a total now of 144 records. How can I isolate the newest records created today?
A simple solution is to use current_date to figure out today's date and date() to remove the time portion of your column. Then:
where current_date = date(createdTS)
This is fine for a small dataset as yours. As general solution, you'd need a query that won't need to manipulate every row, e.g.
where createdTS >= current_date and createdTS < current_date + interval 1 day
You just have to use your createdTS column, (assuming you know what was the timestamp of both runs).
SELECT * FROM `my_table` WHERE `createdTS` > '2019-07-25 15:00:00'
You could also RANK() over and get only the newest run for each user (something like this)

SQL Like Search

I am trying run to say find the devices that did not contain 01: in the past 7 days.
I have tried "Where column Not Like '%01:%'" but it just removes the 01: and still shows the machine that had the 01: in the past 7 days.
I have a table called devices. Each location has a unique ID number. Each device runs a job at 1am and 7pm. Devices should have 1 entry for 01:00:00 per week then 3 entries for 19:00:00 per week. Ex of cell data is 2017-10-23 19:00:02.
So I begin with
Select * From devices
Where locationid=##
AND jobdate < DATE_SUB(NOW(), INTERVAL 7 DAY))
AND jobdate not like '%01:%'
What I get in result is the machine that did run at 01:00 2 days ago. The job date shows 19:00 so it sounds like it just removed the 01:.
I am thinking of grouping the job data then say list the computer that did not have 2017-10-23 01:00:02 .
There is a good deal of intuition in the following suggestion, more on that later.
Most databases don't actually store date/time information is a WYSIWYG fashion. Indeed if you think about it long enough you will understand that date/times are really "sets of numbers". That is why we can do things like calculate the number of days from date1 to date2 etc. So, IF the data is stored as a datetime data type don't attempt to use LIKE (which is for text) against a datetime column. Instead look for date and time related functions that may apply to your situation. Here you are looking for not equal to specific time of day (I think). So, to remove "date" from consideration convert it to "time", and then you can filter on that.
So below, I introduce a new column jobtime which is the time portion of jobdate, and then I look for any times not equal to a given value.
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE Devices
(`locationid` varchar(2), `jobdate` datetime)
;
INSERT INTO Devices
(`locationid`, `jobdate`)
VALUES
('##', '2017-10-23 01:00:00'),
('##', '2017-10-23 19:00:02')
;
Query 1:
select
*
from (
select locationid, cast(jobdate as time) jobtime, jobdate
from devices
) d
where locationid = '##'
and jobtime <> '01:00:00'
;
Results:
| locationid | jobtime | jobdate |
|------------|----------|----------------------|
| ## | 19:00:02 | 2017-10-23T19:00:02Z |
...
why is there "intuition" above? (the "more on this later")
It is remarkably frustrating to not know which database is in use because the syntax differs so much between the vendors. It is also essential to know the EXACT data type of the jobdate column - because if it is varchar for example I have just made a complete fool of myself in the query above. In other words we are not likely to answer because key facts are missing.
Finally, you have data! It's in your table(s) already. Why not make it easy on everyone by sharing a few bits of it? Provide "sample data" with your question, and the "expected result" too (i.e. provide 2 things, not one without the other, and do not use images of data!!!). Hopefully you can see from the example above how useful sample data & result is. For example, if my intuition is way off, you can tell in an instant that it is - even if you don't read the SQL.
Rant over, not all points raised here apply to this question.

SQL Query to get data between two weeks?

I have a week column with week numbers as w0, w1, w2.... I am trying to get last last six weeks data. Here's the sql query I am using.
SELECT * FROM week
WHERE uid = '9df984da-4318-1035-9589-493e89385fad'
AND report_week BETWEEN `'w52' AND 'w5'`;
'w52' is essentially week 52 in December 2015 and 'w5' is Jan 2016. The 'between' seems to not work. Whats the best way to get data from the above two weeks?
Here's the CREATE TABLE statement:
CREATE TABLE `week` (`uid` VARCHAR(255) DEFAULT '' NOT NULL,
`report_week` VARCHAR(7) NOT NULL,
`report_files_active` BIGINT DEFAULT NULL);
Essentially this table is getting populated from other table which has date column. It uses dates from other table and summarizes weekly data into this.
Any help is appreciated.
Refer to this SO Discussion which details the reasons for a problem similar to yours.
BETWEEN 'a' and 'b' actually matches to columnValue >='a' and columnValue <= 'b'
In your case w52 is greater than w5 due to lexicographic ordering of Strings - this means that the BETWEEN clause will never return a true (think about it as equivalent to saying BETWEEN 10 and 1 instead of BETWEEN 1 and 10.
Edit to my response:
Refrain from storing the week value as a string. Instead here are a couple of approaches in order of their preference:
Have a timestamp column. You can easily then use MySQL query
facilities to extract the week information out of this. For a
reference see this post.
Maintain two columns - YEAR, WEEKNO where YEAR will store values
like 2015, 2016 etc and WEEKNO will store the week number.
This way you can query data for any week in any year.
please show me table structure and DB name because it different for other, if it is any timestamp then we can use BETWEEN 'systemdate' AND 'systemdate-6'

Calculating anniversaries of employees with their joining dates

As part of database testing, we are to verify if the data is correctly rendered onto the webpage from database.
We have a table called 'emp_details' which stores employee details. We store joining date of an employee in it. Now, using this joining date field, I need to get a list all the employees who have a start date or anniversary date within the last ten days.
I tried various combinations of DATEDIFF() in MySQL but did not succeed.
The format on the webpage would look like this:
Name Start Date Years
----------------------------------
William 07/25/2004 8
Gordon 07/22/2007 5
Jill 07/26/2009 3
Could anyone please help me with the query for MySQL DB.
Thanks,
select * from
employees where
dayofyear(`start date`) between dayofyear(curdate())-10 and dayofyear(curdate())
You can use following in WHERE clause
DAYOFYEAR(CURDATE()) - DAYOFYEAR(start_date) < 10
OR is greater than (365 - 10)

Displaying records up to a certain 'age' - MySQL query syntax?

Quite a simple question, but I haven't been able to find any useful resources to help with this.
Basically, I want to query my SQL database table, of which one of the fields is of type 'date'. Currently, the webpage outputs the three most recent records dependent on this date field. Ideally, I want to display all records which are under 4 months old, but I'm not sure how to go about implementing this in my query.
Can anyone advise?
SELECT * FROM table WHERE datecolumn > DATE_SUB(NOW(), INTERVAL 4 MONTH)
Will select all rows where the date is larger than 4 months ago (assuming you have no dates in the future - else you could use a BETWEEN)