How to group by two fields? - mysql

I have the following table:
listId | accountId | amount
1 1 20
1 1 20
2 2 30
2 2 30
I need to SUM(amount) and group by listId, accountId to get result:
listId | accountId | amount |
1 1 40
2 2 60
But it does not work for me: SUM(amount) ... GROUP BY listId, accountId
My full query is:
select `account_transactions`.*,
`enterprise_invoces`.*,
ABS(SUM(IF(AT_amount>0, AT_amount, 0))) AS debit,
ABS(SUM(IF(AT_amount<0, AT_amount, 0))) AS credit
from `account_transactions`
inner join `enterprise_invoces`
on `enterprise_invoces`.`AC_id` = `account_transactions`.`AT_code`
where `AT_createuser` = 15 and
date(`AT_transactiondatetime`) >= 2019-04-11 and
date(`AT_transactiondatetime`) <= 2019-07-29 and
`AC_code` >= 601 and
`AC_code` <= 761
group by `enterprise_invoces`.`AC_id`, `account_transactions.AT_transactionficheno`
order by `AT_transactiondatetime` desc

Your select query should not have other columns and should have only the columns mentioned in group by and also the column which needs to be aggregated. So the query should be like this below.
select enterprise_invoces.AC_id, account_transactions.AT_transactionficheno ,
ABS(SUM(IF(AT_amount>0, AT_amount, 0))) AS debit,
ABS(SUM(IF(AT_amount<0, AT_amount, 0))) AS credit
from account_transactions
inner join enterprise_invoces
on enterprise_invoces.AC_id = account_transactions.AT_code
where AT_createuser = 15 and
date(AT_transactiondatetime) >= 2019-04-11 and
date(AT_transactiondatetime) <= 2019-07-29 and
AC_code >= 601 and
AC_code <= 761
group by enterprise_invoces.AC_id, account_transactions.AT_transactionficheno
order by AT_transactiondatetime desc

Related

How to get highest count from list data in mysql?

i have a query like this :
select DATE_FORMAT(o.created_at, '%Y-%m') as date,
JSON_EXTRACT(oi.inventory, '$.id') as inv_id,
count(JSON_EXTRACT(oi.inventory, '$.id')) as inv_count
from orders as o inner join order_items as oi on oi.order_id = o.id
where o.created_at >= (CURDATE() + INTERVAL (1 - DAY(CURDATE())) DAY) - INTERVAL 12 MONTH AND
o.created_at < (CURDATE() + INTERVAL (1 - DAY(CURDATE())) DAY) + INTERVAL 1 MONTH
group by date, inv_id
order by date desc limit 10
and the result is :
date
inv_id
inv_count
2023-01
1
22
2023-01
2
29
2022-12
1
1
2022-12
2
2
2022-11
1
2
2022-11
2
1
2022-10
1
2
2022-10
2
1
but all i want is result like this :
date
inv_id
inv_count
2023-01
2
29
2022-12
2
2
2022-11
1
2
2022-10
1
2
how to grouping data like that? because i can't use max(inv_count) inside group by

Count records which fulfil given condition

