Mysql split row sum by count - mysql

I have the following tables
http://sqlfiddle.com/#!2/d0a3d
sp | product | exp
1 A | 50
1 B 50
1 A 100
1 B 100
2 B 200
2 C 200
3 A 50
3 B 50
Technical I want to divide exp to total number of products associated with t_id
The final result should be
sp | A | B | C
1 | 150 | 150 | 0
2 | 0 | 200 | 200
3 | 50 | 50 | 0

Can be done like this (done to match your sql fiddle columns):-
SELECT b.sp_id,
SUM(IF(a.product = 'A', b.exp, 0)) AS A,
SUM(IF(a.product = 'B', b.exp, 0)) AS B,
SUM(IF(a.product = 'C', b.exp, 0)) AS C
FROM topic_product a
INNER JOIN exp_speaker_topic b
ON a.t_id = b.t_id
GROUP BY b.sp_id
But a mess waiting to happen when extra values get added.
EDIT - amended to give what I think you are saying you want.
SELECT sp_id,
SUM(IF(product = 'A', avg_exp, 0)) AS A,
SUM(IF(product = 'B', avg_exp, 0)) AS B,
SUM(IF(product = 'C', avg_exp, 0)) AS C
FROM
(
SELECT sp_id, a.product, exp / Sub1.product_count AS avg_exp
FROM topic_product a
INNER JOIN exp_speaker_topic b
ON a.t_id = b.t_id
INNER JOIN
(
SELECT t_id, COUNT(*) AS product_count
FROM topic_product
GROUP BY t_id
) Sub1
ON a.t_id = Sub1.t_id
) Sub2
GROUP BY sp_id
SQL fiddle:-
http://sqlfiddle.com/#!2/d0a3d/33

OK, slow day. Just do the following, and handle missing results and display logic in the presentation layer/application-level code (e.g. a simple php loop acting upon an ordered array)...
SELECT p.product
, s.sp_id
, SUM(s.exp/x.cnt) total
FROM topic_product p
JOIN exp_speaker_topic s
ON s.t_id = p.t_id
JOIN
( SELECT t_id
, COUNT(0) cnt
FROM topic_product
GROUP
BY t_id
) x
ON x.t_id = p.t_id
GROUP
BY sp_id,product;

Here is the SQL Fiddle demonstrating the below queries.
You could also use CASE statements like the following:
SELECT e.sp_id,
SUM(CASE WHEN t.product = 'A' THEN e.exp ELSE 0 END) AS A,
SUM(CASE WHEN t.product = 'B' THEN e.exp ELSE 0 END) AS B,
SUM(CASE WHEN t.product = 'C' THEN e.exp ELSE 0 END) AS C
FROM topic_product t INNER JOIN exp_speaker_topic e ON t.t_id = e.t_id
GROUP BY e.sp_id;
If you want to divide by the number of record use the following:
SELECT e.sp_id,
SUM(CASE WHEN t.product = 'A' THEN e.exp ELSE 0 END) /
SUM(CASE WHEN t.product = 'A' THEN 1 ELSE 0 END) AS A,
SUM(CASE WHEN t.product = 'B' THEN e.exp ELSE 0 END) /
SUM(CASE WHEN t.product = 'B' THEN 1 ELSE 0 END) AS B,
SUM(CASE WHEN t.product = 'C' THEN e.exp ELSE 0 END) /
SUM(CASE WHEN t.product = 'C' THEN 1 ELSE 0 END) AS C
FROM topic_product t INNER JOIN exp_speaker_topic e ON t.t_id = e.t_id
GROUP BY e.sp_id;
If you want to get rid of the Nulls you could use the following:
SELECT m.sp_id,
CASE WHEN ISNULL(m.A) = 0 THEN m.A ELSE 0 END AS A,
CASE WHEN ISNULL(m.B) = 0 THEN m.B ELSE 0 END AS B,
CASE WHEN ISNULL(m.C) = 0 THEN m.C ELSE 0 END AS C
FROM
(
SELECT e.sp_id,
SUM(CASE WHEN t.product = 'A' THEN e.exp ELSE 0 END) / SUM(CASE WHEN t.product = 'A' THEN 1 ELSE 0 END) AS A,
SUM(CASE WHEN t.product = 'B' THEN e.exp ELSE 0 END) / SUM(CASE WHEN t.product = 'B' THEN 1 ELSE 0 END) AS B,
SUM(CASE WHEN t.product = 'C' THEN e.exp ELSE 0 END) / SUM(CASE WHEN t.product = 'C' THEN 1 ELSE 0 END) AS C
FROM topic_product t INNER JOIN exp_speaker_topic e ON t.t_id = e.t_id
GROUP BY e.sp_id
) AS m;

