Fetching Date and year from Timestamp column (Athena) - sql

I'm working in Athena (SQL) and trying to extract the date, year and month from column called "createddate" which involves timestamp and its a varchar data type. I have tried bunch of queries but I am getting errors after errors. Can you please help me extracting the date, year, month, week from this createddate column?
The field "createddate" is found in the following format:
id
createddate
1
11/29/2016 10:58:02
2
12/04/2016 07:07:58
3
10/22/2018 03:47:23
4
10/22/2018 08:20:25
5
10/22/2018 08:29:26
6
10/22/2018 08:42:28
7
10/22/2018 08:46:21
8
10/22/2018 10:18:57
12
10/22/2018 22:16:46
13
10/22/2018 22:24:33
14
10/23/2018 02:55:49
15
10/23/2018 07:49:39
16
10/23/2018 09:15:57
32
10/26/2018 06:19:13
33
10/26/2018 06:21:09
34
10/26/2018 06:24:59
48
10/30/2018 19:11:41
49
10/30/2018 20:10:10
64
11/01/2018 18:06:15
65
11/01/2018 18:08:00
66
11/01/2018 18:08:37
2
12/04/2016 07:07:58
99
11/09/2018 23:52:02
100
11/09/2018 23:57:13
Here are all my attempts:
select * from table where YEAR(from_iso8601_timestamp(createddate)) = 2022
and MONTH(from_iso8601_timestamp(createddate)) = 6 limit 10
select date_format(createddate,'%m/%d/%Y %H:%m:%s') from table limit 10
select date_format(createddate,'%m/%d/%Y H:m:s') from table limit 10
select date_format(createddate,'%m/%d/%Y %H:%i:%s') from table limit 10
select date_parse(createddate, '%m/%d/%Y %H:%i%p') from table limit 10
select date_parse(createddate, ' %m/%d/%Y') from table limit 10
select cast(date_parse(createddate,'%Y-%m-%d %h24:%i:%s') as date) from table limit 10
Select CAST(date_format(date_parse(cast(createddate as varchar(10)), '%m%d%Y'), '%m/%d/%Y') AS DATE) from table limit 10

Try with this one:
WITH cte AS (
SELECT id,
DATE_PARSE(createddate, '%m/%d/%Y %H:%i:%s') AS createddate
FROM table
)
SELECT id,
createddate,
YEAR(createddate),
MONTH(createddate),
WEEK(createddate)
FROM cte
You can find the documentation relative to the employed date functions here.

Related

SQL: Get SUM of grouped query having count statement

I'm looking for a query that counts all entries which have more than 1 entry.
e.g.:
adrnr value datum
15 8.68 2021-08-29
10 16.4 2021-08-30
15 33.6 2021-08-30
16 125.98 2021-08-31
10 23.44 2021-08-31
18 19.87 2021-08-31
16 26.87 2021-09-01
10 10.0 2021-09-01
I came up with following query:
SELECT adrnr, COUNT(adrnr) as summe FROM tablename WHERE datum >= '2021-01-08 00:00:00.000'
AND datum <= '2021-31-08 23:59:59.999' group by adrnr having count(adrnr) > 1
I then get an answer:
adrnr summe
15 2
10 2
This is fine, but I want to get just one SUM of all entries, i.e.:
summe
4
How do I get this answer. And after that I want to get the the SUM of all values that depends on the same condition, i.e. 82.12
Any idea?
Regards Jens
Use a subquery against your current query:
SELECT SUM(summe)
FROM
(
SELECT COUNT(adrnr) AS summe
FROM tablename
WHERE datum >= '2021-08-01' AND datum < '2021-09-01'
GROUP BY adrnr
HAVING COUNT(drnr) > 1
) t;
Note: Your timestamp/date literals looked a bit off. The above assumes you only want to target the month of August, 2021.

SQL: Select only users who are new in 2021

If we have a table as follows:
User_ID
Order_date
Order_ID
1
2020-02-02
23
2
2021-03-03
45
1
2021-02-02
13
3
2019-05-23
34
3
2021-01-31
56
How to select only the user whose first order is in the year 2021 (in this case, only User 2)?
You can use aggregation:
select user_id
from t
group by user_id
having min(order_date) >= '2021-01-01';
This checks that the earliest order date is after the first of the year.

SQL query to get records inserted over last 7 days grouped by day

