I will ask for help from yours.
Example table:
| ID | NAME |POINT|
| 1 | alex | 2 |
| 2 | alex | 2 |
| 3 | jenn | 4 |
| 4 | shama| 3 |
| 5 | jenn | 4 |
| 6 | Mike | 1 |
I want to find repetitive name and change name value and sum repetitive value.
Like
| ID | NAME |POINT|
| 1 | alexander| 4 |
| 2 | jennifer | 8 |
Is it possible mysql query?
Thanks.
try this
select id,
case when name = 'alex' then replace(NAME,'alex','alexander')
when name = 'jenn' then replace(NAME,'jenn','jennifer')
end as d,sum(point)
from name group by d having d is not null;
Is this what you want?
select (#rn := #rn + 1) as id, name, sum(point) as point
from t cross join
(select #rn := 0) params
group by name
having count(*) >= 2;
Related
Let say I have this table:
| Order ID | User | Order Date |
-----------------------------------
| 1 | Dave | 03/01/2017 |
| 2 | Jim | 03/09/2017 |
| 3 | John | 03/15/2017 |
| 4 | John | 03/18/2017 |
| 5 | Jim | 03/18/2017 |
| 6 | Dave | 03/30/2017 |
| 7 | John | 04/04/2017 |
| 8 | Jim | 04/16/2017 |
Things I want is to put one more column to indicate the n-th order per individual, because I need to analyze the repeated behavior of users.
So the output should be like this:
| Order ID | User | Order Date | n-th |
-----------------------------------------
| 1 | Dave | 03/01/2017 | 1 |
| 2 | Jim | 03/09/2017 | 1 |
| 3 | John | 03/15/2017 | 1 |
| 4 | John | 03/18/2017 | 2 |
| 5 | Jim | 03/18/2017 | 2 |
| 6 | Dave | 03/30/2017 | 2 |
| 7 | John | 04/04/2017 | 3 |
| 8 | Jim | 04/16/2017 | 3 |
That means:
order #1 is Dave's 1st order
order #2 is Jim's 1st order
...
order #5 is Jim's 2nd order
and so on!
How can I get this using raw query in SQL?
Thank you.
In most databases, you would use row_number(). In MySQL, this is best done using variables:
select t.*,
(#rn := if(#u = user, #rn + 1,
if(#u := user, 1, 1)
)
) as nth
from t cross join
(select #u := '', #rn := 0) params
order by user, orderdate;
You can also formulate this query using a correlated subquery, but the method using variables should have better performance.
To get the data back in its original format, use the above as a subquery and then sort by the order id.
I have a table looks like this:
|__name___|value__|
|__James__|___6___|
|__Jerry__|___5___|
|__Jerry__|___4___|
|__James__|___3___|
|__James__|___2___|
|__James__|___2___|
and I need to get from first table output like :
|_name____|value_|
| James | 2 |
| | 2 |
| | 3 |
|_________|___6__|
| Jerry | 4 |
|_________|___5__|
Any ideas?
this also works. It stores the last record in #old_name and compare it
SELECT
IF(name = #old_name ,'',(#old_name := name)) AS name
,value
FROM mytable
,(SELECT #old_name:='')AS tmp
ORDER BY name,value;
|_name____|value_|
| James | 2 |
| James | 2 |
| James | 3 |
| James | 6 |
| Jerry | 4 |
| Jerry |___5__|
only like this (select * from $table_name order by name;),
cant like this:
|_name____|value_|
| James | 2 |
| | 2 |
| | 3 |
|_________|___6__|
| Jerry | 4 |
|_________|___5__|
SELECT CASE WHEN
value = (SELECT MIN(value) FROM table t WHERE t.name = name)
THEN name
ELSE ''
END AS name, value
FROM table
ORDER BY name ASC, value ASC
Bernd Buffen.. Your Select return something like that :/ :
|_name____|value_|
|_________| 2 |
|_________| 2 |
|_________| 3 |
|_________| 4 |
|__James__| 5 |
|__Jerry__|___6__|
I need to write an SQL query to combine two tables...
Table: names
+-------------+---------------+
| id | Name |
+-------------+---------------+
| 1 | Bob |
| 2 | Geoff |
| 3 | Jim |
+-------------+---------------+
Table: attributes
+-------------+---------------+
| id | Attribute |
+-------------+---------------+
| 1 | Age |
| 2 | Height |
| 3 | Weight |
+-------------+---------------+
...so that each record in the names table has a copy of each record in the Attributes table.
i.e.
+-------------+---------------+---------------+
| id | Name | Attribute |
+-------------+---------------+---------------+
| 1 | Bob | Age |
| 2 | Bob | Height |
| 3 | Bob | Weight |
| 4 | Geoff | Age |
| 5 | Geoff | Height |
| 6 | Geoff | Weight |
| 7 | Jim | Age |
| 8 | Jim | Height |
| 9 | Jim | Weight |
+-------------+---------------+---------------+
Is there a way to do this?
SELECT n.name, a.attribute from names n cross join attributes a
The same can be achieved with following query:
SELECT n.name, a.attribute from names n join attributes a ON 1=1
The only thing is that you show id from 1 to 9 in the output but there are no such IDs in the sample data. But if you have table with autoincrement then after you insert this data, the ID will be as you expected.
UPDATED:
As suggested in comments and if you need id, then you can do following:
SELECT (n.id-1)*3+a.id AS id, n.name, a.attribute from names n cross join attributes a
If you want to get an incremented id, just use a variable:
select (#rn := #rn + 1) as id, n.name, a.attribute
from names n cross join
attributes a cross join
(select #rn := 0) vars;
select stu.id,stu.name,attribute.attribute from stu cross join
attribute order by stu.id
In MySQL:
Lets say I've this table:
id | name | count |
1 | John | |
2 | John | |
3 | John | |
4 | Mary | |
5 | Lewis| |
6 | Lewis| |
7 | Max | |
8 | Max | |
The names are already grouped, so the same name comes up together.
Now I want the table to be like this:
id | name | count |
1 | John | 1 |
2 | John | 2 |
3 | John | 3 |
4 | Mary | 1 |
5 | Lewis| 1 |
6 | Lewis| 2 |
7 | Max | 1 |
8 | Max | 2 |
Notice it auto increments the value of count everytime there is a repetition of the same name.
Thanks!
You can use a user variable.
Something like this:-
UPDATE somepeople a
INNER JOIN (
SELECT id, name, IF(#PrevName=name, #aCnt := #aCnt + 1, #aCnt := 1) AS sequence, #PrevName:=name
FROM somepeople,
(SELECT #aCnt:=1, #PrevName:='') Sub1
ORDER BY name, id) b
ON a.id = b.id
SET a.count = b.sequence
I have a problem, please see my database:
-------------------
| id | article_id |
-------------------
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 3 |
| 7 | 3 |
| 8 | 3 |
| 9 | 3 |
| 10 | 3 |
And I want to receive something like this (order by votes, from max to min):
---------------------------
| id | article_id | votes |
---------------------------
| 1 | 3 | 5 |
| 2 | 1 | 3 |
| 3 | 2 | 2 |
Could you please help me to write proper sql query?
SET #currentRow = 0;
SELECT #currentRow := #currentRow + 1 AS id, t.article_id, t.c AS `votes`
FROM (
SELECT article_id, count(*) as `c`
FROM table_votes
GROUP BY article_id
) t
ORDER BY t.c DESC
please note that you can't select an id column like this in this context, and your "expected result" is incorrect. I tried to adapt it at a maximum.
cheers
SELECT article_id, COUNT(article_id) AS votes
FROM votes_table
GROUP BY article_id
ORDER BY votes DESC;