Total sales per month - sql-server

If you have a table with SALES {Customer_ID, Price, SmallDateTime date}. How do you report all sales per month?
SELECT Sum(Price) As Total Sales FROM SALES Group By What Having What

SELECT YEAR(date) as SalesYear,
MONTH(date) as SalesMonth,
SUM(Price) AS TotalSales
FROM Sales
GROUP BY YEAR(date), MONTH(date)
ORDER BY YEAR(date), MONTH(date)

SELECT CONVERT(CHAR(7), SmallDateTime, 120) as Year_Month,
SUM(Price)
FROM Sales
GROUP BY CONVERT(CHAR(7), SmallDateTime, 120)
ORDER BY Year_Month

Another solution is to calculate the first day of the month
Select DateAdd(d,DateDiff(d,0,[Date])-DatePart(d,[Date])+1,0)
, Sum( Price )
From Sales
Group By DateAdd(d,DateDiff(d,0,[Date])-DatePart(d,[Date])+1,0)

Try this
SELECT A.CAL_YEAR AS YEAR ,A.CAL_WEEK AS WEEK,SUM(B.SUM_OF_PROFIT) as profit FROM
CALANDER A,SALES_FACT B WHERE A.DATE_ID=B.DATE_ID
GROUP BY A.CAL_YEAR,A.CAL_WEEK;

Related

Retrieve count of orders by specific (each) date , example in Saturday has 20 orders in Sunday has 22 how can do it

Select
ORDER_DATE as count_date,
(select Count(ORDER_ID_TRN) as 'qty' from Orders)
from
Orders
group by
ORDER_DATE
Having ORDER_DATE = '2019-12-14'
How can I do that?
If you group your data then aggregate functions like count() apply to each group and not the complete result set
Select
ORDER_DATE as count_date,
Count(ORDER_ID_TRN) as qty
from
Orders
group by
ORDER_DATE
Simply use count(*) for counting orders.
Select
ORDER_DATE as count_date,
count(*)
from
Orders
group by
ORDER_DATE

SQL Count number of records that appear more than once a month and aggregate by month

I am going through a ton of records and want to count the number of times an ID is updated more than once a month.
SELECT COUNT(*) AS Frequency, MONTH(Date) AS MM, YEAR(Date) AS YYYY, Id
FROM data
WHERE -- [some filtering]
AND Date <= -- end date
AND Date >= -- start date
GROUP BY Date, Id
HAVING COUNT(*) > 1
ORDER BY MM DESC
Now what happens there is that I am only finding the number of Id's that are updated more than once per day. What I want to do is to group the ID's by month.
I have tried using the column MM in my GROUP BY but then I get error codes stating that these are invalid column selections.
I tried using the following GROUP BY:
GROUP BY DATEPART(MONTH, Date), Id
All I get is continuous 8120 errors and I cannot figure out how I should put this together. Any help would be greatly appreciated.
First let's fix your query so it does proper aggregation:
SELECT COUNT(*) AS Frequency, MONTH(Date) AS MM, YEAR(Date) AS YYYY, Id
FROM data with(NOLOCK)
WHERE -- [some filtering]
GROUP BY MONTH(Date), YEAR(Date), Id
HAVING COUNT(*) > 1
ORDER BY YYYY, MM DESC
This gives you a list of Ids that were updated more than once each month.
Now, if you want to know how many Ids were updated more than once each month, you can add another level of aggregation:
SELECT MM, YYY, COUNT(*)
FROM (
SELECT COUNT(*) AS Frequency, MONTH(Date) AS MM, YEAR(Date) AS YYYY, Id
FROM data with(NOLOCK)
WHERE -- [some filtering]
GROUP BY MONTH(Date), YEAR(Date), Id
HAVING COUNT(*) > 1
) x
ORDER BY YYYY, MM DESC

Group By Month and Year to get distinct month and years then combine as a single date SQL

