Query to Get the "prix_total" per month in SQLite3 - sql

I have a table with the following attributes, "id_commande, id_client, id_agent, D_commande, Heure_commande, prix_total".
D_commande represents DATE OF SALE
prix_total represents the price of a sale.
when I tried to create a Query to Get the "prix_total" per month. I struggled.
notice that I tried this query but I don't get the correct result:
SELECT sum(prix_total)
FROM commande
GROUP BY (
SELECT strftime('%y','D_commande')
)

You can use
SELECT strftime('%Y-%m',D_commande) AS month,
SUM(prix_total) AS prix_total
FROM commande
GROUP BY strftime('%Y-%m',D_commande)
ORDER BY month
in order to get the result partitioned by months

SELECT sum(prix_total)
FROM commande
GROUP BY strftime('%Y',D_commande)
, strftime('%m',D_commande)
Here is a demo
And also you can order by year and month:
ORDER BY strftime('%Y',D_commande)
, strftime('%m',D_commande)

Related

Retrieving Date function in sql

I have an employee table with the hire_date column.
I am stuck with one query related to the date function, where I have used data type 'DATE' to insert date of hiring and using DATE_FORMAT fun. to retrieve no. of employees hired in every month, but in SQL-server it is not supporting the date_format function.
I'm using SQL -server
Query: - list of the no.of employee hired every month in ascending order.
select date_format(hire_date,'%b') month, count(*)
from employee
group by DATE_FORMAT(hire_date,'%b')
order by month
date_format(hire_date,'%b') in MySQL will return abbreviated monthname. However, you can still have this functionality by combining MONTHNAME with LEFT in SQL Server.
select LEFT(DATENAME(MONTH,hire_date),3) month, count(*)
from employee
group by LEFT(DATENAME(MONTH,hire_date),3)
order by month
select Month(Hire_Date),count('x') 'count' from employee
group by Month(Hire_Date)
order by Month(Hire_Date) asc
Instead, you can directly use:
select MONTH(hire_date), count(*)
from employee
group by MONTH(hire_date)
order by MONTH(hire_date)
or
select hire_date.MONTH, count(*)
from employee
group by hire_date.MONTH
order by hire_date.MONTH

Redshift list 3 most recent values per year

