SQL Athena getting the data for the past 12 months - sql

I need to grab the data for the past 12 months. I am using SQL within AWS Athena. Below is my code:
CREATE
OR REPLACE VIEW response_view AS
SELECT
"cust"."customer_id",
"cust"."event_triggered_date"
FROM
(
db.population_view pop
INNER JOIN new_db.manual_response_view cust ON ("pop"."customer_id" = "cust"."customer_id")
)
WHERE "cust"."event_triggered_date" > current_date - interval '12' month
Gives me an error: cannot be applied to varchar, date
event_triggered_filed is a string
This is the structure of the date field: 2019-12-04 00:00:00.000

Try this.
CAST(EVENT_TRIGGERED_DATE AS DATE)
OR
CAST(EVENT_TRIGGERED_DATE AS TIMESTAMP )
Data Types

I had same problem I need to get the stating day of the 6th and 12th month from current date.
I have written this, it works for me I hope it will work for other as well.
please refer attached image for query result
select current_date as today, date_add('month', -6, (select date(date_format(cast(current_date as date),'%Y-%m-01')))) as Start_of_the_date_6_month_before
For Staring day of the 12 month before date.
select current_date as today, date_add('month', -12, (select date(date_format(cast(current_date as date),'%Y-%m-01')))) as Starting_day_of_the_month_12_before
Finally i make this changes like this :
With
six_month_And_minus_1_day AS (
select date_add('month', -6, (select date(date_format(cast(current_date as date),'%Y-%m-01')))) as Start_of_the_date_6_month_before,
date_add('day', -1, date_add('month', -6, (select date(date_format(cast(current_date as date),'%Y-%m-01'))))) as Start_of_the_date_6_month_before_minus_1
)
select * from six_month_And_minus_1_day
Any one has better way to do this please suggest.

Related

Is there is a way to get the month end date for every month

How to get the current month end date through db2 query.
I should not need to modify the query for every month
Every month the sql is executing so it need to automatically take care
In Db2 11.1 this could be a solution:
select next_month(current date) - 1 day from sysibm.sysdummy1
Next_Month will return the first day of the next month from the data provided.
SELECT LAST_DAY(CURRENT_DATE) AS LASTDAY
FROM SYSIBM.SYSDUMMY1;
The query is working
LASTDAY
2019-04-30
The old way would be
CURRENT DATE - (DAY(CURRENT DATE)) DAYS + 1 MONTH
To get the last day of the current month, use this:
SELECT DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0))
This will format the date for you
SELECT CONVERT(VARCHAR(10), DATE, 12)
See this for more information regarding the '12'. 12 is the right code
Another solution is to use TIMESTAMP_FORMAT as such:
TIMESTAMP_FORMAT('190404', 'YYMMDD')

SQL: how do you fetch a first day of the month?

