MySql: sum value based on state field on millions rows table - mysql

I have a table with millions of rows (SF_COLLECTIONS)
ID MEMBERID COLLECTIONID CARDID STATE (D / M) HOWMANY
1 1 1 1 D 1
2 1 1 2 D 2
3 2 1 1 M 1
4 2 1 2 M 1
5 2 2 3 D 1
6 1 2 3 M 2
and I want to know for every COLLECTIONID the SUM of HOWMANY field for STATE=D and STATE=M
So I try this approach
select COLLECTIONID,
sum(if(STATE='D',HOWMANY,0)) as HMD,
sum(if(STATE='M',HOWMANY,0)) as HMM
from SF_COLLECTIONS
group by COLLECTIONID
and it takes about 15 seconds to answer
Any suggestions to get better performances?
Thanks in advance

Please try this:
SELECT COLLECTIONID,
SUM(CASE WHEN STATE='D' THEN HOWMANY END) AS HMD,
SUM(CASE WHEN STATE='M' THEN HOWMANY END) AS HMM
FROM SF_COLLECTIONS
GROUP BY COLLECTIONID;
Duration / Fetch Time
0.00068 sec / 0.000012 sec
Your existing_query:
SELECT COLLECTIONID,
SUM(IF(STATE='D',HOWMANY,0)) AS HMD,
SUM(IF(STATE='M',HOWMANY,0)) AS HMM
FROM SF_COLLECTIONS
GROUP BY COLLECTIONID;
Duration / Fetch Time
0.00072 sec / 0.000012 sec
Only 6 rows there is 0.00004 difference :
For 10 million rows, I think It is noticeable:)

Related

How to use count in sql based on a IF condition

From this table
groupId
flag
flagValue
1
0
500
2
0
100
1
1
10
2
1
50
3
0
100
1
1
200
3
1
1000
2
1
50
I need this result
groupId
flag1
flag0
valFlag1
valFlag0
totalFlags
1
2
1
210
500
3
2
2
1
100
100
3
3
1
1
1000
100
2
where
flag1 is number of times flag is 1 for a particular group
flag0 is number of times flag is 0 for a particular group
valFlag1 is sum of flagVal when flag is 1
valFlag0 is sum of flagVal when flag is 0
totalFlags is sum of total flags associated with a group
I am stuck as to how to actually count values based on an IF condition.
Anyhelp is appreciated. Thanks.
I have used a table named group_table with your values
Try using this:
SELECT
g.`groupId`,
SUM(g.`flag`=1 ) AS flag1,
SUM(g.`flag`=0) AS flag0,
SUM(CASE WHEN g.`flag`=1 THEN g.`flagValue` ELSE 0 END) AS valFalg1,
SUM(CASE WHEN g.`flag`=0 THEN g.`flagValue` ELSE 0 END) AS valFalg0,
COUNT(*) AS totalFlags
FROM
`group_table` g
GROUP BY g.`groupId`
If you have to use the IF,
SELECT
g.`groupId`,
IF(g.`flag`=1,1,0 ) AS flag1,
IF(g.`flag`=0,1,0) AS flag0,
SUM(IF(g.`flag`=1,g.`flagValue`,0 )) AS valFalg1,
SUM(IF(g.`flag`=0,g.`flagValue`,0 )) AS valFalg0,
COUNT(*) AS totalFlags
FROM
`group_table` g
GROUP BY g.`groupId`, flag1, flag0
They'll produce the same result

MySQL Group by consecutive values and count

I have a table that looks like this
id
1
2
4
5
6
10
11
So a bunch of consecutive values, an unknown number of absent fields and then other consecutive values.
What I am trying to achieve is to get
id
stint
1
0
2
0
4
1
5
1
6
1
10
2
11
2
By incrementing every time the number of the stint, which I can later use for summing over other columns.
Is it possible? Thanks
If your MySQL version support window function.
You can try to use LAG window function in subquery to get previous id column, then use SUM condition aggregate window function.
Query #1
SELECT Id,
SUM(id - n_Id > 1) OVER(ORDER BY id) stint
FROM (
SELECT *,LAG(id,1,id) OVER(ORDER BY id) n_Id
FROM T
) t1
Id
stint
1
0
2
0
4
1
5
1
6
1
10
2
11
2
View on DB Fiddle

