SQL how to sum quantity monthly for report - sql

I have table like this
PartID Quantity Date
12315 1 2017-01-24
51245 3 2017-01-26
12345 4 2017-02-20
12415 1 2017-02-25
..... .. ...........
I need for the report result like:
Monthly Quantity January: 4
Monthly Quantity February: 5
So in my report - every column should have the name of month and include all quantities (count) for this month. Any help would be appreciated.

I think you are looking for a query like:
select month(Date), sum(Quantity)
from table
group by month(Date)

In MS SQL :
You can use something like this:
DATENAME() function returns a specified part of a given date, as a string value.
To know more about DATENAME(), please visit https://www.w3schools.com/sql/func_sqlserver_datename.asp
CAST() function converts an expression from one data type to another data type.
To know more about CAST(), please visit https://www.w3schools.com/sql/func_sqlserver_cast.asp
SELECT
MONTH(Date) AS MonthNo,
'Monthly Quantity ' + DATENAME(MONTH, date) + ' : ' + CAST(SUM(Quantity) AS nvarchar(10)) AS Monthwise
FROM parttable
GROUP BY DATENAME(MONTH, date),
MONTH(Date)
ORDER BY MONTH(Date)
So the output will be

Two options you can have :
1) create another column and store name of month in it .
PartID Quantity Date. Month
12315 1 2017-01-24. January
51245 3 2017-01-26. January
12345 4 2017-02-20. February
12415 1 2017-02-25. February
And use below query to get the report as you needed :
Select sum(Quantity) as total_quantity, Month from table group by Month
2) by using inbuilt date and time functions of MySQL.
Select sum(Quantity) as total_quantity, Month(Date) as month from table group by Month(Date)

Which database are you using?
On SQL Server:
SELECT
DATENAME(month, Date) AS 'Month Name', SUM(Quantity)
FROM table
GROUP BY DATENAME(month, Date)

Related

Remove Duplicates and show Total sales by year and month

i am trying to work with this query to produce a list of all 11 years and 12 months within the years with the sales data for each month. Any suggestions? this is my query so far.
SELECT
distinct(extract(year from date)) as year
, sum(sale_dollars) as year_sales
from `project-1-349215.Dataset.sales`
group by date
it just creates a long list of over 2000 results when i am expecting 132 max one for each month in the years.
You should change your group by statement if you have more results than you expected.
You can try:
group by YEAR(date), MONTH(date)
or
group by EXTRACT(YEAR_MONTH FROM date)
A Grouping function is for takes a subsection of the date in your case year and moth and collect all rows that fit, and sum it up,
So a sĀ“GROUp BY date makes no sense, what so ever as you don't want the sum of every day
So make this
SELECT
extract(year from date) as year
,extract(MONTH from date) as month
, sum(sale_dollars) as year_sales
from `project-1-349215.Dataset.sales`
group by 1,2
Or you can combine both year and month
SELECT
extract(YEAR_MONTH from date) as year
, sum(sale_dollars) as year_sales
from `project-1-349215.Dataset.sales`
group by 1

Add same day in a year month column

I am trying to add the day 15 in each of the year_month column record.
Here an example of what I want to achieve. This applies to all the year_month record. Basically all the year_month records should contain a 15 after the month
From
year month year_month
-----------------------------
2019 4 2019-04
2019 6 2019-06
To
year month year_month
-----------------------------
2019 4 2019-04-15
2019 6 2019-06-15
Didn't try any code yet as honestly I don't know how to start.
You can use datefromparts() function. It takes year, month and day as arguments and produces a date from it. You can hardcoded day 15.
select datefromparts(year, month, 15)
One more way to do using CAST() and CONCAT()
SELECT CAST(CONCAT([year], '-', [month], '-15') AS DATE)
Example: SELECT CAST(CONCAT(2019, '-', 4, '-15') AS DATE) will result as 2019-04-15
If year_month is a string, then:
update t
set year_month = year_month + '-15';
Once you do this, I would suggest changing the type to a date:
alter table t alter year_month date;
Or, if you just want a date in the table when you select it:
alter table t add year_month_date as (convert(date, year_month + '-15'));
This adds a computed column which is a date to the table. You can then select it and use it in queries as you would any other column.