SELECT CONVERT(DATE,CAST([Year] AS VARCHAR(4))+'-'+
CAST([Month] AS VARCHAR(2))+'-'+
CAST('1' AS VARCHAR(2))) Date
FROM (SELECT YEAR(Date) as [Year], MONTH(Date) as [Month] FROM [dbo].[Data]
GROUP BY YEAR(Date), MONTH(Date)) x
ORDER BY Date DESC
Is there a better way to doinq this with a single query?
The query should return the unique month and year from a table but as combined Date.
IF OBJECT_ID ('tempdb..#TempT') IS NOT NULL DROP TABLE #TempT
CREATE TABLE #TempT(
dt datetime)
INSERT INTO #TempT (dt) VALUES
('2016-10-11'),
('2016-10-3'),
('2016-9-13'),
('2016-9-16')
SELECT DISTINCT CAST(DATEADD(month, DATEDIFF(month, 0, dt), 0) as DATE) AS Dates
from #TempT
Sample data is really a necessity for these kinds of questions, but this function could also be a help for you DATEFROMPARTS
SELECT
DATEFROMPARTS([Year], [Month], 01)
FROM
(
SELECT
YEAR(Date) as [Year],
MONTH(Date) as [Month]
FROM [dbo].[Data]
GROUP BY YEAR(Date), MONTH(Date)
) x
ORDER BY Date DESC
This code takes whatever date you enter and evaluates it as the first of the month. Effectively looking at only month and year:
SELECT
DATEADD(MM,DATEDIFF(MM,0, [Date] ),0) AS [YearMonth]
FROM [dbo].[Data]
ORDER BY
DATEADD(MM,DATEDIFF(MM,0, [Date] ),0)
;

Calculating total cost per month sql

I want to work out the total cost for a specific month, my code is generating the items purchased that month but it is calculating the cost of all the items instead of just that month
set dateformat 'dmy'
Select OrderID, OrderDate, UnitPrice
From SupplierOrder
Where OrderDate between '01/07/2015' AND '31/07/2015'
group by OrderID, OrderDate, UnitPrice
SELECT SUM(UnitPrice) AS TotalCostOfItems FROM SupplierOrder;
I'm a little confused as to why you have two queries. Regardless, the first is filtering on date, but the second is not - hence the second query is calculating the summed unit price for all orders. To get the second query working, simply add the same filter that you have for the first
SELECT SUM(UnitPrice) AS TotalCostOfItems FROM SupplierOrder Where OrderDate between '01/07/2015' AND '31/07/2015';
you should add the where statement after your select.
SELECT SUM(UnitPrice) AS TotalCostOfItems FROM SupplierOrder
Where OrderDate between '01/07/2015' AND '31/07/2015'
hope this help
Try this
select mnth, yr, sum(unitprice) unitprice
from
(
SELECT
DATEPART(month,orderdate) mnth,
datepart(year,orderdate) yr,
unitprice
from supplierorder
) a
group by mnth, yr

Get month wise records of payments for last six months

Table Structure:
Invoice
Invoice Payment
Current Query:
;WITH cte (clientid, invoiceid, paid, disc)
As
(
Select client_id clientId, vinvoice_Id invoiceId, sum(amount_received) paid, sum(discount) disc
From tbl_Vendor_Invoice_Payment
Group by vinvoice_id, client_id
)
Select I.date, I.total_price, Isnull(paid, 0) Paid, (Total_price - Isnull(paid,0) - Isnull(disc,0)) Balance
From tbl_Vendor_invoice I Left join cte On I.client_id = cte.clientId
And I.vinvoice_id = cte.invoiceid
order by vinvoice_id desc
Output:
But my requirement is to get month-wise result of last six months as below:
Month total_price Paid Balance
--------------------------------------------
October 800.00 750.00 50.00
September 200.00 100.00 100.00
August 350.00 350.00 0.00
.........
Can anyone please help me to get this ?
SELECT DATEADD(month,DATEDIFF(month,0,[vi.date]),0), SUM(vi.total_price) total_price, SUM(vip.amount_received) Paid, SUM(vip.balance) balance
FROM tbl_Vendor_Invoice vi
INNER JOIN tbl_Vendor_Invoice_Payment vip
on vi.vInvoice_Id = vip.vInvoice_Id
WHERE [vi.date] >= DATEADD(month, -6, DATEADD(month,DATEDIFF(month,0,[vi.date]),0))
GROUP BY DATEADD(month,DATEDIFF(month,0,[vi.date]),0)
This will group by the first day of the month, including the year. If you want just the month name, you can extract that using DATENAME(month, [date]).
I have found an answer:
;WITH cte (clientid, invoiceid, paid, disc)
As
(
Select client_id clientId, vinvoice_Id invoiceId, sum(amount_received) paid, sum(discount) disc
From tbl_Vendor_Invoice_Payment
Group by vinvoice_id, client_id
)
SELECT DATENAME(month, [date]) mMonth, SUM(Isnull(paid, 0)) Amount_Paid
FROM tbl_Vendor_Invoice vi
INNER JOIN cte vip on vi.vinvoice_Id = vip.invoiceid
WHERE [date] >= DATEADD(month, -6, DATEADD(month,DATEDIFF(month,0,getdate()),0))
GROUP BY DATENAME(month, [date]), MONTH([date])
ORDER BY MONTH([date])

Resources