I have a column of dates and I want to find the three maximum dates for each year I have tried the following.
select max(date, rank() over (partition by SPLIT_PART(date, '-', 1) order by date desc)
from table
;
My desired output would be
2013,2010-12-31
2013,2010-12-30
2013,2010-12-29
also there are repeats dates in the table so I would have to filter those out as well
Assuming there are no duplicate dates, you can partition by the year part of date and get the latest 3 dates per year. Use distinct (if needed) in the final query to remove the duplicates, if any.
select yr,date
from (select date_part(year,date) as yr,date
,dense_rank() over (partition by date_part(year,date) order by date desc) as rnk
from table
) t
where rnk<=3

Calculating an AVG of COUNT by MONTH

I'm trying to display the average number of counts/records for each month in 2016. The following code does not display each month, rather displays only the monthly average for 2016:
SELECT AVG(DISTINCT DayCnt) AS AvgCnt
FROM
(
SELECT COUNT(*) As DayCnt
FROM table
WHERE YEAR(Insert_Date) = '2016'
GROUP BY MONTH(Insert_Date)
)
AS AvgCnt
You first should be group your result per month and days and count daily inserted records, after that to get average of per month inserted records try this:
SELECT monthGroup, AVG(DayCnt) AS AvgCnt
FROM
(
SELECT MONTH(Insert_Date) monthGroup, DAY(Insert_Date) dayGroup, COUNT(*) As DayCnt
FROM table
WHERE YEAR(Insert_Date) = '2016'
GROUP BY MONTH(Insert_Date), DAY(Insert_Date)
)
AS AvgCnt
GROUP BY monthGroup
I'm not sure since you didn't post the data but if I understand correctly, this should work for you:
SELECT AVG(DayCnt) AS AvgCnt, mth
FROM
(
SELECT COUNT(*) As DayCnt, MONTH(Insert_Date) as mth
FROM table
WHERE YEAR(Insert_Date) = '2016'
GROUP BY MONTH(Insert_Date), DAY(Insert_Date) )
AS AvgCnt
GROUP BY mth

Select per month

I've got the following table:
purchases(id, item, user, price, time);
The time field is a timestamp.
I need a query that would return one row per month and that row would contain the sum of price for each item in that month.
Try this:
SELECT MONTH(`time`) AS month, SUM(price) AS price
FROM your_table
GROUP BY MONTH(`time`)
If you have more than one year's data you may also want to include the year in your group by:
SELECT YEAR(`time`) AS year, MONTH(`time`) AS month, SUM(price) AS price
FROM your_table
GROUP BY YEAR(`time`), MONTH(`time`)
what about GROUP BY YEAR(DATE(time)) ASC, MONTH(DATE(time)) ASC?

Last day of the month with a twist in SQLPLUS

I would appreciate a little expert help please.
in an SQL SELECT statement I am trying to get the last day with data per month for the last year.
Example, I am easily able to get the last day of each month and join that to my data table, but the problem is, if the last day of the month does not have data, then there is no returned data. What I need is for the SELECT to return the last day with data for the month.
This is probably easy to do, but to be honest, my brain fart is starting to hurt.
I've attached the select below that works for returning the data for only the last day of the month for the last 12 months.
Thanks in advance for your help!
SELECT fd.cust_id,fd.server_name,fd.instance_name,
TRUNC(fd.coll_date) AS coll_date,fd.column_name
FROM super_table fd,
(SELECT TRUNC(daterange,'MM')-1 first_of_month
FROM (
select TRUNC(sysdate-365,'MM') + level as DateRange
from dual
connect by level<=365)
GROUP BY TRUNC(daterange,'MM')) fom
WHERE fd.cust_id = :CUST_ID
AND fd.coll_date > SYSDATE-400
AND TRUNC(fd.coll_date) = fom.first_of_month
GROUP BY fd.cust_id,fd.server_name,fd.instance_name,
TRUNC(fd.coll_date),fd.column_name
ORDER BY fd.server_name,fd.instance_name,TRUNC(fd.coll_date)
You probably need to group your data so that each month's data is in the group, and then within the group select the maximum date present. The sub-query might be:
SELECT MAX(coll_date) AS last_day_of_month
FROM Super_Table AS fd
GROUP BY YEAR(coll_date) * 100 + MONTH(coll_date);
This presumes that the functions YEAR() and MONTH() exist to extract the year and month from a date as an integer value. Clearly, this doesn't constrain the range of dates - you can do that, too. If you don't have the functions in Oracle, then you do some sort of manipulation to get the equivalent result.
Using information from Rhose (thanks):
SELECT MAX(coll_date) AS last_day_of_month
FROM Super_Table AS fd
GROUP BY TO_CHAR(coll_date, 'YYYYMM');
This achieves the same net result, putting all dates from the same calendar month into a group and then determining the maximum value present within that group.
Here's another approach, if ANSI row_number() is supported:
with RevDayRanked(itemDate,rn) as (
select
cast(coll_date as date),
row_number() over (
partition by datediff(month,coll_date,'2000-01-01') -- rewrite datediff as needed for your platform
order by coll_date desc
)
from super_table
)
select itemDate
from RevDayRanked
where rn = 1;
Rows numbered 1 will be nondeterministically chosen among rows on the last active date of the month, so you don't need distinct. If you want information out of the table for all rows on these dates, use rank() over days instead of row_number() over coll_date values, so a value of 1 appears for any row on the last active date of the month, and select the additional columns you need:
with RevDayRanked(cust_id, server_name, coll_date, rk) as (
select
cust_id, server_name, coll_date,
rank() over (
partition by datediff(month,coll_date,'2000-01-01')
order by cast(coll_date as date) desc
)
from super_table
)
select cust_id, server_name, coll_date
from RevDayRanked
where rk = 1;
If row_number() and rank() aren't supported, another approach is this (for the second query above). Select all rows from your table for which there's no row in the table from a later day in the same month.
select
cust_id, server_name, coll_date
from super_table as ST1
where not exists (
select *
from super_table as ST2
where datediff(month,ST1.coll_date,ST2.coll_date) = 0
and cast(ST2.coll_date as date) > cast(ST1.coll_date as date)
)
If you have to do this kind of thing a lot, see if you can create an index over computed columns that hold cast(coll_date as date) and a month indicator like datediff(month,'2001-01-01',coll_date). That'll make more of the predicates SARGs.
Putting the above pieces together, would something like this work for you?
SELECT fd.cust_id,
fd.server_name,
fd.instance_name,
TRUNC(fd.coll_date) AS coll_date,
fd.column_name
FROM super_table fd,
WHERE fd.cust_id = :CUST_ID
AND TRUNC(fd.coll_date) IN (
SELECT MAX(TRUNC(coll_date))
FROM super_table
WHERE coll_date > SYSDATE - 400
AND cust_id = :CUST_ID
GROUP BY TO_CHAR(coll_date,'YYYYMM')
)
GROUP BY fd.cust_id,fd.server_name,fd.instance_name,TRUNC(fd.coll_date),fd.column_name
ORDER BY fd.server_name,fd.instance_name,TRUNC(fd.coll_date)