Postgresql max date in dataset for each month - sql

I have a table with the following columns (in addition to others):
name char
tanggal date
Rows are inserted into this table each day.
How can I get the formatted name for the max date of each month,
for example:
Jan 31, Feb 28, Mar 31, Apr 30,...
I am using Postgresql 8.3

You could use extract to get the month of the date. From there on, it's a straight forward group by query:
SELECT MAX(tanggal)
FROM mytable
GROUP BY EXTRACT (MONTH FROM tanggal)

Related

Specific Month-day select statement query

I would like to select from a table where the date falls within a specific time each year f.e:
select * from Customer where date >= August 15th and date <= December 20th
Since this will be for a report that runs every year, I do not want to hardcore the date as I will have to change it every year. I would like to have it dynamic to pick the date from August 15th to December 20th of the current year.
I have the below query where I can retrieve the Month and Date:
SELECT DATENAME(month, date) AS Month,DATENAME(day, date) AS Day from Customer
However, I am struggling to have this selection date range.
TIA
In SQL SERVER 2017
maybe this can help you:
SELECT * FROM Customer WHERE date BETWEEN
CONVERT(DATETIME,CONCAT(YEAR(GETDATE()),'08','15')) AND
CONVERT(DATETIME,CONCAT(YEAR(GETDATE()),'12','20'))
This add the current year to a concatenated date you want, then convert it all into datetime type..

Find and sort all the data(dates) after a certain date by month/day and not year in SQLite

I wanna order the data in an sqlite3 database by date. (Day and Month to be precise)
I have a table,
the data in the table are in the format YYYY-MM-DD
2003-02-20, 2005-07-16, 2008-11-18, 1998-01-02, 1996-08-27
Here, I wanna find all the data after a certain date(Current date- 'now') and in order.
The data is birthdays, so the order should be just based off of Month and Day and shouldn't care about the year.
For example, the data here is
Feb 20, Jul 16, Nov 18, Jan 1, Aug 27
current day= July 28
I want the output to look like
Aug 27, Nov 18, Jan 1, Feb 20, Jul 16
I've looked through many examples and documentations and tried some methods
SELECT * FROM table WHERE birthdays>date('now')
*birthdays are the column where dates are stored*
This gives all the data after ('now') as an output, but it orders it by year as well. Hence, the output will be none since none of the years are greater than current year. I wanna take the year out of the equation and just order it by Month and Day.
How can I do it?
You don't need a WHERE clause because all rows of the table should be returned.
What you want is a proper ORDER BY clause:
SELECT *
FROM tablename
ORDER BY strftime('%m-%d', birthdays) > strftime('%m-%d', 'now') DESC,
strftime('%m-%d', birthdays);
See the demo.
According to the sample data and the expected output you posted, you want to find all birthdays that will occur this year after the date of today. You may use the strftime function to extract month and day as the following:
Select user_id, DOB
From your_table
Where strftime('%m-%d',DOB) > strftime('%m-%d',date())
Order By strftime('%m-%d',DOB)
See a demo from db-fiddle.
you can select the dates, by taking he day of birth, adding the current year ( or the next one if it is smalerer tan the current date)and then selecting the dates that are bigger than the current date limiting 10
SELECT user_id, DOB
FROM your_table
ORDER BY
CASE WHEN date(strftime('%Y', date('now')) || strftime('-%m-%d', DOB)) > DATE() then date(strftime('%Y', date('now')) || strftime('-%m-%d', DOB))
ELSE date(strftime('%Y', date('now','+1 years')) || strftime('-%m-%d', DOB)) END
LIMIT 10;
user_id DOB
5 1996-08-27
10 1996-08-27
15 1996-09-27
13 2008-10-18
3 2008-11-18
8 2008-11-18
4 1998-01-02
9 1998-01-02
14 1998-01-02
1 2003-01-31
db<>fiddle here

How to create a rolling period-over-period comparison in Redshift SQL