I have a table with the following schema:
+-------------------------------------------------------+
| table_counter |
+----+---------------------+------------+---------------+
| id | timestamp | entry_type | country |
+----+---------------------+------------+---------------+
+----+---------------------+------------+---------------+
| 10 | 2017-05-01 12:00:00 | click | Germany |
+----+---------------------+------------+---------------+
| 11 | 2017-05-01 12:00:00 | view | Austria |
+----+---------------------+------------+---------------+
| 12 | 2017-05-01 12:00:00 | click | UK |
+----+---------------------+------------+---------------+
| 13 | 2017-05-01 12:00:00 | view | USA |
+----+---------------------+------------+---------------+
I need to return the following result: Select the sum of views and clicks of the top 5 countries by sum of views in the past 30 days.
I know how to count the records all right, but how do I define the constrains? How do I return all entries from five countries with the highest number of views?
Limiting the result to the last 30 days is trivial, but I'm pretty much stuck at the beginning.
Using order by and limit keywords,
SELECT SUM(IF(entry_type = "view", 1, 0)) as view_count FROM t3 GROUP BY country, entry_type ORDER BY view_count DESC LIMIT 5
--EDIT
As per the requirement stated in the comments, here's the updated query:
SELECT SUM(view_click_count) as all_total FROM (SELECT country, SUM(IF(entry_type = "view", 1, 0)) as view_count, SUM(IF(entry_type = "click", 1, 0)) as click_count, count(entry_type) as view_click_count FROM t3 GROUP BY country ORDER BY view_count DESC LIMIT 5) t2
all_total gives the total count as needed, for top 5 countries.
You can do it this way:
select
tc.country,
count(case entry_type when 'click' then 1 else null end) clicks,
count(case entry_type when 'view' then 1 else null end) views
from table_counter tc
inner join (
select top 5 country from [dbo].[table_counter]
where entry_type = 'view'
and timestamp >= DATEADD(DAY, -30, GETDATE())
group by country
order by count(entry_type) desc
) t on t.country = tc.country
where timestamp >= DATEADD(DAY, -30, GETDATE())
group by tc.country
order by views desc
This is for SQL Server. A few tweaks might be needed for MySQL (i.e. 'Limit' instead of 'TOP')
You can get top 5 countries by views with the following query, e.g.:
SELECT country, count(*) as 'views'
FROM table
WHERE timestamp BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW()
AND entry_type = 'view'
GROUP BY country
ORDER BY count(*) DESC
LIMIT 5
Now, to select clicks, you can add another query in SELECT , e.g.:
SELECT t.country, COUNT(*) as 'views',
(SELECT COUNT(*)
FROM `table`
WHERE country = t.country
AND entry_type = 'click'
AND timestamp BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW()
) as 'clicks'
FROM `table` t
WHERE t.timestamp BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW()
AND t.entry_type = 'view'
GROUP BY t.country
ORDER BY count(*) DESC
LIMIT 5
Here's the SQL Fiddle.
Update
To get the SUM of views and clicks, wrap the above query into another SELECT, e.g.:
SELECT country, views + clicks
FROM(
SELECT t.country, COUNT(*) as 'views',
(SELECT COUNT(*)
FROM `table`
WHERE country = t.country
AND entry_type = 'click'
AND timestamp BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW()
) as 'clicks'
FROM `table` t
WHERE t.timestamp BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW()
AND t.entry_type = 'view'
GROUP BY t.country
ORDER BY count(*) DESC
LIMIT 5
) b;
Here's the updated SQL Fiddle.

Group by a summed variable

SELECT deposit.numberSuccessfulDeposits, count(distinct userid)
FROM deposit WHERE deposit.asOfDate between '2016-04-01 00:00:00' and '2016-04-03 23:59:59'
AND deposit.licenseeId = 1306
GROUP BY deposit.numberSuccessfulDeposits
Sample output
numberSuccessfulDeposits count(distinct userid)
0 228
1 878
2 90
3 37
4 17
However, if Bob made 1 deposit on Monday and 3 deposits on Tuesday, then it will count towards both "1" and "3" for number of successful deposits.
numberSuccessfulDeposits count(distinct userid)
0 ##
1 1
2 ##
3 1
4 ##
Ideally, it should only count towards "4"
numberSuccessfulDeposits count(distinct userid)
0 ##
1 ##
2 ##
3 ##
4 1
Thoughts?
Change the grouping to user-based and sum all occurences of deposits. Then count users for each sum of those deposits:
SELECT
numberSuccessfulDeposits,
COUNT(userid) AS users_count
FROM (
SELECT
sum(numberSuccessfulDeposits) AS numberSuccessfulDeposits,
userid
FROM deposit
WHERE asOfDate between '2016-04-01 00:00:00' and '2016-04-03 23:59:59'
AND licenseeId = 1306
GROUP BY userid
) t
GROUP BY numberSuccessfulDeposits
Edit: Grouping deposints into 0, 1, 2, 3+ category would look like that:
SELECT
numberSuccessfulDeposits,
COUNT(userid) AS user_count
FROM (
SELECT
CASE WHEN numberSuccessfulDeposits >= 3 THEN '3+' ELSE numberSuccessfulDeposits::TEXT END AS numberSuccessfulDeposits,
userid
FROM (
SELECT
sum(numberSuccessfulDeposits) AS numberSuccessfulDeposits,
userid
FROM deposit
WHERE asOfDate between '2016-04-01 00:00:00' and '2016-04-03 23:59:59'
AND licenseeId = 1306
GROUP BY userid
) t
) f
GROUP BY numberSuccessfulDeposits
Calculate the per-user sum in a subquery, then the per-total count in the main query.
SELECT totalDeposits, COUNT(*)
FROM (SELECT userid, SUM(numberOfSuccessfulDeposits) AS totalDeposits
FROM deposit
WHERE deposit.asOfDate between '2016-04-01 00:00:00' and '2016-04-03 23:59:59'
AND deposit.licenseeId = 1306
GROUP BY userid) AS subquery
GROUP BY totalDeposits

