Calculating total cost per month sql - sql-server

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

Related

Select distinct values from one column and sum another column

I have this stored procedure shown here which calculates the order qty, rejection and percentage for rejection against each department, but I have an issue: for summing the order qty, I need to consider only distinct order number to sum the order qty, but for rejection qty all rows should be considered. How to achieve this?
Stored procedure:
CREATE PROCEDURE orderqtyper
#fromDate Date,
#toDate Date
AS
SELECT
Department,
SUM(Order_Qty) AS [Order Qty],
SUM(Rejection_Qty) AS [Rejection Qty],
FORMAT((SUM(Rejection_Qty) * 100.0 / NULLIF(SUM(Order_Qty), 0) / 100), 'P') AS Percentage
FROM
Semicon_NPD
WHERE
(Date BETWEEN #fromDate AND #toDate)
GROUP BY
Department
ORDER BY
Percentage DESC
Sample table:
Current result:
Expected result (if the the order number is the same, it should take sum of only unique values for order qty but sum of all for rejections qty):
You can use DISTINCT as SUM(DISTINCT <column_name>)
create proc orderqtyper
#fromDate Date,
#toDate Date
as
SELECT Department,
SUM(DISTINCT Order_Qty)as [Order Qty],
SUM(Rejection_Qty) as [Rejection Qty],
Format((SUM(Rejection_Qty) * 100.0 / NULLIF(SUM(Order_Qty), 0)/100),'P') AS Percentage
FROM Semicon_NPD
Where (Date between #fromDate and #toDate)
GROUP BY Department
order by Percentage desc
go

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

SQL Server: selecting distinct values per one column

I was wandering if it's possible to filter select results removing values that partially overlap
For example below, i have thousands of records, but i need the 'week date' value to be unqiue, and in case of duplicates the one with the highest value should remain.
emplo project_id Value week_Date week_ActualStart week_ActualEnd
A0001 project001 100 2015-12-28 2015-12-28 2016-01-03
A0001 project001 60 2015-12-28 2016-01-01 2016-01-03
So only the first row should remain.
I could really use someone's advice
Try something like the following:
;WITH WeekDateCte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY emplno, week_Date ORDER BY Value DESC) RowNo
FROM employee
)
SELECT *
FROM WeekDateCte
WHERE RowNo = 1
For more information about ROW_NUMBER function, check here.
NOTE: ROW_NUMBER() returns BIGINT.
You can use ROW_NUMBER for this:
SELECT emplno, project_id, Value, week_Date,
week_ActualStart, week_ActualEnd
FROM (
SELECT emplno, project_id, Value, week_Date,
week_ActualStart, week_ActualEnd,
ROW_NUMBER() OVER (PARTITION BY emplno, week_Date
ORDER BY Value DESC) AS rn
FROM mytable) AS t
WHERE t.rn = 1
The query picks the row having the greatest Value per emplno, week_Date slice.

Total sales per month

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;

Resources