SQL: how do you fetch a first day of the month?
Hi,
I need to fetch a first day of every month from tbl.date from column start_date.
11/1/2017
12/1/2017
1/1/2018
2/1/2018
ETC.
Thanks
One solution in ANSI SQL looks like this:
select dte - (1 - extract(day from dte)) * interval '1 day'
In SQL Server, this would be:
select dateadd(day, 1 - day(dte), dte)
Similar functionality is available in almost any database.
If you just want the rows that are first days of the month, then:
where extract(day from dte) = 1
In SQL Server, this would be:
where day(dte) = 1
Of course, the syntax might be different depending on the database.
Another solution, using Postgres, in case you need to select the first row for each month, if there's no guarantee that that will be the first day of the month:
CREATE TABLE dates (
id BIGSERIAL PRIMARY KEY,
date TIMESTAMP NOT NULL
);
INSERT INTO dates(date)
VALUES ('2017-05-05'::timestamp), ('2017-05-02'::timestamp), ('2017-05-30'::timestamp), ('2017-05-25'::timestamp), ('2017-05-15'::timestamp), ('2017-05-01'::timestamp), ('2017-06-10'::timestamp), ('2017-06-02');
SELECT DISTINCT ON(EXTRACT(MONTH FROM date), EXTRACT(YEAR FROM date)) date
FROM dates
ORDER BY EXTRACT(MONTH FROM date), EXTRACT(YEAR FROM date), date ASC;
Results:
date
--------------------
2017-05-01T00:00:00Z
2017-06-02T00:00:00Z
Another solution, using Sql server
declare #FirstDOM date, #LastDOM datetime
set #FirstDOM = (select dateadd(d,-1,dateadd(mm,datediff(m,0,getdate()),1 )))
SELECT CONVERT( varchar, #FirstDOM,101);

SQL Select Records based on current date minus two days

I have an orders table which contains an order ID, order date and order description.
I want to run a select query which captures all orders that have been created in the last two days. so the current date minus two days. from the 14th December, I would want to select all orders where the order date is > 13th December. This needs to use a Get date function to pick up the current date and minus the days.
I have tried:
select * from orders where orderdate > getdate() - 2
but this is not producing the correct results. Any idea's how to do this please?
you should try to use dateadd function
select * from orders where orderdate > dateadd(dd,-1,cast(getdate() as date))
Now, this may be exactly what you need but then you need to understand that by casting to date we remove the time part and effectively go back to the start of the day and a day behind it(-1) gives the start of yesterday.
Try this:
select * from orders where orderdate > cast(getdate() - 1 as date)
If you want the orders of the last two days, use DATEADD to add days to today's date (in your case -2 days) then use DATEDIFF to compare the two days:
SELECT * FROM orders
WHERE DATEDIFF(DAY, DATEADD(DAY, -2, GETDATE()), orderdate) > 0
Now, assuming all orders have dates in the past and none in the future (which is what it should be), you can simply use DATEDIFF like this:
SELECT * FROM orders
WHERE DATEDIFF(DAY, orderdate, GETDATE()) <= 2
Note: you can use < 3 instead of <= 2.
Looks like dateadd and convert will solve the problem.
select o.*
from orders o
where o.orderdate >= dateadd(day, -2, convert(date, getdate()))

Comparing dates in SQL Server

I have a DateTime column named EXP_Date which contains date like this :
2014-07-13 00:00:00.000
I want to compare them, like this query :
SELECT COUNT(*)
FROM DB
WHERE ('2014-07-15' - EXP_DATE) > 1
I expect to see the number of customers who have their services expired for over a month.
I know this query wouldn't give me the correct answer, the best way was if I separate the Year / Month / Day into three columns, but isn't any other way to compare them as they are?
You can use DATEADD
SELECT COUNT(*)
FROM DB
where EXP_DATE < DATEADD(month, -1, GETDATE())
Try this
SELECT COUNT(*)
FROM DB
where DATEADD(month, -1, GETDATE()) > EXP_DATE
SELECT COUNT(EXPIRE)FROM
(Select CASE WHEN EXP_DATE < DATEADD(month, -1, GETDATE())THEN 1 ELSE 0 END)AS EXPIRE FROM DB
)tt
Another way using DATEDIFF
SET DATEFORMAT DMY --I like to use "dateformat"
SELECT COUNT(*)
FROM DB
WHERE (DATEDIFF(DAY,#EXP_DATE,GETDATE())) >= 30 --Remember, instead "Day" you can use week, month, year, etc
Syntax: DATEDIFF ( datepart , startdate , enddate )
Depart: year, quarter, month, day, week...
For more information you can visit MSDN

SQL query for getting data for last 3 months

How can you get today's date and convert it to 01/mm /yyyy format and get data from the table with delivery month 3 months ago? Table already contains delivery month as 01/mm/yyyy.
SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(MONTH, -3, GETDATE())
Mureinik's suggested method will return the same results, but doing it this way your query can benefit from any indexes on Date_Column.
or you can check against last 90 days.
SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(DAY, -90, GETDATE())
Latest Versions of mysql don't support DATEADD instead use the syntax
DATE_ADD(date,INTERVAL expr type)
To get the last 3 months data use,
DATE_ADD(NOW(),INTERVAL -90 DAY)
DATE_ADD(NOW(), INTERVAL -3 MONTH)
I'd use datediff, and not care about format conversions:
SELECT *
FROM mytable
WHERE DATEDIFF(MONTH, my_date_column, GETDATE()) <= 3
Last 3 months
SELECT DATEADD(dd,DATEDIFF(dd,0,DATEADD(mm,-3,GETDATE())),0)
Today
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)
Last 3 months record
SELECT *, DATEDIFF(NOW(),`created_at`)as Datediff from `transactions` as t having Datediff <= 90