Add to query in order to get total sum of previous results SQL

I currently have a query that provides the result set below, I now need to add to this query to provide a total at the bottom of all the sales. I am not sure how to do this.
Current query:
SELECT
product,
COUNT(OrderNumber) AS CountOf
FROM
orders
WHERE
STATUS = 'booking' AND
Date(OrderDate) <= CURDATE() AND
Date(OrderDate) > DATE_SUB(CURDATE(),INTERVAL 30 DAY)
GROUP BY
product
ORDER BY CountOf DESC
Current Resultset:
product| count
-----------------------
pd1 | 3
pd4 | 1
pd2 | 1
desired result set =
product| count
-----------------------
pd1 | 3
pd4 | 1
pd2 | 1
Total | 5
Maybe you can add a UNION, and a SELECT with total amount. Something like this:
SELECT
product,
COUNT(OrderNumber) AS CountOf
FROM
orders
WHERE
STATUS = 'booking' AND
Date(OrderDate) <= CURDATE() AND
Date(OrderDate) > DATE_SUB(CURDATE(),INTERVAL 30 DAY)
GROUP BY
product
UNION
SELECT 'Total', count(OrderNumber) AS CountOf
FROM orders
WHERE
STATUS = 'booking' AND
Date(OrderDate) <= CURDATE() AND
Date(OrderDate) > DATE_SUB(CURDATE(),INTERVAL 30 DAY)
ORDER BY CountOf DESC;
Try using an Inner join on the same table, the union did not work due to there being the incorrect amount of columns on each side.
The Initial select had 2 set columns, where the second select (after the union) did not.

SQL Query to find rows that didn't occur this month

