How to query months and group them in SQL Server - sql

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.

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.

Group by clause query with derived fields

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.

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);

SQL QUERY - Filter date from the beginning

How to write SQL query that will sum the amount from the previous days/years. Like from the start.
Scenario I want to compute accumulated sales of the store from the day it was opened.
Example
SELECT SUM(AMOUNT)
FROM TransactionTable
WHERE TransactionDate = ???
The plan that I have is to query on this table and get the oldest transaction date record, then I will use that in the WHERE condition. Do you think that it is the best solution?
You can try below using having min(transaction) which will give you the date when transaction first started
select sum (amt) from
(
SELECT SUM(AMOUNT) as amt from TransactionTable
group by TransactionDate
having TransactionDate between min(TransactionDate) and getdate()
)A
To compute accumulated sales of the store from the day it started you can use SUM with OVER clause
SELECT TransactionDate, SUM(AMOUNT) OVER (ORDER BY TransactionDate) AS AccumulatedSales
FROM TransactionTable
use group by TransactionDate
SELECT convert(date,TransactionDate), SUM(AMOUNT) from TransactionTable
group by convert(date,TransactionDate)

How do I correctly use the SQL Sum function with multiple variables and grouping?

I am trying to write an SQL statement based on the following code.
CREATE TABLE mytable (
year INTEGER,
month INTEGER,
day INTEGER,
hoursWorked INTEGER )
Assuming that each employee works multiple days over each month in a 3 year period.
I need to write an sql statement that returns the total hours worked in each month, grouped by earliest year/month first.
I tried doing this, but I don't think it is correct:
SELECT Sum(hoursWorked) FROM mytable
ORDER BY(year,month)
GROUP BY(month);
I am a little confused about how to operate the sum function in conjunction with thee GROUP BY or ORDER BY function. How does one go about doing this?
Try this:
SELECT year, month, SUM(hoursWorked)
FROM mytable
GROUP BY year, month
ORDER BY year, month
This way you will have for example:
2014 December 30
2015 January 12
2015 February 40
Fields you want to group by always have be present in SELECT part of query. And vice-versa - what you put in SELECT part, need be also in GROUP BY.
SELECT year, month, Sum(hoursWorked)as workedhours
FROM mytable
GROUP BY year,month
ORDER BY year,month;
You have to group by year and month.
Is this what you are trying to do. This will sum by Year/Month and Order by Year/Month.
Select [Year], [Month], Sum(HoursWorked) as WorkedHours
From mytable
Group By [Year], [Month]
Order by [Year], [Month]
You have to group by year and month, otherwise you will have the hours you worked on March 2014 and 2015 in one record :)
SELECT Sum(hoursWorked) as hoursWorked, year, month
FROM mytable
GROUP BY(year, month)
ORDER BY(year,month)
;