Related

make 2-dimension coordinate using mysql select results

I have question about mysql queries.
I have a table which have data below.
From To Weight
--------------
A B 1
A C 3
B C 2
D E 4
And I want to get sql result like below..
(?) A B C D E
----------------------
A 0 1 3 0 0
B 0 0 2 0 0
C 0 0 0 0 0
D 0 0 0 0 4
E 0 0 0 0 0
And what data is in original table is not determined.
How can I acheive this?
If you know the original columns, you can do:
select c.col1,
sum(case when to = 'A' then weight else 0 end) as a,
sum(case when to = 'B' then weight else 0 end) as b,
sum(case when to = 'C' then weight else 0 end) as c,
sum(case when to = 'D' then weight else 0 end) as d,
sum(case when to = 'E' then weight else 0 end) as d
from (select 'A' as col1 union all select 'B' union all select 'C' union all select 'D' union all select 'E'
) c left join
t
on t.from = c.col1
group by c.col1;
If you don't know the original columns, you could combine the values into a single string:
select col1.col,
group_concat(col2.col, ':', t.weight order by col2.col)
from ((select `from` as col from t
) union -- on purpose to remove duplicates
(select `to` from t
)
) col1 cross join
((select `from` as col from t
) union -- on purpose to remove duplicates
(select `to` from t
)
) col2 left join
t
on col1.col = t.`from` and col2.col = t.`from`
group by col1.col;
If you actually want separate columns and don't know the values, then you would need dynamic SQL.

SQL JOIN, GROUP BY on Four tables to get Record By Month

I have the following DB design. Tables are:
auth_user
----------
first_name
last_name
staffuser
----------
phone_number
user_id
billing_customerservicebill
-----------------------------
bill_id
service_provider_id
discounted_price
billing_billmanagement
------------------------
creation_date
My query return Sum of discounted_price each user by month row wise. I need every month record show in column.
The following query gives me This record
select a.service_provider_id, first_name, Sum(a.discounted_price), EXTRACT(MONTH FROM c.creation_date)
from billing_customerservicebill a
left outer join users_staffuser b
on a.service_provider_id = b.id
left outer join billing_billmanagement c
on a.bill_id = c.id
left outer join auth_user d
on d.id = b.user_id
where c.creation_date between '2017-11-01' AND '2017-12-31'
group by service_provider_id, first_name, EXTRACT(MONTH FROM c.creation_date)
order by 1
My data show in Table Currently
service_provider_id | first_name | Sum | Month
5 | suneel 31500 | 11
5 | Suneel | 900 | 12
Expected data is
service_provider_id | first_name | Nov | December
5 | suneel | 31500 | 900
The most flexible approach is to use conditional aggregation...
select
a.service_provider_id,
first_name,
SUM(CASE WHEN c.creation_date >= '2017-11-01' AND c.creation_date < '2017-12-01' THEN a.discounted_price END) AS nov,
SUM(CASE WHEN c.creation_date >= '2017-12-01' AND c.creation_date < '2018-01-01' THEN a.discounted_price END) AS dec
from billing_customerservicebill a
left outer join users_staffuser b
on a.service_provider_id = b.id
left outer join billing_billmanagement c
on a.bill_id = c.id
left outer join auth_user d
on d.id = b.user_id
where c.creation_date between '2017-11-01' AND '2017-12-31'
group by service_provider_id, first_name
order by 1
This shows that you need to know in advance which columns you're going to calculate.
Please, try with below solution it's near to your answer:
Where month as column and group by users:
select B.service_provider_id, B.first_name,
(case when month=1 then discounted_price else 0 end) as JAN,
(case when month=2 then discounted_price else 0 end) as FEB,
(case when month=3 then discounted_price else 0 end) as MAR,
(case when month=4 then discounted_price else 0 end) as APR,
(case when month=5 then discounted_price else 0 end) as MAY,
(case when month=6 then discounted_price else 0 end) as JUN,
(case when month=7 then discounted_price else 0 end) as JULY,
(case when month=8 then discounted_price else 0 end) as AUG,
(case when month=9 then discounted_price else 0 end) as SEP,
(case when month=10 then discounted_price else 0 end) as OCT,
(case when month=11 then discounted_price else 0 end) as NOV,
(case when month=12 then discounted_price else 0 end) as DEC
from(
select a.service_provider_id, first_name, Sum(a.discounted_price) as discounted_price, EXTRACT(MONTH FROM c.creation_date) as month
from billing_customerservicebill a
left outer join users_staffuser b
on a.service_provider_id = b.id
left outer join billing_billmanagement c
on a.bill_id = c.id
left outer join auth_user d
on d.id = b.user_id
where c.creation_date between '2017-11-01' AND '2017-12-31'
group by service_provider_id, first_name, EXTRACT(MONTH FROM c.creation_date)
) as B
group by B.service_provider_id, B.first_name