I have a table two very similar tables that store purchases and downloads they both look similar to this with an id and date
id date
1 2020-06-15 18:25:27.415548+01
2 2020-06-15 11:03:30.157502+01
3 2020-06-15 17:09:15.592209+01
4 2020-06-14 18:29:18.332623+01
5 2020-06-13 18:09:31.990473+01
... many more rows ...
I would like to be able execute a Postgres query that returns the count of all the purchases and downloads inserted over the last 7 days grouped by day. An ideal response would look like this
date purchase_count download_count
2020-06-13 37 64
2020-06-14 44 56
2020-06-15 34 63
2020-06-16 41 72
2020-06-17 30 40
2020-06-18 42 55
2020-06-19 9 22
One method uses aggregation with full join:
select dte, coalesce(d.downloads, 00) as downloads, coalesce(p.purchases, 0) as purchases
from (select date_trunc('day', date) as dte, count(*) as downloads
from downloads
group by dte
) d full join
(select date_trunc('day', date) as dte, count(*) as purchases
from purchases
group by dte
) p
using (dte)
order by dte;

Sorting of Date Format in Oracle

I am using a query in oracle which gives the below result (its a kind of month-wise transaction report):
Month Total Submitted Approved
--------------------------------------
DEC-14 2 2 0
APR-15 17 12 5
SEP-14 1 1 0
FEB-15 7 4 3
JUL-15 1 1 0
JAN-15 18 4 14
MAR-15 2 1 1
OCT-14 2 (null) (null)
JUN-15 136 91 45
JUN-14 1 1 0
MAY-15 179 63 116
I want to get the result in a sorted format, like JUN-14,SEP-14,OCT-14,DEC-14,JAN-15....so on. Thanks in advance.
order by date_column desc where date_column is the column that holds the date. This will order by the date_column in descending order.
Use asc to order in ascending order.
If month data type in character format you have to use
select * from table_name
order by to_char(to_date(month,'mm/yy'),'yy') asc,to_char(to_date(month,'mm/yy'),'mm') asc
if it is in date
select * from table_name
order by to_char(month,'yy') asc,to_char(month,'mm') asc
i assumed that you were using the following for displaying month column data.
TO_char(hiredate,'mon-yy')
if you used this then it will be easy for sorting them.
select your column list from table order by source_date_column asc;
for reference use the link

SELECT all at once

I want to select patient_id and date difference from below table:
p_id TreatmentDate
15 2008-05-01
15 2008-05-03
15 2008-05-05
15 2008-05-07
16 2008-05-01
16 2008-05-03
16 2008-05-05
16 2008-05-09
16 2008-05-11
17 2008-05-03
17 2008-05-05
17 2008-05-07
I want to have this result:
p_id Day Difference
15 6 Days
16 10 Days
17 4 Days
Do you have any offer that can generate this result in sql statement?
This should work in general
select p_id, max(TreatmentDate) - min(TreatmentDate) from
patientsTable group by p_id
more specifically, for MSSQL Server
select p_id, DATEDIFF(D, MIN(TreatmentDate), MAX(TreatmentDate)) from
patientsTable group by p_id
MS SQL Server:
SELECT
p_id,
STR(DATEDIFF(DAY, MIN(TreatmentDate), MAX(TreatmentDate))) + ' Days' AS DayDifference
FROM
table
GROUP BY
p_id
MS SQL:
select
p_id,
datediff(d, min(TreatmentDate), max(TreatmentDate)) AS DayDifference
from
patientsTable
group by
p_id;
This will work:
SELECT p_id, CONCAT(max(TreatmentDate) - min(TreatmentDate),' Days') as "Day Difference"
FROM
patient_info
GROUP BY p_id;
Given this schema/data:
CREATE TABLE patient_info (
p_id INT,
TreatmentDate DATE
);
INSERT INTO patient_info
VALUES
(15,'2008-05-01'),
(15,'2008-05-03'),
(15,'2008-05-05'),
(15,'2008-05-07'),
(16,'2008-05-01'),
(16,'2008-05-03'),
(16,'2008-05-05'),
(16,'2008-05-09'),
(17,'2008-05-03'),
(17,'2008-05-05'),
(17,'2008-05-07');
+------+----------------+
| p_id | Day Difference |
+------+----------------+
| 15 | 6 Days |
| 16 | 8 Days |
| 17 | 4 Days |
+------+----------------+
3 rows in set (0.00 sec)
Please let me know if you need more help.