mysql query to find rows having only single specific sub-category against any category (in group by data)

I have MySQL table with name users. SQL returns following result
select user_id, vegetarian, count(*) from users group by user_id, vegetarian;
Current SQL Output
user_id vegetarian count(*)
2 1 15
3 0 131
3 1 6
4 1 6
5 0 113
5 1 7
6 1 6
7 0 107
7 1 11
Required Output
My required output is:
user_id vegetarian count(*)
2 1 15
4 1 6
6 1 6
Kindly help me for SQL query to get my required output.
Kind Regards
SELECT user_id, 1 vegetarian, COUNT(*)
FROM users
GROUP BY user_id
HAVING NOT SUM(NOT vegetarian);
NOT vegetarian converts 0 to 1 and backward. Hence SUM() calculates the amount of rows which are not vegetarians. And HAVING NOT SUM() removes the rows where this sum is above zero.

mysql compare 2 rows same table

I am trying to compare 2 rows and display the same ones.I did browse but was not able to find the right solution.
Table A
Count status Division
20 A 1
30 B 2
10 c 1
12 z 1
From the above table I want to display whose division is same.
Count status Division
20 A 1
10 c 1
12 z 1
Try this
Select * from TableA
Group By Division
Having Count(*) > 1
Select * from TableA
Group By Division
having Count(*) = 1
Here i used case statement , it worked for me
select CompanyCode ,'Commission Pec', Year
,sum(case when CommissionType='Commission Recevied' then JAN else 0 end)/sum(case when CommissionType='Net Payments from WM' and isnull(JAN,0)<>0 then JAN else 1 end)
from Commission_Consolidate
group by CompanyCode,Year
end

MYSQL select query using count (*)

i have a problem concerning a select query in MYSQL
i have two different tables and i want to obtain a certain result
i used COUNT method which gave me only the results (>=1)
But in reality , i want to use all counts with zero included how to do it?
My query is:
SELECT
first.subscriber_id,
second.tag_id,
COUNT(*)
FROM
content_hits first
JOIN content_tag second ON first.content_id=second.content_id
GROUP BY
second.Tag_id,first.Subscriber_id<br>
First table:Content_hits
CONTENT_ID SUBSCRIBER_ID
30 1
10 10
34 4
32 2
40 3
28 3
30 6
31 8
12 3
Second table:Content_tag
CONTENT_ID TAG_ID
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
11 2
12 2
13 2
14 2
Result but incomplete For example:Subsrciber6 for tag_id=1 should have a count(*)=0
subscriber_id tag_id COUNT(*)
1 1 4
2 1 7
3 1 2
4 1 1
5 1 3
7 1 2
8 1 1
9 1 1
10 1 3
1 2 2
2 2 3
3 2 2
Now that you have further elaborated on what you actually want to achieve, it can be seen that the problem is much more complex. You actually want all combinations of subscriber_id and tag_id, and then count the number of actual entries in the joined table product. whew. So here goes the SQL:
SELECT combinations.tag_id,
combinations.subscriber_id,
-- correlated subquery to count the actual hits by tag/subscriber when joining
-- the two tables using content_id
(SELECT count(*)
FROM content_hits AS h
JOIN content_tag AS t ON h.content_id = t.content_id
WHERE h.subscriber_id = combinations.subscriber_id
AND t.tag_id = combinations.tag_id) as cnt
-- Create all combinations of tag/subscribers first, before counting anything
-- This will be necessary to have "zero-counts" for any combination of
-- tag/subscriber
FROM (
SELECT DISTINCT tag_id, subscriber_id
FROM content_tag
CROSS JOIN content_hits
) AS combinations
Not sure, but is this what you want?
SELECT first.subscriber_id, second.tag_id, COUNT(*) AS c
FROM content_hits first JOIN content_tag second ON first.content_id=second.content_id
GROUP BY second.Tag_id,first.Subscriber_id HAVING c = 0