Supposed I have a SQL table that looks like this
Now I am suppose to do this 'logic' so that I know that on 23/6/2017, the word 'accessories' appeared 2 times and 'tools' appeared 1 time.
I think there is some kind of way to do this is mysql, something along the lines of COUNT() and GROUPBY but I cannot get the result I want.
Appreciate any guidance. Thanks!
You can do this by using GROUP BY :
SELECT date, category, count(*) as count FROM table_name GROUP BY date, category
You have to put every selected columns after group by otherwise it will show query error.
SELECT date, category, count(*)
FORM yourtable
GROUP BY date, category
It's as simple as counting the categories corresponding to each date
SELECT date
category
COUNT(*)
FROM table_name
GROUP BY date, category
Related
I have table with users actions. One of them occurs every time when user opens certain page.
Table structure:
id, user_id, action_type, created_at...
I need to select from this table all actions per day/week... but without repeating of similar in one day. For example: user has visited 10 pages but 5 of them was the same. The result of selection should contain only unique pages per day.
Is it possible to do with only MySQL logic? Or better I should update repeated action if it occurs the same day?
One approach uses select distinct:
select distinct user_id, action_type, date(created_at) created_date
from mytable
If needed, you can also count how many times each action_type was met on a user_id and day basis with aggregation:
select user_id, action_type, date(created_at) created_date, count(*) cnt
from mytable
group by user_id, action_type, date(created_at)
I suggest the following SQL code :
SELECT DISTINCT URL
FROM table_name
GROUP BY date;
I assume that your table name is table_name, you have the URLs (pages) in the column named URL and you you track the date in the column named date;
Hello there I have the following doubt I want to count how many times in a month I enter data.
My database is:
Date:
10/2010
10/2010
09/2010
08/2010
I have the following query.
SELECT DISTINCT (date)
FROM employee
WHERE date
IN (SELECT date
FROM employee
GROUP BY date
HAVING count( date ) >0)
ORDER BY date DESC;
This query gives me:
Date:
10/2017
8/2017
9/2017
But I want you to give me something like that.
Count | Date
2 | 10/2017
1 | 9/2017
1 | 10/2017
I hope I have explained my regards.
You're overcomplicating it; no subquery, or DISTINCT, needed.
SELECT `date`, count(*)
FROM `employee`
GROUP BY `date`
HAVING count(*) > 0
ORDER BY `date` DESC;
I am a little confused as to what reason you would have for the HAVING count() > 0 though; the only way something could have a zero count would mean it wasn't in the table (and therefore wouldn't show up anyway).
Other observations:
DISTINCT is not a function; enclosing the date in parenthesis in the SELECT clause has absolutely no effect. (Also, DISTINCT is almost never appropriate for a GROUPing query.)
COUNT(somefield) is the same as COUNT(1), COUNT(*). If you want the count of unique values you can do COUNT(DISTINCT somefield); but it wouldn't make sense to COUNT(DISTINCT groupingfield) as that would always result in 1.
The query you wrote is a bit complicated. Distinct and group by are doing the same thing for you here. When you do a group by count will automatically give you the count of grouped rows. Also you will have unique dates as well. Try this.
SELECT count(date), date
FROM employee
GROUP BY date
HAVING count( date ) >0
ORDER BY date DESC;
I need a query where the result only shows records in which there are multiple values in a group concat. I'm only interested in records that have more than one value displayed in the group_contact column.
I have a change log table that stores price changes of a PO. Each change in PO price is another entry in the table. Here's my query so far:
SELECT PONum, POLine, GROUP_CONCAT(DISTINCT DocUnitCost) AS priceChanges
FROM `podetailcl`
WHERE 1 -- ?something here saying where COUNT of price changes > 1?
GROUP BY PONum, POLine
Additional Information:
I might need to use HAVING but I'm not too clear on how to structure that. I did try the suggestions on other pages but I'm still lost on this one.
You should use having for filter aggregated result and not where ..
SELECT PONum, POLine, GROUP_CONCAT(DISTINCT DocUnitCost) AS priceChanges
FROM `podetailcl`
GROUP BY PONum, POLine
having count(DISTINCT DocUnitCost)>1
SELECT PONum, POLine, GROUP_CONCAT(DISTINCT DocUnitCost) AS priceChanges
FROM `podetailcl`
GROUP BY PONum, POLine
HAVING COUNT(DISTINCT DocUnitCost) > 1
I have one table with this headers
id city date item
I am interested in identify the id with the same item in the same month and count how many items repeated receive in the same month. So my query in mysql is:
select a.id, concat(a.id, month(a.date), a.item) as a from table
where a = (select concat(b.id, month(b.date), b.item) as b from table)
group by a.id
having count(a=b)>=1
But that query take too much time and may not work, could you help me?
You haven't reacted to the comments made three hours ago. But from your comments to Seth McClaine's answer I read that you want a list of rows showing id (which despite its name is not the table's unique ID!), month, item, and the number of occurences.
One row per id, month and item means: group by id, month, item.
Use a HAVING clause in order to only show occurences > 1.
select id, month(date), item, count(*)
from mytable
group by id, month(date), item
having count(*) > 1;
Well, this is about what Seth McClaine already told you. And while you commented on his answer, you didn't say what is missing from it for you to accept it.
I think you should be able to do something like this, giving you the count total of items with matching id, month, and item
Select Count(*) as total, month(date) as month, item, id
From table
group by (item, month(date), id)
I have a voting application that writes values to a mysql db table. It is a preference/weighted voting system so people choose a first option, second option, and third option. These all go into separate fields in the table. I'm looking for a way to write a query that will assign numerical values to the responses (3 for a first response, 2 for a second, 1 for a first) and then display the value with the summed score. I've been able to do this for total number of votes
select count(name) as votes,name
from (select 1st_option as name from votes
union all
select 2nd_option from votes
union all
select 3rd_option from votes) as tbl
group by name
having count(name) > 0
order by 1 desc;
but haven't quite figured out how to assign values to response in each column and then pull them together. Any help is much appreciated. Thanks!
You could do something like this:
select sum(score) as votes,name
from (select 1st_option as name, 3 as score from votes
union all
select 2nd_option as name, 2 as score from votes
union all
select 3rd_option as name, 1 as score from votes) as tbl
group by name;