Finding the Total transactions done by an user in each month - sql

There is a user table(usr) and a transactions(txn) table .The transactions table contains the following fields :- Usr_id, tscn_id,tscn_amt,tscn,date.
I am not able to write the SQL query to find the total transaction amount for each user calculated for each month. Could someone please help me with this ?

Can you try the following:
SELECT
sum(tscn_amt) as total,
Usr_id,to_char(date,'MM') as month
FROM txn
GROUP BY Usr_id, to_char(date,'MM');

You can try something like this :
select sum(tscn_amt) as sum,Usr_id,month(date) from `txn` group by Usr_id, month(date)
Check out the date and time functions in MySQL.

Related

Grouping and Summing in SQL

Good day
I would like to find out how I would Sort and group the following data by grouping the Account and summing the grandTotal of the Account per Month.
This is my current Select statement:
SELECT
tbl_AccountLedger.ledgerName
,tbl_SalesMaster.date
, tbl_SalesMaster.grandTotal
FROM tbl_SalesMaster
INNER JOIN tbl_AccountLedger ON tbl_SalesMaster.ledgerId =tbl_AccountLedger.ledgerId
Here is wat my select statement is bringing back
Now I need to sort this data by summing the grand total for each month for a legerName
You can use the below query on SQL 2012 or higher version. I will suggest please read grouping in SQL.
SELECT
tbl_AccountLedger.ledgerName
,DATEFROMPARTS(YEAR(tbl_SalesMaster.date),MONTH(tbl_SalesMaster.date),1) AS Month
, SUM(tbl_SalesMaster.grandTotal) AS TotalGrandTotal
FROM tbl_SalesMaster
INNER JOIN tbl_AccountLedger ON tbl_SalesMaster.ledgerId =tbl_AccountLedger.ledgerId
GROUP BY tbl_AccountLedger.ledgerName, DATEFROMPARTS(YEAR(tbl_SalesMaster.date),MONTH(tbl_SalesMaster.date),1)

MS Access Query to get multiple counts from the same field

I'm querying imported data that has a date/time field that I can't format as date in the table.
Sample:
Ticket Name Date
INC000101 User1 9/5/2016 10:00:34AM
INC000102 User2 9/5/2016 12:02:00PM
INC000103 User1 9/7/2016 3:34:00PM
INC000104 User2 10/1/2016 9:30:23AM
INC000105 User1 10/5/2016 10:20:00AM
INC000106 USer2 10/6/2016 4:56:00PM
I'm trying to get a count of how many tickets each user has per month. Because the Date field comes from the database as a text field, I can't seem to make that format as date/time so I use "left" to filter by month. This is what I've used to get a return on a single User item for the month of October.
SELECT COUNT(*)
FROM 2016YTD
WHERE [Name]='User1' AND left(Date,3) = '10/';
I would like to add counts for User2 through UserX per month so that I can get a count row or column for each the quantity of tickets for each user each month in one report. Everything I've tried won't save the query due to syntax errors in one form or another. I've tried variations of the following query help post as well without success.
SELECT a.distributor_id,
(SELECT COUNT(*) FROM myTable WHERE level='personal' and distributor_id = a.distributor_id) as PersonalCount,
(SELECT COUNT(*) FROM myTable WHERE level='exec' and distributor_id = a.distributor_id) as ExecCount,
(SELECT COUNT(*) FROM myTable WHERE distributor_id = a.distributor_id) as TotalCount
FROM myTable a ;
I'm sure the answer is staring at me, just not sure what at the moment.
Thanks for reading
John
This is only an answer to your first question about how to deal with dates that are stored in text fields.
To get the count for all users for every month you can do:
SELECT [Name], Format([Date],'mmmm') AS Month, COUNT(*) as Count
FROM 2016YTD
GROUP BY [Name], Format([Date],'mmmm')
A text field containing a date that is always in the same format can be treated as a date with Format() so Format([Date],'mmmm') returns the full month name for each date.
you should just need conditional aggregation. I haven't looked at access in a while but probably just something like this:
SELECT
a.distributor_id
,Format([Date],'mmmm') as Month
,SUM(IIF(level='personal',1,0)) as PersonalCount
,SUM(IIF(level='exec',1,0)) as ExecCount
,COUNT(*) as TotalCount
FROM
myTable a
GROUP BY
a.distributor_id
,Format([Date],'mmmm')

SQL Query to Get Count of Items within a timeframe

I need help with an SQL query that will get me a count of hourly deposits to an account for a specific date. The database consists of deposits and time of deposit to various accounts. I need a query that will return the number deposits made to a certain account on an hourly basis. Ideally the result would look something like this:
MM/DD/YYYY Account XYZ :
12:00PM : 3
1:00PM : 5
2:00PM: 7
3:00PM; 11
*Formatting doesnt really matter, just as long as I can get this info.
Try:
DECLARE #CheckDate DATE
SET #CheckDate='2014/5/13'
SELECT DATEPART(hh, DespositDate), COUNT(1)
FROM table t
WHERE account = 'XYZ' AND CAST(DepositDate AS DATE)=#CheckDate
GROUP BY DATEPART(hh, DespositDate)
You can use the date/time functions for aggregation. In your case, this would look like:
select cast(DepositDate as date), datepart(hour, DespositDate), count(*)
from table t
where account = 'XYZ'
group by cast(DepositDate as date), datepart(hour, DespositDate);

Using a timestamp function in a GROUP BY

I'm working with a large transaction data set and would like to group a count of individual customer transactions by month. I am unable to use the timestamp function in the GROUP BY and return the following error:
BAD_QUERY (expression STRFTIME_UTC_USEC([DATESTART], '%b') in GROUP BY is invalid)
Is there a simple workaround to achieve this or should I build a calendar table (which may be the simplest option)?
You have to use an alias:
SELECT STRFTIME_UTC_USEC(DATESTART, '%b') as month, COUNT(TRANSACTION)
FROM datasetId.tableId
GROUP BY month
#Charles is correct but as an aside you can also group by column number.
SELECT STRFTIME_UTC_USEC(DATESTART, '%b') as month, COUNT(TRANSACTION) as count
FROM [datasetId.tableId]
GROUP BY 1
ORDER BY 2 DESC

Date problem in MYSQL Query

Am looking for a query to sum values in a particular time duration say an year or a particular month in an year using MySQL syntax. Note that my transaction_date column stores daily amount transacted.
Am example of a query that returns total sales in an year query would look something like this
SELECT SUM(transaction_amount) WHERE transaction_date = (YEAR)
Am example of a query that returns total sales in an particular month and year would look something like this
SELECT SUM(transaction_amount) WHERE transaction_date = (YEAR)(MONTH)
How achievable is this?
SELECT SUM(transaction_amount)
WHERE YEAR(transaction_date) = '2008'
GROUP BY YEAR(transaction_date)
The second line may not be needed, depending on what you want exactly.