Mysql numbered rows in group - mysql

I have table
users|visits
--------------
user1|visit1
user1|visit2
user1|visit3
user2|visit1
user2|visit2
I want to get numbered rows in any group. How i can do this?
users|visits|visit number
-----------------------
user1|visit1|1
user1|visit2|2
user1|visit3|3
user2|visit1|1
user2|visit2|2

Try as below :
SELECT
users, visits, count(*) as `visit number`
FROM tableName group by visits,users

From what you have provide about your problem i can imagine that you have records something like this
users|visits
------------
user1|visit1
user1|visit2
user1|visit1
user1|visit3
And you want to get
users|visits|visit number
-------------------------
user1|visit1|2
user1|visit2|1
user1|visit3|1
To do that use query like this
SELECT
DISTINCT a.users,
a.visits,
(select count(*) from tableName b where b.users = a.users and b.visits = a.visits) as `visit number`
FROM tableName a

Related

How to count occurrences with derived tables in SQL?

I have this very simple table:
CREATE TABLE MyTable
(
Id INT(6) PRIMARY KEY,
Name VARCHAR(200) /* NOT UNIQUE */
);
If I want the Name(s) that is(are) the most frequent and the corresponding count(s), I can neither do this
SELECT Name, total
FROM table2
WHERE total = (SELECT MAX(total) FROM (SELECT Name, COUNT(*) AS total
FROM MyTable GROUP BY Name) table2);
nor this
SELECT Name, total
FROM (SELECT Name, COUNT(*) AS total FROM MyTable GROUP BY Name) table1
WHERE total = (SELECT MAX(total) FROM table1);
Also, (let's say the maximum count is 4) in the second proposition, if I replace the third line by
WHERE total = 4;
it works.
Why is that so?
Thanks a lot
You can try the following:
WITH stats as
(
SELECT Name
,COUNT(id) as count_ids
FROM MyTable
GROUP BY Name
)
SELECT Name
,count_ids
FROM
(
SELECT Name
,count_ids
,RANK() OVER(ORDER BY count_ids DESC) as rank_ -- this ranks all names
FROM stats
) s
WHERE rank_ = 1 -- the most popular ```
This should work in TSQL.
Your queries can't be executed because "total" is no column in your table. It's not sufficient to have it within a sub query, you also have to make sure the sub query will be executed, produces the desired result and then you can use this.
You should also consider to use a window function like proposed in Dimi's answer.
The advantage of such a function is that it can be much easier to read.
But you need to be careful since such functions often differ depending on the DB type.
If you want to go your way with a sub query, you can do something like this:
SELECT name, COUNT(name) AS total FROM myTable
GROUP BY name
HAVING COUNT(name) =
(SELECT MAX(sub.total) AS highestCount FROM
(SELECT Name, COUNT(*) AS total
FROM MyTable GROUP BY Name) sub);
I created a fiddle example which shows both queries mentioned here will produce the same and correct result:
db<>fiddle

My sql select that has multiple row in the same criteria

I have mysql table like this
I want to get row that has minimum 2 or more than 2 (multiple) row only from this table, so the result would be like this
What do i do?
thank you
Use GROUP BY and HAVING clauses
SELECT t.* FROM my_table t
JOIN (
SELECT cust_id, MIN(transaction_no) AS transaction_no
FROM my_table
GROUP BY cust_id
HAVING COUNT(cust_id) > 1
) agg ON t.transaction_no = agg.transaction_no

how to find next max(id) in a table

lets say
My table contains
Id Status
1 0
2 1
3 0
4 0
5 1
6 0
I need output like
Id Status
5 1
I tried like Max(id) but it gives output as
id status
6 0
I can only suppose, that you want to know about the maximum id of those entries having Status=1, right? Then use
select max(id) from mytable where Status=1
Try this:
SELECT id, status
FROM myTable ORDER BY `id` DESC LIMIT 1 , 1
I have assumed that you are looking for the second highest Id record values.
I guess you would like to have the max(id) where Status=1?
Then use select max(id) from table where Status=1
Since you want to get the absolute max from both columns use :
SELECT GREATEST(MAX(id), MAX(status));
Your question is a bit unclear what it is you actually want. From your example, I guess you want the maximum ID for the max status.
This is the max id for every status:
select max(id), status from table
group by status;
This will result in 5,1 and 6,0.
You could then filter out what you don't need, e.g.
select * from (
select max(id), status from t
group by status
) maxidperstatus
where maxidperstatus.status = (select max(status) from t);
Try this also
select Id,Status
from table
where Id=(
select MAX(Id) from table where Id <> (select MAX(Id) from table)
)
I assume that you want the Id where the status is the biggest
Try this
select max(status),id from table group by id order by max(status) DESC

Select last value in SQL table with conditions

i have a sample data like this:
Now i want to get lastest data used to have user_id = 41 and highest price, output like this:
How can i do it with SQL command line ?
Thank for read
Try this query
select User_id,auction_id,price from tablename where price in(select price from tablename where id in(select max(id) from tablename group by user_id))
As per your output that you wish try this:
select user_id,auction_id,price
from table_name
where user_id=41
order by id desc;
If you have updated output incorrectly then try it as per your provided conditions-
select user_id,auction_id,price
from mytable
where user_id=41
order by auction_id;
But if as per your output you want 1 latest row of user_id=41 and then all rows of highest price then you can try it-
SELECT user_id,auction_id,price
FROM mytable
WHERE user_id=41
ORDER BY id DESC LIMIT 1
UNION
SELECT b.user_id,b.auction_id,price
FROM mytable b
JOIN (
SELECT MAX(price) AS price
FROM mytable
) a ON a.price=b.price;

how to count the rows and use group by accordingly?

i could not create the correct query.
This is the screenshot from the table
I am trying to count only the unique question_id for each cat_id. SO the output must be
totalQuestion cat_id
2 2
2 1
we can use GROUP BY and get distinct count for the questions
SELECT COUNT(DISTINCT question_id) as total_questions, cat_id
from tableA
group by cat_id
select
count(distinct qid) as totalQuestion
, cid
from table_name group by cid