Slow MySQL queries using SUM()

I have to run two queries in my code to get my tenants balance. However, these queries are too slow.
First query, I get all the tenants and it's unit name:
SELECT t.TenantID
FROM Tenants t
JOIN Units u
ON t.UnitID = u.UnitID
Where t.Prospect = 2
AND t.PropertyID = 8
ORDER
BY CONCAT(Left(Replace(UnitName,'-',''),2),
REPEAT('0', (10-CHAR_LENGTH(UnitName))),
Right(Replace(UnitName,'-',''),
CHAR_LENGTH(Replace(UnitName,'-',''))-2
) )
It returns 500 rows
Then I get the balances in 4 conditions. This query will be inside of first query loop:
Select
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalDebit,
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingDebit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalCredit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingCredit
From TenantTransactions
Where TenantID= FirstQuery.TenantID
Am I doing the queries wrong? It's taking like 1 minute to run.
Do this in a single query with GROUP BY.
Try something like this:
SELECT t.TenantID, TotalDebit, HousingDebit, TotalCredit, HousingCredit
FROM Tenants t
JOIN Units u ON t.UnitID = u.UnitID
LEFT JOIN (
Select
TenantID,
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalDebit,
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingDebit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalCredit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingCredit
From TenantTransactions
Group By TenantID
) sums ON sums.TenantID = t.TenantID
Where t.Prospect = 2
AND t.PropertyID = 8
ORDER
BY CONCAT(Left(Replace(UnitName,'-',''),2),REPEAT('0', (10-CHAR_LENGTH(UnitName))),Right(Replace(UnitName,'-',''),CHAR_LENGTH(Replace(UnitName,'-',''))-2))
The inner query may still run for a while but it will only run once.
Try a compound covering index on TenantTransactions containing these columns: (TenantID, TransactionTypeID, ChargeTypeID, TransactionAmount) to optimize the query with the SUMs in it.
Try a compound index on Tenants with the columns (PropertyID, Prospect) in it.
Here's another way to do it with a subquery. You know, the performance problem might not be database performance, but the back and forth between your database and application server. So that is where a single query will help.
SELECT t.TenantID,
(SELECT SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS TotalDebit,
(SELECT SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS HousingDebit,
(SELECT SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS TotalCredit,
(SELECT SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS HousingCredit
FROM Tenants t
JOIN Units u
ON t.UnitID = u.UnitID
Where t.Prospect = 2
AND t.PropertyID = 8
ORDER
BY CONCAT(Left(Replace(UnitName,'-',''),2),REPEAT('0', (10-CHAR_LENGTH(UnitName))),Right(Replace(UnitName,'-',''),CHAR_LENGTH(Replace(UnitName,'-',''))-2))

[HY000][1111] Invalid use of group function

I have searched a lot ,but none of other questions with error 1111 solves my problem.
My needs are to count the distinct phone number of some id
The following code works:
SELECT
a.id_borrow_application,
count(DISTINCT c.phone_no) CVG_CALL_OUT_COUNTS_6M
FROM t_snow_borrow_application_id a
JOIN t_snow_call_mobile b
JOIN t_snow_call_record_201612 c ON
(
a.id_borrow_application = b.id_borrow_application
AND b.id = c.id_call_mobile
)
WHERE c.call_type = 0
GROUP BY a.id_borrow_application;
But when I want to write 4 similar queries together,the error in title
happens.
[HY000][1111] Invalid use of group function
SELECT
a.id_borrow_application,
sum(CASE WHEN call_type = 0
THEN count(DISTINCT c.phone_no)
ELSE 0 END) CVG_CALL_OUT_COUNTS_6M,
sum(CASE WHEN call_type = 0 AND c.days <= 30
THEN count(DISTINCT c.phone_no)
ELSE 0 END) CVG_CALL_OUT_COUNTS_1M,
sum(CASE WHEN call_type = 1
THEN count(DISTINCT c.phone_no)
ELSE 0 END) CVG_CALL_IN_COUNTS_6M,
sum(CASE WHEN call_type = 1 AND c.days <= 30
THEN count(DISTINCT c.phone_no)
ELSE 0 END) CVG_CALL_IN_COUNTS_1M
FROM t_snow_borrow_application_id a
JOIN t_snow_call_mobile b
JOIN t_snow_call_record_201612 c ON
(
a.id_borrow_application = b.id_borrow_application
AND b.id = c.id_call_mobile
)
GROUP BY a.id_borrow_application;
Do I have to write 4 queries?
You are nesting aggregate function which is not allowed in MySQL.
You don't actually need the sum function for count distinct phone_nos for different conditions. Take the count (distinct outside the case and remove sum function and else clause of the case.
Try this:
select a.id_borrow_application,
count(distinct case when call_type = 0 then c.phone_no end) CVG_CALL_OUT_COUNTS_6M,
count(distinct case when call_type = 0
and c.days <= 30 then c.phone_no end) CVG_CALL_OUT_COUNTS_1M,
count(distinct case when call_type = 1 then c.phone_no end) CVG_CALL_IN_COUNTS_6M,
count(distinct case when call_type = 1
and c.days <= 30 then c.phone_no end) CVG_CALL_IN_COUNTS_1M
from t_snow_borrow_application_id a
join t_snow_call_mobile b
join t_snow_call_record_201612 c on (
a.id_borrow_application = b.id_borrow_application
and b.id = c.id_call_mobile
)
group by a.id_borrow_application;

Flattening two tables and using a group by

I have two transaction tables like this:
TableA
X R S
71 1 10
71 2 20
71 3 30
72 1 40
72 2 50
72 3 60
TableB
X P Q
71 1 110
71 2 120
71 3 130
73 1 140
73 2 150
73 3 160
I want to flatten the table and execute a query which gives me a result like this:
X S1 S2 S3 Q1 Q2 Q3
71 10 20 30 110 120 130
72 40 50 60 0 0 0
73 0 0 0 140 150 160
where the subscripts in S and Q denote the value of the third column when the second column equals the subscript. For example, S1 denotes the S column value when second column is equal to 1, or Q2 denotes the value of Q when the second column is 2.
I can't figure out what is the best way to about it. Maybe I can use a case statement or may be a subquery. But honestly I have no concrete idea of going about it.
You would want to do this with a join and two aggregations:
select coalesce(a.x, b.x) as x, a.s1, a.s2, a.s3, b.q1, b.q2, b.q3
from (select x,
max(case when r = 1 then s end) as s1,
max(case when r = 2 then s end) as s2,
max(case when r = 3 then s end) as s3
from tableA a
group by x
) a full outer join
(select x,
max(case when p = 1 then q end) as q1,
max(case when p = 2 then q end) as q2,
max(case when p = 3 then q end) as q3
from tableb b
group by x
) b
on a.x = b.x;
EDIT:
If you want to avoid the nested subqueries, you could try:
select coalesce(a.x, b.x) as x,
max(case when r = 1 then s end) as s1,
max(case when r = 2 then s end) as s2,
max(case when r = 3 then s end) as s3
max(case when p = 1 then q end) as q1,
max(case when p = 2 then q end) as q2,
max(case when p = 3 then q end) as q3
from TableA a full outer join
TableB b
on a.x = b.x and a.r = b.p
group by coalesce(a.x, b.x);
It might even be more efficient.
SELECT
ISNULL(A.x, B.x) AS x, SUM(CASE WHEN R = 1 THEN S ELSE 0 END) AS S1,
SUM(CASE WHEN R = 2 THEN S ELSE 0 END) AS S2,
SUM(CASE WHEN R = 3 THEN S ELSE 0 END) AS S3,
SUM(CASE WHEN P = 1 THEN Q ELSE 0 END) AS Q1,
SUM(CASE WHEN P = 2 THEN Q ELSE 0 END) AS Q2,
SUM(CASE WHEN P = 3 THEN Q ELSE 0 END) AS Q3
FROM tableA A FULL JOIN tableB B ON A.X = B.X
GROUP BY ISNULL(A.x, B.x)
T-SQL version