How to get previous months data using KDB query? - sql

How can I retrieve data from the previous month in such a way that, if the query were to be automated, the date value in the query would change accordingly, every month?
So for example:
When query is run on 2012.01.01 --> select * from Table where date >= 2011.12.01
When query is run on 2012.02.01 --> select * from Table where date >= 2012.01.01
When query is run on 2012.03.01 --> select * from Table where date >= 2012.02.01
and so on..
Help would be much appreciated!

I assume you are using Oracle Database;
select * from Table where date >= ADD_MONTHS(TRUNC(SYSDATE),-1)

Related

Automatically execute script for each day

I have a script which calculates the metrics for yesterday automatically and inserts the data into the table, but I want to fill the table with all missing dates.Is it even possible to do automatically or I should manually start the script for each day?
Here is the simplified version of the script:
select sum(amount),id,yesterday
where date < yesterday
group by id
But for example the day before yesterday is also missing in the table, so I want the above script to execute, and also the script:
select sum(amount),id,day_before_yesterday
where date < day_before_yesterday
group by id
use the last date you have in the target table :
select sum(amount),id, max(date)
from table
where date < (select max(date) - interval 1 day from target)
group by id

Need your assistance with a SQL Server query to get result days between two dates

How can I display the query in such a way to show each day between date?
SELECT job.wo_id
FROM [MES].[MESDB].[dbo].[job] AS job
WHERE job.init_sched_ent_id = 227
AND job.sched_start_time_local >= #paramStartDate
AND job.sched_finish_time_local <= #paramEndDate
I can select each day separately by function from this post but I don't know how to combines these two tables.
https://stackoverflow.com/a/44671808/8853661
You can generate dates in mySql and cross join with your select.
The good description is here : generate days from date range

generate each minute string for a day within specified time limit

My aim is to generate per minute count of all records existing in a table like this.
SELECT
COUNT(*) as RECORD_COUNT,
to_Char(MY_DATE,'HH24:MI') MINUTE_GAP
FROM
TABLE_A
WHERE
BLAH='Blah! Blah!!'
GROUP BY
to_Char(MY_DATE,'HH24:MI')
However, This query doesn't give me the minutes where there were no results.
To get the desired result it, I'm to using the following query to fill the gaps in the original query by doing a JOIN between these two results.
SELECT
*
FROM
( SELECT
TO_CHAR(TRUNC(SYSDATE)+( (ROWNUM-1) /1440) ,'HH24:MI') as MINUTE_GAP,
0 as COUNT
FROM
SOME_LARGE_TABLE_B
WHERE
rownum<=1440
)
WHERE
minute_gap>'07:00' /*I want only the data starting from 7:00AM*/
This works for me, But
I can't rely on SOME_LARGE_TABLE_B to generate the minutes
because it might have no records at some point in future
The query doesn't look like a professional solution.
Is there any easier way to do this?
NOTE:I don't want any new tables created with static values for all the minutes just for one query.
Just generate your timestamps and left join your grouped data to it:
SELECT MINUTE, ....
FROM (
SELECT TO_CHAR(TO_DATE((LEVEL + 419) * 60, 'SSSSS'), 'HH24:MI') MINUTE /* 07:00 - 23:59 */ FROM DUAL CONNECT BY LEVEL <= 1020)
LEFT JOIN (
<your grouped subquery>
) ON MINUTE = MINUTE_GAP

Date in sql database

i want to access date from my table and i only want only particular dates to display (for eg: date=1) .
Is there any way to access date,month and year individually from date format?
please help
Avinash is this what you are looking for ?
select MONTH('10/15/1981') --10
select DAY('10/15/1981') --15
select YEAR('10/15/1981') --1981
Select Dates
from tablename
Where MONTH(Dates) = 10

Help me build a SQL select statement

SQL isn't my greatest strength and I need some help building a select statement.
Basically, this is my requirement. The table stores a list of names and a timestamp of when the name was entered in the table. Names may be entered multiple times during a week, but only once a day.
I want the select query to return names that were entered anytime in the past 7 days, but not today.
To get a list of names entered today, this is the statement I have:
Select * from table where Date(timestamp) = Date(now())
And to get a list of names entered in the past 7 days, not including today:
Select * from table where (Date(now())- Date(timestamp) < 7) and (date(timestamp) != date(now()))
If the first query returns a set or results, say A, and the second query returns B, how can I get
B-A
Try this if you're working with SQL Server:
SELECT * FROM Table
WHERE Timestamp BETWEEN
dateadd(day,datediff(day,0,getdate()),-7),
AND dateadd(day,datediff(day,0,getdate()),0)
This ensures that the timestamp is between 00:00 7 days ago, and 00:00 today. Today's entries with time greater than 00:00 will not be included.
In plain English, you want records from your second query where the name is not in your first query. In SQL:
Select *
from table
where (Date(now())- Date(timestamp) < 7)
and (date(timestamp) != date(now()))
and name not in (Select name
from table
where Date(timestamp) = Date(now())
)
not in
like
select pk from B where PK not in A
or you can do something like
Select * from table where (Date(now())- Date(timestamp) < 7) and (Date(now())- Date(timestamp) > 1)