How to get transaction count per year in SQL - 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);

Related

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.

First user by category

How can I count the new users for each category who bought in the category for the first by year? For instance, 2015-2020 by year, if someone bought in 2015 for the first it will be counted as a new uesr in 2015 but not in 2016-2020.
Table_1 (Columns: product_name, date, category, sales, user_id)
Want to get the result as bleow
You’ll want to start with a sub query to get the first date each user purchased in the category. This is a pretty straightforward group by problem:
select
user_id,
category,
min(date) as first_category_purchase
from my_table
group by user_id, category;
Next, you can use Postgres’s date_trunc function to group by year and category, using your first query as a sub query:
select
category,
date_trunc('year', first_category_purchase)
count(*)
from (
select
user_id,
category,
min(date) as first_category_purchase
from my_table
group by user_id, category
) a
group by 1, 2;
In Postgres, one method is group by after a distinct on:
select date, count(*) as num_new_users
from (select distinct on (user_id, category) t.*
from t
order by user_id, category, date asc
) d
group by date
order by date;
If date is really a date and not a year, then you need something like to_char() or date_trunc() to convert it to a year.

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)

Simple sql query, but stuck on it

database mode
The only relevant table is 'employee' in the database model.
Asked: In which month are the most employee's birthdays?
By using
SELECT DATEPART(m, dateofbirth) AS month
FROM employee
I can actually see all the months for every employee and count it myself.
But how can I show the most common birthday month?
Thanks in advance!
recent output (for comment below)
You need to use GROUP BY. This groups up the separate month values. Once you've done that, you can apply COUNT, and then order the values in descending order on that statistic. Then you need to wrap that logic in a Common Table Expression, so you can select just the months that have the maximum COUNT.
WITH ranking AS (
SELECT
DATEPART(m, dateofbirth) AS month,
COUNT(*) as ct
FROM DM_MTA.dbo.employee
GROUP BY DATEPART(m, dateofbirth)
)
select
month
from
ranking
where ct = (select max(ct) from ranking)
This would give you exact month you're looking for:
SELECT TOP 1 DATEPART(m, dateofbirth) AS month
FROM employee
GROUP BY DATEPART(m, dateofbirth)
ORDER BY count(DATEPART(m, dateofbirth)) DESC

SQL: aggregation (group by like) in a column

I have a select that group by customers spending of the past two months by customer id and date. What I need to do is to associate for each row the total amount spent by that customer in the whole first week of the two month time period (of course it would be a repetition for each row of one customer, but for some reason that's ok ). do you know how to do that without using a sub query as a column?
I was thinking using some combination of OVER PARTITION, but could not figure out how...
Thanks a lot in advance.
Raffaele
Query:
select customer_id, date, sum(sales)
from transaction_table
group by customer_id, date
If it's a specific first week (e.g. you always want the first week of the year, and your data set normally includes January and February spending), you could use sum(case...):
select distinct customer_id, date, sum(sales) over (partition by customer_ID, date)
, sum(case when date between '1/1/15' and '1/7/15' then Sales end)
over (partition by customer_id) as FirstWeekSales
from transaction_table
In response to the comments below; I'm not sure if this is what you're looking for, since it involves a subquery, but here's my best shot:
select distinct a.customer_id, date
, sum(sales) over (partition by a.customer_ID, date)
, sum(case when date between mindate and dateadd(DD, 7, mindate)
then Sales end)
over (partition by a.customer_id) as FirstWeekSales
from transaction_table a
left join
(select customer_ID, min(date) as mindate
from transaction_table group by customer_ID) b
on a.customer_ID = b.customer_ID