SUM of total per month

I have this table with two columns:
Price Date
45.00 12/06/2015 12:32:54 AM
455.98 22/06/2015 11:00:32 AM
32.00 08/07/2015 09:11:45 AM
98.00 11/07/2015 19:22:32 PM
(the date is in the format DD/MM/YYYY)
I need to get the sum of prices grouped by month and for this I need to find a code in sqlite that cut some parts of that date only getting the month (...06...)(...07...). Below is my code used for some parts of the SELECT:
SELECT SUM(CAST(price as INTEGER)) as TOTALPERMONTH FROM cart_history GROUP BY ....... ORDER BY ID
How I can grouped by some parts of that row?
Use STRFTIME function
select SUM(CAST(price as INTEGER)) as Total,
strftime('%m', dateField) as Month
From yourtable
Group by strftime('%m', dateField)
use HAVING clause.
SELECT SUM(CAST(price as INTEGER)) as TOTALPERMONTH
FROM cart_history,Date
GROUP BY Date
HAVING Date IN('your dates ')'

How can I display a result of data in order by a month-year combine column in sql?

I want to display a below data in order by first take month and then year
count Date
------- ----------
5 Aug 2011
6 jan 2008
10 feb 2009
I want to result as a first display a 2008 year, then 2009 like wise ordering wise date and also should display month in order wise
Assumptions:
You are trying to count records grouping by year and month
You have a table named Table1 with a column named EventDate
You are using SQL Server (you didn't specify)
You don't mind having extra columns in the output (you can ignore the columns you don't need)
You want to order by count, then year, then month
Based on these assumptions, the following should give you what you want.
SELECT COUNT(*) AS Count,
CONVERT(CHAR(3), DATENAME(MONTH, EventDate))
+ ' ' + CONVERT(CHAR(4), YEAR(EventDate)) AS Date,
MIN(YEAR(EventDate)) AS Year, MIN(MONTH(EventDate)) AS Month
FROM Table1
GROUP BY CONVERT(CHAR(4), YEAR(EventDate)),
CONVERT(CHAR(3), DATENAME(MONTH, EventDate))
ORDER BY COUNT(*), Year, Month
Here is the solution..
select convert(char(3), Date, 0)+' '+RIGHT(CONVERT(varchar, YEAR(Date)), 4) as [Date]
,count(id) as Count from Tabletest
group by convert(char(3), Date, 0)+' '+ RIGHT(CONVERT(varchar, YEAR(Date)), 4),month(Date),YEAR(Date)
order by YEAR(Date),MONTH(Date)

How to group quantities in a period of time

Im trying to group quantities regarding a time or period, i have the next table
SALES
SALES_DATE
SALES_ITEM
SALES_QUANTITY
The query that im doing it's
SELECT DATE,ITEM,SUM(QUANTITY)
FROM SALES
WHERE DATE BETWEEN "DATE1" AND "DATE2";
The problem is that i dont need the DATE to appear, if i look for the sales of october it should appear the sum of october without showing the date... Thank you very much for your help
Example:
What i get...
DATE ITEM SALES
2012-06-12 14152 7
2012-06-14 14152 15
2012-06-16 14157 25
What i need: query between 06-12 and 06-16
ITEM SALES
14152 22
14157 25
Thanks you very much
If you want the sum by month, you can include that in the group by expression. Here is one way:
SELECT extract(year from DATE) as yr, extract(month from date) as mon, ITEM, SUM(QUANTITY)
FROM SALES
WHERE DATE BETWEEN "DATE1" AND "DATE2"
group by extract(year from DATE), extract(month from date)
order by 1, 2
Although extract is standard SQL, not all databases support it. For instance, you might use to_char(date, 'YYYY-MM') in Oracle or datepart(month, date) in SQL Server.