I am trying to find the number of sellers that made a sale last month but didn't make a sale this month.
I have a query that works but I don't think its efficient and I haven't figured out how to do this for all months.
SELECT count(distinct user_id) as users
FROM transactions
WHERE MONTH(date) = 12
AND YEAR(date) = 2015
AND transactions.status = 'COMPLETED'
AND transactions.amount > 0
AND transactions.user_id NOT IN
(
SELECT distinct user_id
FROM transactions
WHERE MONTH(date) = 1
AND YEAR(date) = 2016
AND transactions.status = 'COMPLETED'
AND transactions.amount > 0
)
The structure of the table is:
+---------+------------+-------------+--------+
| user_id | date | status | amount |
+---------+------------+-------------+--------+
| 1 | 2016-01-01 | 'COMPLETED' | 1.00 |
| 2 | 2015-12-01 | 'COMPLETED' | 1.00 |
| 3 | 2015-12-01 | 'COMPLETED' | 2.00 |
| 1 | 2015-12-01 | 'COMPLETED' | 3.00 |
+---------+------------+-------------+--------+
So in this case, users with ID 2 and 3, didn't make a sale this month.
Use conditional aggregation:
SELECT count(*) as users
FROM
(
SELECT user_id
FROM transactions
-- 1st of previous month
WHERE date BETWEEN SUBDATE(SUBDATE(CURRENT_DATE, DAYOFMONTH(CURRENT_DATE)-1), interval 1 month)
-- end of current month
AND LAST_DAY(CURRENT_DATE)
AND transactions.status = 'COMPLETED'
AND transactions.amount > 0
GROUP BY user_id
-- any row from previous month
HAVING MAX(CASE WHEN date < SUBDATE(CURRENT_DATE, DAYOFMONTH(CURRENT_DATE)-1)
THEN date
END) IS NOT NULL
-- no row in current month
AND MAX(CASE WHEN date >= SUBDATE(CURRENT_DATE, DAYOFMONTH(CURRENT_DATE)-1)
THEN date
END) IS NULL
) AS dt
SUBDATE(CURRENT_DATE, DAYOFMONTH(CURRENT_DATE)-1) = first day of current month
SUBDATE(first day of current month, interval 1 month) = first day of previous month
LAST_DAY(CURRENT_DATE) = end of current month
if you want to generify it, you can use curdate() to get current month, and DATE_SUB(curdate(), INTERVAL 1 MONTH) to get last month (you will need to do some if clause for January/December though):
SELECT count(distinct user_id) as users
FROM transactions
WHERE MONTH(date) = MONTH(DATE_SUB(curdate(), INTERVAL 1 MONTH))
AND transactions.status = 'COMPLETED'
AND transactions.amount > 0
AND transactions.user_id NOT IN
(
SELECT distinct user_id
FROM transactions
WHERE MONTH(date) = MONTH(curdate())
AND transactions.status = 'COMPLETED'
AND transactions.amount > 0
)
as far as efficiency goes, I don't see a problem with this one
The following should be pretty efficient. In order to make it even more so, you would need to provide the table definition and and the EXPLAIN.
SELECT COUNT(DISTINCT user_id) users
FROM transactions t
LEFT
JOIN transactions x
ON x.user_id = t.user_id
AND x.date BETWEEN '2016-01-01' AND '2016-01-31'
AND x.status = 'COMPLETED'
AND x.amount > 0
WHERE t.date BETWEEN '2015-12-01' AND '2015-12-31'
AND t.status = 'COMPLETED'
AND t.amount > 0
AND x.user_id IS NULL;
Just some input for thought:
You could create aggregated lists of user-IDs per month, representing all the unique buyers in that month. In your application, you would then simply have to subtract the two months in question in order to get all user-IDs that have only made a sale in one of the two months.
See below for query- and post-processing-examples.
In order to make your query efficient, I would recommend at least a 2-column index for table transactions on [status, amount]. However, in order to prevent the query from having to look up data in the actual table, you could even create a 4-column index [status, amount, date, user_id], which should further improve the performance of your query.
Postgres (v9.0+, tested)
SELECT (DATE_PART('year', t.date) || '-' || DATE_PART('month', t.date)) AS d,
STRING_AGG( DISTINCT t.user_id::TEXT, ',' ) AS buyers
FROM transactions t
WHERE t.status = 'COMPLETED'
AND t.amount > 0
GROUP BY DATE_PART('year', t.date),
DATE_PART('month', t.date)
ORDER BY DATE_PART('year', t.date),
DATE_PART('month', t.date)
;
MySQL (not tested)
SELECT (YEAR(t.date) || '-' || MONTH(t.date)) AS d,
GROUP_CONCAT( DISTINCT t.user_id ) AS buyers
FROM transactions t
WHERE t.status = 'COMPLETED'
AND t.amount > 0
GROUP BY YEAR(t.date), MONTH(t.date)
ORDER BY YEAR(t.date), MONTH(t.date)
;
Ruby (example for post-processing)
db_result = ActiveRecord::Base.connection_pool.with_connection { |con| con.execute( db_query ) }
unique_buyers = db_result.map{|e|[e['d'],e['buyers'].split(',')]}.to_h
buyers_dec15_but_not_jan16 = unique_buyers['2015-12'] - unique_buyers['2016-1']
buyers_nov15_but_not_dec16 = unique_buyers['2015-11']||[] - unique_buyers['2015-12']
...(and so on)...