I have the following query that pulls all records from a Redshift table from January 1st of the current year through the final date of the most recent, full quarter.
SELECT *
FROM table
WHERE date_value BETWEEN DATE_TRUNC('year',getdate()) AND DATE_TRUNC('quarter',dateadd(day,-1,getdate()));
I now want to create a period-over-period comparison query that returns all records for the previous n months. Ex. if the first query returns all records for Jan - Jun 2022, this query will return all records for Jul - Dec 2021.
Here is what I have so far, however it currently returns Jan - Jun 2021 instead of the desired date range. I've tried playing around with DATEDIFF() instead of DATEADD() but haven't had any luck with that either. Any help is much appreciated.
SELECT *
FROM TABLE
WHERE date_value BETWEEN DATE_TRUNC('year',dateadd(year,-1,getdate())) AND DATE_TRUNC('quarter',dateadd(year,-1,getdate()));

How to get year, month and day from seconds in PostgreSql?

I'm trying to create three columns based on date in seconds format.
My user.updated_at = 1521533490
I would like to get year, month and day separately and put these formatted values to columns for example:
year -> 2018, month -> 11, day -> 23
Does someone know how can I do that in pgSQL?
I would like to get year, month and day separately and put these formated values to columns
Don't do that.
Use a single column of type date or timestamp, depending on your application. Not every combination of your three columns will be a valid date. But every value in a single column of type date will be a valid date.
If you need the parts of a date separately, use PostgreSQL's date/time functions.
Try this approche to get differents arguments, then you can do whatever you want:
SELECT to_timestamp(1521533490); //2018-03-20T08:11:30.000Z
SELECT to_char(to_timestamp(1521533490), 'HH'); // 08 Hour
SELECT to_char(to_timestamp(1521533490), 'MI'); // 11 Minutes
SELECT to_char(to_timestamp(1521533490), 'SS'); // 30 Seconds
SELECT to_char(to_timestamp(1521533490), 'DD'); // 20 Day
SELECT to_char(to_timestamp(1521533490), 'Mon'); // MAR Month
SELECT to_char(to_timestamp(1521533490), 'YYYY'); // 2018 Year
Use the EXTRACT function.
SELECT to_timestamp(updated_at) "Date",
EXTRACT(YEAR FROM (to_timestamp(updated_at))) "Year",
EXTRACT(MONTH FROM (to_timestamp(updated_at))) "Month",
EXTRACT(DAY FROM (to_timestamp(updated_at))) "Day"
FROM users
Output
Date Year Month Day
2018-03-20T08:11:30Z 2018 3 20
SQL Fiddle: http://sqlfiddle.com/#!15/afe0e/15/0
More information on the EXTRACT function.

Summarising MONTH value

I have a simple statement that starts:
SELECT a.product, MONTH(a.saledate) AS Month, Count(*) AS Total
Which yields, for example,
Product Month Total
Bike 8 1000
Please can anyone advise if it's possible to add the month's name to this query and also, is it possible to get a monthly total to appear as well?
Thanks!
The query in your example counts all the rows in your table, then presents that count next to a randomly chosen row's product and sale date. That's -- almost certainly -- not what you want. MySQL is quirky that way. Other DBMSs reject your example query.
If you want to display a monthly summary of product sold, here's the basic query:
SELECT a.product,
LAST_DAY(a.saledate) AS month_ending,
COUNT(*) AS Total
FROM table a
GROUP BY a.product, LAST_DAY(a.saledate)
The LAST_DAY() function is a great way to extract month and year from a date.
Finally, if you want to display the text name of the month, you can use the DATE_FORMAT() function to do that. %b as a format specifier gives a three-letter month name, and %M gives the full month name. So this query will do it.
SELECT a.product,
LAST_DAY(a.saledate) AS month_ending,
DATE_FORMAT(LAST_DAY(a.saledate), '%M %Y')) AS month
COUNT(*) AS Total
FROM table a
GROUP BY a.product, LAST_DAY(a.saledate)
In SQL Server 2012+ you can use the EOMONTH() function in place of LAST_DAY().
In SQL Server 2008+ you can use DATENAME(mm, a.saledate) to retrieve the month name from a date.
There are two ways of getting month name
1)
SUBSTRING('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ', (MONTH(a.saledate) * 4) - 3, 3)
2)
DATENAME(month, a.saledate)
Some poeple say You might be using MYSQL:
Then getting month name will be:
SELECT MONTHNAME( a.saledate);