I have a table Logs with fields
Amount,date
I need to get the sum of amount and months grouped by each day
I need to migrate my sqlite code to postgresql but i find the code migration kind of hard.The sqlite code is as follows
SELECT SUM(amount),transaction_date FROM log WHERE user_id = 1 AND strftime('%Y', transaction_date) = '2019' GROUP BY strftime('%d', transaction_date);
What i need to is date and total amount grouped by day for the year 2019
You need extract() function instead of strftime() to get the year and cast transaction_date to date only if it is timestamp, if it is of data type date remove the casting ::date:
SELECT
SUM(amount),
transaction_date::date
FROM log WHERE user_id = 1 AND extract(year from transaction_date) = 2019
GROUP BY transaction_date::date
Related
I am facing an issue extracting the month and year from the data (in Character varying type) >> InvoiceDate in SQL Postgres. I have seen the solution is relatively easy with MySQL function: DATEFROMPARTS as per the below Code which is not available in SQLpostgres. How can I get the same result DATA_PART function in Postgres SQL, but simultaneously I need to change the data type of the column "InvoiceDate" to the date
Select
CustomerID,
min(InvoiceDate) first_purchase_date,
DATEFROMPARTS(year(min(InvoiceDate)), month(min(InvoiceDate)), 1) Cohort_Date
into #cohort
from #online_retail_main
group by CustomerID
The output:
Customer ID| first_purchase_date |Cohort_Date|
-----------+-------------------------+-----------+
12345 | 2010-12-20 15:47:00:00 | 2010-12-01|
I am trying to make a date consits of Year and Month , while the day to be set as 1 for all
Assuming a valid Postgres timestamp:
select date_trunc('month', '2010-12-20 15:47:00.00'::timestamp)::date;
date_trunc
------------
12/01/2010
--or ISO format
set datestyle = 'ISO,MDY';
select date_trunc('month', '2010-12-20 15:47:00.00'::timestamp)::date;
date_trunc
------------
2010-12-01
Uses date_trunc to truncate the timestamp to a month which means the first of the month. Then cast(::date) to a date type. The DateStyle just deals with how the value is presented to the user. The value is not stored formatted.
To do something similar to what you did in MySQL:
select make_date(extract(year from '2010-12-20 15:47:00.00'::timestamp)::integer, extract(month from '2010-12-20 15:47:00.00'::timestamp)::integer, 1);
This uses make_date from here Date/time functions and extract to build a date.
i have a table and have 4 columns
id name from_date to_date
1 vivek 2018-11-12 2018-11-22
2 abc 2018-10-11 2018-10-21
from date and to date is date when my users are allowed to access the website paid data so i want to retrieve data of those user who is able to see my paid data today
CURDATE() might be useful here:
SELECT *
FROM yourTable
WHERE CURDATE() BETWEEN from_date AND to_date;
This answer assumes that the range from from_date to to_date represents a range inclusive on both ends. If not, then the query would change slightly.
I have a table that has a column date and value, what I need is to sum a value showing just one date column.
Ex:
I have this:
date value
2018-01-01 150
2018-01-23 140
what I need:
date sum(value)
2018-01 290
Simple solution to get sums per month:
SELECT to_char(date, 'YYYY-MM') AS mon, sum(value) AS sum_value
FROM tbl
GROUP BY 1;
For large tables it's cheaper to group on date_trunc('month', date) instead.
Related:
Concatenate multiple result rows of one column into one, group by another column
Group and count events per time intervals, plus running total
How to get the date and time from timestamp in PostgreSQL select query?
I have a customer_visit table, my requirement is to get the records of previous month starting from 3rd date to current month 2nd date and which contains flag 'Y'. How can I achieve this in Postgres. Following is the sample of my table. Any help is appreciated.
Use make_date() function from postgres to construct the date and query on it.
select * from customer_visit
where created_time > make_date(2018, date_part('month', now())::int - 1, 03)
and created_time <= make_date(2018, date_part('month', now())::int, 02)
and flag = 'Y';
You can use date_part('year', timestamp) to extract year also from current date.
I have a table with a row for each sale of a product. These rows include a date. I want to know the number of products sold for each distinct day in a range (user specifies the begin and end dates.) There is a distinct row for each sale, so on days where several products were sold, several rows with this date exist. I want to count the number of these rows, with the same date. How might this be done efficiently in postgresql?
Example
2015-01-02: 0
2015-01-03: 7
2015-01-04: 2
Assuming the date column is stored as a datetime, something like this should get you in the right direction:
SELECT date_trunc('day', TIMESTAMP sales.date) as day, COUNT(*)
FROM sales
WHERE day >= start and <= end
GROUP BY day;
where start and end are filled in by the user.
More documentation for postgres's date features found here:
http://www.postgresql.org/docs/9.4/static/functions-datetime.html
If they are not datetime, you can use the to_date function with the appropriate format string to convert to datetime and the use the solution above:
SELECT date_trunc('day', to_date('2015-01-02', 'YYYY-MM-DD'));
http://www.postgresql.org/docs/9.4/static/functions-formatting.html