I have a the following table structure:
Users table:
id username
1 bob
2 john
3 harry
Posts table:
id user_id status
1 1 4
2 2 4
3 3 2
4 1 1
5 2 1
If I wanted a summary of posts per user and cols of statuses, what is the most efficient sql to generate that?
Ex:
username status 4 status 2 status 1
bob 1 0 1
john 1 0 1
harry 0 1 0
This should work:
SELECT users.username
, sum(if(posts.status=4,1,0)) as `status 4`
, sum(if(posts.status=2,1,0)) as `status 2`
, sum(if(posts.status=1,1,0)) as `status 1`
FROM users, posts
WHERE users.id=posts.user_id
GROUP BY users.id
You can try somthing like this:-
SELECT A.USERNAME,
CASE WHEN STATUS = 4 THEN COUNT(B.STATUS) AS STATUS4 ELSE 0 END,
CASE WHEN STATUS = 2 THEN COUNT(B.STATUS) AS STATUS2 ELSE 0 END,
CASE WHEN STATUS = 1 THEN COUNT(B.STATUS) AS STATUS1 ELSE 0 END,
FROM USERS INNER JOIN POSTS
ON USERS.ID = POSTS.USER_ID
GROUP BY USERS.ID;
This might help you.
Related
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
purchased_models
user_id model_id
1 1
1 4
1 9
1 3
2 3
2 2
2 7
2 4
3 9
3 6
3 1
3 5
4 8
4 7
4 9
4 1
5 8
5 9
Using the above table, write a query to output a table the columns of which are user_id, number of models that user has purchased, and the number of people that have purchased any of the same models that the user in question has purchased. The result should look like this:
Result
user_id models_purchased users_purchasing_same_models
1 4 4
2 4 2
3 4 3
4 4 4
5 2 3
Got the first two with:
SELECT
DISTINCT user_id,
COUNT(DISTINCT model_id)
FROM
purchased_models
GROUP BY user_id
, but having trouble with the third one. Any help would be greatly appreciated. Thanks!
One way to do this is to use a correlated subquery that counts the distinct number of users that has purchased any of the models that the user referenced in the outer scope has purchased:
select
t1.user_id,
count(distinct t1.model_id) models_purchased,
(
select count(distinct user_id)
from purchased_models t2
where t2.model_id in (
select model_id
from purchased_models
where user_id = t1.user_id
and t2.user_id <> t1.user_id
)
) users_purchasing_same_models
from purchased_models t1
group by t1.user_id
order by t1.user_id
I would personaly try to self-join the table and conditional sum, as follows:
SELECT
a.user_id AS Id,
SUM(CASE WHEN a.user_id = b.user_id AND a.model_id=b.model_id THEN 1 ELSE 0 END) AS ModelCount,
SUM(CASE WHEN a.user_id != b.user_id AND a.model_id=b.model_id THEN 1 ELSE 0 END) AS Others
FROM purchased_models AS a
LEFT OUTER JOIN purchased_models AS b
ON TRUE
GROUP BY a.user_id
I have 2 tables, orders and order_lineitems.
orders contains the order status info (sold date, invoice no, type of sale, etc)
order_lineitems contains the item(s) for each order in a one to many relationship.
Since we provide shipping info by line item, ship_date is in the order_lineitems table, null if not shipped, a date if it is shipped.
I am trying to pull the orders where all items have shipped by comparing the number of line item rows against the line item rows that have a ship date. While I have successfully pulled all that info, I am unable to make the last step, limiting the result set to include only the completely shipped orders (number of rows = number of rows where ship_date is not null).
I know I am missing something simple, but just don't see it..
select sum(custom.lineitems) as totalitems, sum(custom.shipped) as totalshipped,
custom.invoice, z.shipregion
from (
select a.invoice, count(a.invoice) as lineitems, 0 as shipped
from order_lineitem a
group by a.invoice
UNION ALL
select b.invoice, 0 as notshipped, count(b.ship_date) as shipped
from order_lineitem b
where b.ship_date is not null
group by b.invoice
) as custom
left join orders z on custom.invoice = z.invoice
where z.result = 0
and z.respmsg like 'Approved%'
and z.shipregion <> 'PENDING'
and z.cancelorder = 0
group by custom.invoice;
This returns a result set like so (one row for each invoice in the DB)
totalitems totalshipped invoice shipregion
4 2 1000 REGION08
1 1 10001 REGION07
1 1 10004 REGION05
3 1 10006 REGION05
2 2 10007 REGION04
1 1 10008 REGION08
7 7 10009 REGION01
1 1 1001 REGION08
What I am looking for is a result set like this - only where totalitems = totalshipped
totalitems totalshipped invoice shipregion
1 1 10001 REGION07
1 1 10004 REGION05
2 2 10007 REGION04
1 1 10008 REGION08
7 7 10009 REGION01
1 1 1001 REGION08
Use HAVING clause
SELECT a.invoice, z.shipregion, COUNT(a.invoice) AS lineitems,
SUM(CASE WHEN a.ship_date IS NOT NULL THEN 1 ELSE 0 END) AS shipped
FROM order_lineitem a
LEFT JOIN orders z ON a.invoice = z.invoice AND z.result = 0 AND z.cancelorder = 0 AND
z.respmsg LIKE 'Approved%' AND z.shipregion <> 'PENDING'
GROUP BY a.invoice HAVING lineitems = shipped
OR
SELECT a.invoice, a.shipregion, a.lineitems, a.shipped
FROM (SELECT a.invoice, z.shipregion, COUNT(a.invoice) AS lineitems,
SUM(CASE WHEN a.ship_date IS NOT NULL THEN 1 ELSE 0 END) AS shipped
FROM order_lineitem a
LEFT JOIN orders z ON a.invoice = z.invoice AND z.result = 0 AND z.cancelorder = 0 AND
z.respmsg LIKE 'Approved%' AND z.shipregion <> 'PENDING'
GROUP BY a.invoice
) AS a WHERE a.lineitems = a.shipped
One more outer query needed.
select * from
(
\\Your whole query here
) as Result
where Result.totalitems = Result.totalshipped
I'm trying to put together a MYSQL query that will count the number of Non-Null (or better yet, non-zero) values in select fields in a single row and then sort from lowest to highest (based on the count). For example, I have a table with 5 fields... ID, Name, Score_1, Score_2, Score_3. I want to count how many times the value "0" exists in Score_1, Score_2 and Score_3 for each record, then sort from most non zero values to least.
ID Name Score_1 Score_2 Score_3
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
4 Mike 4 5 5
I assume the query has to look something like this...
Select ID, Name, Score_1, Score_2, Score_3 where (???) ORDER BY (???)
Output should look like this (ID 4 is displayed first since it has the least amount of non-zero entries)...
ID Name Score_1 Score_2 Score_3
4 Mike 4 5 5
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
I'm somewhat new to mysql query's, so any help would be greatly appreciated. I thought the COUNT function would help, but that function appears to count columns from all rows. Perhaps there is a way to use the COUNT function and limit it to a singel row so it can be sorted by that row count?
This should do what you want:
SELECT ID, Name, Score_1, Score_2, Score_3
FROM Table1
ORDER BY (Score_1 = 0) + (Score_2 = 0) + (Score_3 = 0)
Result:
ID Name Score_1 Score_2 Score_3
4 Mike 4 5 5
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
try This:
Select id, Count1, Count2, Count3, Count4
From
(Select
Sum(Case When IsNull(Score_1,0) = 0 Then 1 Else 0 End) Count1,
Sum(Case When IsNull(Score_2,0) = 0 Then 1 Else 0 End) Count2,
Sum(Case When IsNull(Score_3,0) = 0 Then 1 Else 0 End) Count3,
Sum(Case When IsNull(Score_4,0) = 0 Then 1 Else 0 End) Count4
From Table
Group By Id) Z -- This column (Id) better not be the PK for this table!!!
Order By Count1 + Count2 + Count3 + Count4