I'm adding a new column position to a table. This column should hold a unique value within all rows with the same userId. It doesn't really matter if they're ordered by id, creation or just randomly. For instance:
id userId time subn position created
1 1 33 FG 1 2016-08-13
2 1 57 FG 2 2016-08-15
3 1 20 XM 3 2016-08-11
4 2 81 FG 1 2016-08-01
5 3 12 FG 1 2016-08-27
6 3 33 FG 2 2016-08-18
How can I do this? I've tried creating a copy table (so that MySQL doesn't complain of target the target table for update being specified in from), setting the initial value to 0 and running this:
UPDATE userTimes_copy uic
SET position = (SELECT 1 + MAX(uic2.position)
FROM userTimes uic2
WHERE uic.userId = uic2.userId);
But it doesn't work. The first two rows, that have the same userId, get 486 (I have 485 rows) in position, and all of the rest 1.
Related
I have a database about sports event that contains:
*User ID
*Amount of Points that the user got on that event
*Time (HH:MM:SS) that took the user to complete track.
How can I first sort them by no. of points, then if two users have same amount of points, by time (shorter is better); and then insert the places to rows?
I have database like that:
ID No. of Points Time Place
------------------------------------
1 15 00:56:00
2 13 00:55:15
3 17 01:00:00
4 17 00:57:00
5 19 00:52:15
I need to have it with places:
ID No. of Points Time Place
------------------------------------
1 15 00:56:00 4
2 13 00:55:15 5
3 17 01:00:00 3
4 17 00:57:00 2
5 19 00:52:15 1
I hope, you understand that. Sorry for bad English.
Best regards,
You can do this with update statement as follows.
SET #placeValue:=0;
UPDATE [Table Name] SET Place=#placeValue:=#placeValue+1 ORDER BY
[Amount of Points] DESC,Time ASC
I'm trying to normalize some data, and can't seem to come up with a solution. What I have is a table like this:
weight position1 position2 position3
1 10 20 30
2 25 35 45
3 17 05 22
and one like this:
location position
6 1
7 1
8 2
9 2
10 2
11 3
12 3
How do I normalize the above so that given a location and a weight, I can find the value for a given position?
I can use Perl, Python, Excel, MySQL or pretty much any tool on the block to do the actual reshuffling of the data; where I'm having a problem is in coming up with a reasonable schema.
The desired outcome here is something like
if location == 11 -> position is 3
therefore,
if weight == 2 -> the value is 45
The only thing to do is "unpivot" your first table to this:
weight position value
1 1 10
1 2 20
1 3 30
2 1 25
2 2 35
2 3 45
3 1 17
3 2 05
3 3 22
The first two columns should contain unique pairs of values. If you have other information that only depends on weight, you would need another table for that. Same for positions.
Converting to the new model
If you already have the tables, then you can create the first table (t1) with this statement:
create table t1_new
select weight, 1 as position, position1 as value
from t1
union all
select weight, 2 as position, position2 as value
from t1
union all
select weight, 3 as position, position3 as value
from t1
Then, after verification of the result, drop t1, and rename t1_new to t1.
Querying from the new model
To query from these tables the value for a given location and weight, you should use a join:
select value
from t1
inner join t2 on t2.weight = t1.weight
where t2.location = 11
and t1.position = 3
i have a table :
a b c
1 10 1001
7 6 54
56 2000 31
1200 5 9
4 10 20
2 65 20
how can i select rows with column's value of this row smaller than 1000. i want to get this
a b c
7 6 54
4 10 20
2 65 20
mysql query still get all value :
SELECT a,b,c FROM test
where a <'1000' or b<'1000' or c<'1000'
It sounds like you would like to pull a row where there is NO column greater than 1000 in that row, if that is correct then you need to us AND instead of OR.
SELECT a,b,c FROM test
where a <'1000' AND b<'1000' AND c<'1000'
Hope that helps!
Getting stumped. How would I create a new column items_delta. I want items_delta to be a value that uses the column name, items, and last date. I need to find the delta between two different dates for the same name.
Ex: For name = A, the latest date is 12/14, and the items = 150. For name = A, I want the difference in items now compared to the items from the most previous date only. So essentially for name = A, I want (150 - 120)/120 = 0.25 which is the difference from the two latest dates.
Test Table
id name items last_date
1 A 150 12/14/15
2 B 100 12/14/15
3 C 50 12/14/15
4 A 120 12/13/15
5 B 110 12/13/15
6 B 90 12/12/15
7 A 110 12/12/15
8 A 100 12/11/15
9 A 150 12/10/15
10 C 35 12/10/15
What I want:
Test Table 2
id name items last_date items_delta
1 A 150 12/14/15 0.25
2 B 100 12/14/15 -0.09
3 C 50 12/14/15 0.43
Relatively new to mySQL, please be gentle.
![enter image description here][1]In the First Table tblserialnumbersprimary i have a primary field called as serialNoId ,this field is repeating multiple times in the second table tblserialnumbers. when we are inserting multiple records with that serialNoId.
I want to get Distinct records from the first table i.e tblserialnumbersprimary and corresponding multiple records from the dependent table i.e tblserialnumbers in MySQL
First Table Fields are:
tblserialnumbersprimary (serialNoId,serialPO,serialProductNo,
SerialNumberMode,serialNoAutoPrefix,serialDateOfCreation,
serialModifiedBy,serialStatus)
Second Table Fields are:
tblserialnumbers(serialId,serialNoId,
serialNo,serialNoBatchId![enter image description here][1])
I tried With This Joins Query.But its giving multiple records of first table
select * FROM tblserialnumbersprimary
LEFT OUTER JOIN tblserialnumbers
ON (tblserialnumbersprimary. serialNoId = tblserialnumbers.serialNoId )
First Table values are:
serialNoId serialPO serialProductNo SerialNumberMode serialNoAutoPrefix serialDateOfCreation serialModifiedBy serialStatus
1 PO1 PROD121 Automatic TCS-03 2/25/2014 12:00:00 AM admin 0
2 PO2 PROD345 Automatic TCS-03 2/25/2014 12:00:00 AM admin 1
3 PO5 PROD816 Automatic 2/26/2014 12:00:00 AM admin 1
4 PO1 PROD121 Automatic GTS-03 2/26/2014 12:00:00 AM admin 1
Second Table values are:
serialId serialNoId serialNo serialNoBatchId
1 1 TCS-03-PROD121-1 batch1
2 1 TCS-03-PROD121-2
3 1 TCS-03-PROD121-3 batch3
4 1 TCS-03-PROD121-4
5 1 100
6 1 101
1 2 TCS-03-PROD345-1 batch1
2 2 TCS-03-PROD345-2
3 2 TCS-03-PROD345-3 batch3
4 2 TCS-03-PROD345-4
1 3 --1
2 3 --2
3 3 --3
4 3 --4
5 3 12
6 3 13
7 3 11
1 4 -PROD816-1 batch1
2 4 -PROD816-2 batch2
1 5 GTS-03-PROD121-1 batch1
2 5 GTS-03-PROD121-2
3 5 GTS-03-PROD121-3 batch3
4 5 GTS-03-PROD121-4
Use alias and join for record in multiple table.
For i.e. `SELECT table2.id,
table1.pid,
table1.color,
table2.pname,
table2.image
FROM tbl_productcolor table1
JOIN tbl_product table2 ON table1.pid = table2.id;`
Apply your table in this way you can get all the data.