Group by clause query with derived fields - sql

My table has quantity, sale price and sale date, and I need to derive the sales price and group the sales by month. I used the below but wasn't able to get the group by clause right.
select
(QUANTITY*SALEPRICE) as sales,
DATEPART (month, saledate) as MM
from [dbo].[PETSALE]
GROUP BY DATEPART (month, saledate)

You would need an aggregate function in the select clause to make your statement a valid aggregate query.
I would phrase your query as:
select
sum(quantity * saleprice) as sales, -- aggregate function
year(saledate) yyyy,
month(saledate) mm
from [dbo].[petsale]
group by year(saledate), month(saledate)
Note that I added the sales year to the select and group by clauses: in case your data spreads over more than 12 months, you probably don't want sales from the same month in different years to be grouped together. But if you do, you can just remove year(saledate) from the query.

Related

Match month of outer query to month in subquery

I am attempting to get the total of a column (Amount) by Month:
SELECT
Date,
DATEPART(month, Date) AS Month,
DATEPART(year, Date) AS year,
(
SELECT
SUM(Amount)
FROM Transactions AS T
WHERE DATEPART(month, Date) = DATEPART(month, T.Date)
) AS Amount
FROM Transactions AS T
I know that this could be done in a straightforward manner using a group by clause as follows:
SELECT
DATEPART(month, Date) AS Month,
SUM(Amount) AS Total
FROM Transactions AS T
GROUP BY DATEPART(month, Date)
However I need to use subquery instead as the subquery will contain additional conditions and joins to assist in the calculation of a total based on rules from other tables.
I expected the first query to return all months and their respective totals, in the subquery I am matching Month of the transactions with the Month of the outer query row. However it simply returns the full SUM of all months for each month.
My guess is that my attempt to match the month is not quite correct, but I can't see how to fix.

How to query months and group them in SQL Server

I have a query which sums up the transactions of a given month over a given period in SQL Server. I want to list the months and transactions in a table but the DATENAME() function is only returning one month i.e January in the list. The query is as shown below ..
SELECT
DATENAME(MONTH, DATEPART(MONTH FROM TransactionDate)) AS Month_Name,
SUM(ABS(Income)) AS Income
FROM
Transactions
GROUP BY
DATEPART(MONTH FROM TransactionDate)
Please help ...
Try this,
SELECT
datename(mm,TransactionDate) AS Month_Name,
SUM(ABS(Income)) AS Income
FROM Transactions
GROUP BY
datename(mm, TransactionDate)
Try this:
select datename(mm, TransactionDate) month_name, sum(income) income
from transactions
group by month_name
Remove some unnecessary to tune up the performance.

SQL - Aggregate dates from different columns into Month/Year table

So I have an 'Orders' table that lists the 'Ordered' and 'Shipped' dates for each order.
These are custom products and it takes 1 week to fill orders.
This is pretty representative of the table I have:
I want to aggregate this into a table so that I can see how many orders were ordered and shipped for each month during the date range specified when the report is run, and I want the Months and years to automatically populate without me having to hardcode for each month and year:
What's the best way to do this with SQL?
I eventually want to place the aggregated table into an SSRS report so that you can expand/collapse each year, if needed.
Date/time functions are notoriously database dependent. Here is a typical approach, though:
select yyyy, mm, sum(num_ordered), sum(num_shipped)
from ((select year(ordered) as yyyy, month(ordered) as mm, count(*) as num_ordered, 0 as num_shipped
from orders
group by year(ordered), month(ordered)
) union all
(select year(shipped) as yyyy, month(shipped) as mm, 0 count(*) as num_shipped
from orders
group by year(shipped), month(shipped)
)
) ym
group by yyyy, mm;

How to get transaction count per year in SQL

I want to get yearly transaction every category
SELECT DATEPART(YEAR, trans_date) AS YEAR, SUM(CAST(doc_no as int))
FROM transac_tbl1 WHERE agent_id IN ('transaction1', 'transaction2')
GROUP BY trans_date
You obviously have an issue with the GROUP BY. I would also suggest the YEAR() function:
SELECT YEAR(trans_date) AS YEAR, SUM(doc_no)
FROM transac_tbl1
WHERE agent_id in ('transaction1', 'transaction2')
GROUP BY YEAR(trans_date);
I'm not sure why you are casting doc_no to an int. I wonder if you really just want the count:
SELECT YEAR(trans_date) AS YEAR, COUNT(*)
FROM transac_tbl1
WHERE agent_id in ('transaction1', 'transaction2')
GROUP BY YEAR(trans_date);

How to group by year with the year showing only once

I have tried using the following query
select distinct Year (SaleDate) AS SaleYear,Max(SalePrice)
from Sale
group by SaleDate
The years 2010 and 2014 are showing twice,even though i used distinct and group by. the amounts in Maxprice are different as well. am i doing something wrong here?
You need to repeat year() in the group by:
select Year(SaleDate) AS SaleYear, Max(SalePrice)
from Sale
group by year(SaleDate);
SELECT DISTINCT with GROUP BY is almost never correct. All that your query does is aggregate by SaleDate and in the result set extract the year. That is why you see duplicates.