Unlike SQL: Count each row value in a column and return count into multiple column
My rows are also different.
I want vote count result like this.
CONTENT_NO CONTENT_TEXT one two three four(ANSWER_SCORE)
1 How often? 0 1 0 1
2 How fast? 0 1 0 1
3 How long? 0 0 2 0
4 How much? 1 1 0 0
I have 2 tables
question_content
CONTENT_NO CONTENT_TEXT TEST_PAPER_NO
1 How often? 1
2 How fast? 1
3 How long? 1
4 How much? 1
question_content_answer
ANSWER_NO CONTENT_NO TEST_PAPER_NO ANSWER_SCORE PERSON_NO
1 1 1 4 1
2 2 1 4 1
3 3 1 3 1
4 4 1 2 1
5 1 1 2 2
6 2 1 2 2
7 3 1 3 2
8 4 1 1 2
I have tried
SELECT question_content.CONTENT_NO,
question_content.CONTENT_TEXT,
(SELECT COUNT(ANSWER_SCORE)
FROM (SELECT ANSWER_SCORE
FROM question_content_answer
WHERE TEST_PAPER_NO= '1'
) as qa
WHERE ANSWER_SCORE = '1'
AND CONTENT_NO = qa.CONTENT_NO
GROUP BY CONTENT_NO
) as one,
(SELECT COUNT(ANSWER_SCORE)
FROM (SELECT ANSWER_SCORE
FROM question_content_answer
WHERE TEST_PAPER_NO= '1'
) as qa
WHERE ANSWER_SCORE = '2'
AND CONTENT_NO = qa.CONTENT_NO
GROUP BY CONTENT_NO
) as two,
(SELECT COUNT(ANSWER_SCORE)
FROM (SELECT ANSWER_SCORE
FROM question_content_answer
WHERE TEST_PAPER_NO= '1'
) as qa
WHERE ANSWER_SCORE = '3'
AND CONTENT_NO = qa.CONTENT_NO
GROUP BY CONTENT_NO
) as three
FROM question_content_answer
INNER JOIN question_content
USING (CONTENT_NO)
WHERE question_content.TEST_PAPER_NO= '1'
but this counts all the questions in every row.
How to count each row?
Just like SQL: Count each row value in a column and return count into multiple column
This code works fine.
SELECT question_content.CONTENT_NO,
question_content.CONTENT_TEXT,
COUNT(CASE WHEN ANSWER_SCORE = '1' THEN 1 END) AS one,
COUNT(CASE WHEN ANSWER_SCORE = '2' THEN 1 END) AS two,
COUNT(CASE WHEN ANSWER_SCORE = '3' THEN 1 END) AS three,
COUNT(CASE WHEN ANSWER_SCORE = '4' THEN 1 END) AS four
FROM question_content_answer
INNER JOIN question_content
USING (CONTENT_NO)
WHERE question_content.TEST_PAPER_NO = '1'
GROUP BY question_content.CONTENT_NO
Related
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
I have 3 table:
user 1 - n (user_id)feedback(type_id) n - 1 word
User:
id name
1 name1
2 name2
3 name3
4 name4
5 name5
6 name6
Feedback:
id user_id type_id title
1 1 1 title1
2 1 1 title2
3 1 2 title3
4 2 1 title4
5 2 2 title5
6 2 2 title6
7 2 1 title7
Word:
id name category
1 great work FEEDBACK_TYPE
2 good work FEEDBACK_TYPE
I tried to count feedback of each user with below query
Result:
user_id countAllFeedback
1 3
2 4
Query
select
feed.user_id,
count(feed.id) as countAllFeedback
from
feedback feed
group by
feed.user_id;
I want to count feedback of each user by word name
user_id countAllFeedback countGreatWorkFeedback countGoodWorkFeedback
1 3 2 1
2 4 2 2
Please help me with your Query ^^ Thank you
You can try this below script. As you have nothing to collect dynamically from other table, No joining is required. You can use Type_ID as static logic inside CASE statement as those are known list in Word table.
SELECT A.user_id,
COUNT(*) countAllFeedback,
SUM(CASE WHEN A.type_id = 1 THEN 1 ELSE 0 END) countGreatWorkFeedback,
SUM(CASE WHEN A.type_id = 2 THEN 1 ELSE 0 END) countGoodWorkFeedback
FROM Feedback A
GROUP BY A.user_id
If there are possibilities of change in ID, you can do the below-
SELECT A.user_id,
COUNT(A.*) countAllFeedback,
SUM(CASE WHEN B.name = 'great work' THEN 1 ELSE 0 END) countGreatWorkFeedback,
SUM(CASE WHEN B.name = 'good work' THEN 1 ELSE 0 END) countGoodWorkFeedback
FROM Feedback A
INNER JOIN Word B ON A.type_id = B.id
GROUP BY A.user_id
there is no relevance of User table (as per demo data)
This can achieve using this following Query....
SELECT b.user_id,c.name,
SUM(b.user_id) AS 'countAllFeedback',
SUM(CASE WHEN c.name='great work' THEN 1 ELSE 0 END) AS 'countGreatWorkFeedback',
SUM(CASE WHEN c.name='good work' THEN 1 ELSE 0 END) AS 'countGoodWorkFeedback'
FROM tbl_Feedback b
LEFT OUTER JOIN
tbl_Word c
ON b.type_id=c.id
GROUP BY b.user_id,c.name
Note:- Here i'm using Case statement with sum function
I have this data:
id date userid result
1 2015-05-01 1 a
2 2015-05-02 1 b
3 2015-05-03 1 b
4 2015-05-03 1 a
5 2015-05-04 1 a
I need to get users sorted by result:
id a b
1 1 1
2 1 1
You want conditional aggregation:
SELECT user_id, sum(result = 'win') AS wins, sum(result = 'loss') as losses
FROM table
GROUP BY user_id
ORDER BY wins DESC
LIMIT 4;
I would do it like this:
SELECT user_id,
SUM(CASE WHEN result='win' THEN 1 ELSE 0 END) AS wins,
SUM(CASE WHEN result='loss' THEN 1 ELSE 0 END) AS losses
FROM table1
GROUP BY user_id
ORDER BY wins DESC
LIMIT 4
Fiddle is here: http://sqlfiddle.com/#!9/00ac7/8
SELECT a.invoice_id,
invoice_no,
a.cs_no,
identity,
name,
model,
body_color,
lot_no,
key_no,
serial_no,
location,
buyoff_date,
received_date,
tagged_date,
invoice_date,
a.pullout_date,
vin_no,
engine_no,
order_no,
b.approve,
COUNT(a.cs_no) AS buyoff_count,
SUM(CASE WHEN received_date IS NULL THEN 0 ELSE 1 END) AS received_count,
SUM(CASE WHEN tagged_date IS NULL THEN 0 ELSE 1 END) AS tagged_count,
SUM(CASE WHEN invoice_date IS NULL THEN 0 ELSE 1 END) AS invoice_count,
SUM(CASE WHEN a.pullout_date IS NULL THEN 0 ELSE 1 END) AS pullout_count
FROM buy_off a
LEFT JOIN pull_out b
ON a.invoice_id = b.invoice_id
AND a.cs_no = b.cs_no
WHERE tagged_date IS NOT NULL
AND invoice_date IS NULL
GROUP BY identity, tagged_date, invoice_id WITH ROLLUP
My above code have output like this:
invoice_id identity tagged_date
1 a 2/5/2015
total = 1
2 a 2/6/2015
3 a 2/6/2015
total = 2
4 b 2/5/2015
total = 1
5 b 2/6/2015
total = 1
grand total = 5
But this is what i want:
invoice_id identity tagged_date
1 a 2/5/2015
2 a 2/6/2015
3 a 2/6/2015
total = 3
4 b 2/5/2015
5 b 2/6/2015
total = 2
grand total = 5
Can you guys please help to achieve the above output.....Thanks in advance. :D
We have the table to track what product page a user visited.
product_tracking
____________________
id user_id product
1 1 A
2 1 B
3 2 A
4 1 A
I know we can use group by user_id, and group_by product but I need both.
Expected result :
result_table
____________________________
user_id A B C
1 2 1 0
2 1 0 0
any idea how to merge the 2 group by ?
You could do this:
SELECT
product_tracking.user_id,
SUM(CASE WHEN product_tracking.product='A' THEN 1 ELSE 0 END) AS A,
SUM(CASE WHEN product_tracking.product='B' THEN 1 ELSE 0 END) AS B,
SUM(CASE WHEN product_tracking.product='C' THEN 1 ELSE 0 END) AS C
FROM
product_tracking
GROUP BY
product_tracking.user_id
An alternative to using sum(...) is using count(...):
SELECT
product_tracking.user_id,
COUNT(CASE WHEN product_tracking.product='A' THEN 1 END) AS A,
COUNT(CASE WHEN product_tracking.product='B' THEN 1 END) AS B,
COUNT(CASE WHEN product_tracking.product='C' THEN 1 END) AS C
FROM
product_tracking
GROUP BY
product_tracking.user_id