I'm trying to figure out a query that counts the number of times a user has given a unique answer and returns the user, answer, and count of those answers. The table looks like this:
Table Likes
ID Username Choice
1 Bobby Cat
2 Tina Rabbit
3 Bobby Rabbit
4 Alex Goose
5 Bobby Cat
6 Alex Goose
7 Alex Dog
After running the query, I hope to get each user along with his choices and the count of each like so:
Result
Username Choice Count(Selection)
Bobby Rabbit 1
Bobby Cat 2
Tina Rabbit 1
Alex Goose 2
Alex Dog 1
The best I've come up with so far is:
SELECT Username, Choice, Count(Choice) FROM Likes GROUP BY Username
But this is not quite right.
You need to add Choice in Group by to count the number of times a user has given a unique answer. Without choice in Group by u will get the count of answers given by each user
SELECT Username,
Choice,
Count(Choice)
FROM Likes
GROUP BY Username,
choice
This query will be helpful.
SELECT Username, Choice, Count(Choice) AS CountSelection
FROM Likes
GROUP BY Username, Choice
Related
I am a newbie in programming and just started with a project. My question is how should the query look like to get the right data?
My table:
id rfid beer email
1 12345 3 markus.test#gmail.com
2 54321 4 hans.test#gmail.com
3 63737 1 mark.test#gmail.com
4 12345 -2 markus.test#gmail.com
5 54321 -3 hans.test#gmail.com
The query result should look like
[{email: markus.test#gmail.com, beer:1},{email:hans.test#gmail.com, beer:1}....]
I know that you can use SUM(beer) in the query, but I don't know who to match them with the right email.
If you group your data then aggregate functions like sum() will sum for each group and not the complete result
select email, sum(beer) as beer
from your_table
group by email
id_no doc_id item_no product customer
123 2 1 A Daisy
123 2 9 A Ben
123 4 3 A Daisy
123 4 4 A Ben
123 6 11 B Daisy
123 6 13 B Ben
when I put it in my report it results to
Daisy Daisy
Ben
And it is also the result in mysql
select distinct customer from receipt where id_no like '123'
result:
Daisy
Daisy
Ben
Another query that I tried:
select distinct id_no, customer, product from receipt where id_no like '123'
result:
123 Daisy A
123 Daisy B
123 Daisy A
123 Ben A
123 Ben B
desired result:
Daisy
Ben
Please help me please.
Thank you guys for the help I found out why the other one keeps on showing. It is because the other Daisy is spelled as Daissy that's why.
Most likely your Customer name contains additional characters between the two records. Depending on how the datatype is implemented, spaces could matter and have contributed to the difference.
Try concatenating a character before and after customer.
I am unfamiliar with the concepts in Crystal Reports, but from what I understand, you would have to create a formula like so:
"XXX" & {Receipt.Customer} & "XXX"
If you run it again, you might recognize there is additional space like so:
XXXDaisyXXX
XXXDaisy XXX
^____ Additional Space
There is no chance of error while you using distinct ..it should return distinct value ...any way you can try another way
SELECT customer FROM receipt WHERE id_no like '123' GROUP BY customer
I don't see why you are fetching three records. I tried implementing your database and ran your query. It returned the result as expected.
See the above pic. There may be some issue with the data type you used. You may try grouping via customer, but I don't think it should affect your result anyway.
Also Check if the data types match.
The selection you made from customer id and id_no is unique and with distinct it should return only two rows
plase try this code
i get solution
select distinct `customer` from receipt where `id_no`='123'
this is right
i tryied this is my past project
best of luck
I have a simple question that I wasn't really sure how to search for (or title!). I apologize if this has been asked a million times. For the following table, how do I generate a report that will detail the number of companies that a person has worked for and how many people have also worked for that same number? So, for example, this table should return:
people, companiesperperson
1, 1
2, 2
1, 3
for the following table called personalinfo:
id_number first last company
1 John Doe Intel
2 John Doe Microsoft
3 Phil Jenkins Amgen
4 Phil Jenkins Bayer
5 Phil Jenkins Sanofi
6 Josh Edwards Walgreens
7 Amy Dill URS
8 Amy Dill ARCADIS
Let me know if this is still confusing and if I can further clarify what I am looking to do.
Thanks!
This is a rough estimate of the query but
SELECT count as companiesperperson, COUNT(first, last) as people FROM
(SELECT COUNT(company) as count, first, last FROM personalinfo GROUP BY (first, last)) as a
GROUP BY count
To explain the query first in the subquery we are asking for the names and count of companies after splitting up all the rows by names
Then in the outer query we split up all the rows by their count and ask how many unique names can be found in each group.
There may be a few syntax errors I've left straggling but the group by feature is really what's essential to understanding how to solve this question.
I have two tables, follow and followed. I want to get all the rows in the follow table such that follow.screen != followed.following_screen_name.
follow table
ID screen_name
-----------------
1 eddie
2 jason
3 omar
4 jonathan
5 jack
followed table
ID my_screen_name following_screen_name
-------------------------------------------
1 john eddie
2 kenny eddie
3 kenny omar
4 john jason
5 john omar
Query I tried which didn't work
SELECT follow.screen_name from follow, followed where followed.my_screen_name='john'
AND follow.screen_name != followed.following_screen_name
Expected results
ID screen_name
-----------------
1 jonathan
2 jack
you can get this by doing a LEFT JOIN
SELECT F.screen_name FROM follow F
LEFT JOIN followed FD
on F.screen_name = FD.my_screen_name
OR F.screen_name = FD.following_screen_name
WHERE FD.my_screen_name IS NULL
and FD.following_screen_name IS NULL
Another way is to use NOT EXISTS, get all rows that exists in followed and do NOT EXISTS clause to get desired result.
SELECT F.screen_name FROM follow F
WHERE NOT EXISTS
(
SELECT 1 FROM followed FD
WHERE F.screen_name = FD.my_screen_name
OR F.screen_name = FD.following_screen_name
)
There are plenty of ways to solve this, but common to all is that you need to compare the follow.screen_name to both followed.my_screen_name and followed.following_screen__name.
One way is to use NOT IN with a UNION:
select screen_name
from follow
where screen_name not in (
select following_screen_name
from followed
where following_screen_name is not null
union all
select my_screen_name
from followed
where my_screen_name is not null
)
While this approach is nice for clarity, it may not be as good for performance as using a left join or not exists.
A nice place to pick up mysql syntax and logic is here.
But try this code, it selects every row where the screen_name is not identical to anything produced in the next two queries:
SELECT * from follow WHERE screen_name
not in (select screen_name from followed)
AND not in (select followed_screen_name from followed);
The last two queries would look this and the WHERE filters all of the rows out with screen names identical to the fields below.
my_screen_name following_screen_name
-------------------------------------
john eddie
kenny eddie
kenny omar
john jason
john omar
I'm working on a project where I'm dumping data into a mysql database about different people, however, each person can have many entries so I'm not sure how to have a lot of columns.
e.g
Name id
jack 234 01241990 13241990 03451993 10945
james 222 01131998 14242001 03414235 10945435 3456363 3465758
jill 1234 01131998 14242001 03414235 10945435 3456363 3465758 4253156316 6427247 583583
As you can see there can be many entries for each person, not in 100's, but I think the max can be around 20-30ish? So how do I build a database that I can insert values into without knowing how many entries will be per person, beforehand.
I am using perl script to insert values. Any ideas will be helpful
EDIT: People are suggesting to create two tables, however, when I joint he tables, I want one row for each person.
e.g After joining my view should look like
james 222 01131998 14242001 03414235 10945435 3456363 3465758
My suggestion would be to split the data into two tables (People and Data):
People:
NAME ID
Jack 234
James 222
Jill 1234
Data:
ID PeopleID Data
1 234 01241990
2 234 13241990
.
.
99 1234 6427247
100 1234 583583
You can then use joins to get the data for each person
SELECT p.Name,
p.ID,
d.Data
FROM People p
JOIN Data d
ON d.PeopleID = p.ID
ORDER BY p.Name --(assuming you want names in alphabetical order)
You should get something like the following
Name ID Data
Jack 234 01241990
Jack 234 13241990
.
.
Jill 1234 6427247
Jill 1234 583583