I have an issue similar to this question:
updating columns with a sequence number mysql
However, I have a second column that also "sometimes" needs updated. For example, consider this table:
uID | X | Y
1 | 6 | 0
2 | 2 | 0
3 | 7 | 0
4 | 7 | 1
5 | 3 | 0
6 | 1 | 0
I would like 'X' re-ordered sequentially which I can do with the solution from the link, above:
SET #rank:=0;
UPDATE `myTable` SET
X = #rank:= #rank+1
ORDER BY X
However, if the record has Y values greater than 0 I need its X to be updated at the same time. The Y value, being the 2 dimensional value, needs to remain with its 'X'. Make sense? So I need the table to end up:
uID | X | Y
6 | 1 | 0
2 | 2 | 0
5 | 3 | 0
1 | 4 | 0
3 | 5 | 0
4 | 5 | 1
Ideas?
Try:
SET #rank:=0;
UPDATE `myTable` SET
X = #rank:= #rank + if(y=0,1,0)
ORDER BY X
demo--> http://www.sqlfiddle.com/#!2/f158e/1
Related
I have some data (~70,000 rows) that is in a similar format to the below.
+-----------+-----+-----+----+-----------+
| ID | A | B | C | Whatever |
+-----------+-----+-----+----+-----------+
| 1banana | 42 | 0 | 2 | Um |
| fhqwhgads | 514 | 6 | 9 | Nevermind |
| 2banana | 69 | 42 | 0 | NULL |
| pears | 18 | 96 | 2 | 8.8 |
| zubat2 | 96 | 2 | 14 | "NULL" |
+-----------+-----+-----+----+-----------+
I want to make an output table that counts how many times each number occurs in any of the three columns, such as:
+--------+---------+---------+---------+-----+
| Number | A count | B count | C count | sum |
+--------+---------+---------+---------+-----+
| 0 | 0 | 1 | 1 | 2 |
| 2 | 0 | 1 | 2 | 3 |
| 6 | 0 | 1 | 0 | 1 |
| 9 | 0 | 0 | 1 | 1 |
| 14 | 0 | 0 | 1 | 1 |
| 18 | 1 | 0 | 0 | 1 |
| 42 | 1 | 1 | 0 | 2 |
| 69 | 1 | 0 | 0 | 1 |
| 96 | 1 | 1 | 0 | 2 |
| 514 | 1 | 0 | 0 | 1 |
+--------+---------+---------+---------+-----+
(In my real-world use, there would be at least 10 times as many rows in the input table than in the query result)
Whether or not the query returns a row of zeros for numbers that aren't anywhere in those 3 columns isn't that important, as is a lack of a distinct sum column (though my preferences are that it does have the sum column and numbers not in any column are excluded).
Currently, I am using the following query to get ungrouped data:
SELECT * #Number, COUNT(DISTINCT A), COUNT(DISTINCT B), COUNT(DISTINCT C)
FROM
( # Generate a list of numbers to try
SELECT #ROW := #ROW + 1 AS `Number`
FROM DataTable t
join (SELECT #ROW := -9) t2
LIMIT 777 # None of the numbers I am interested in should be greater than this
) AS NumberList
INNER JOIN DataTable ON
Number = A
OR Number = B
OR Number = C
#WHERE <filters on DataTable columns to speed things up>
#WHERE NUMBER = 10 # speed things up
#GROUP BY Number
The above query with the commented-out parts of the code left as they are returns a table similar to the data table, but sorted by which number of the entry it matches. I would like to group together all rows starting with the same Number, and have the values in the "data" columns of the query result be the count of how many times the Number occured in the corresponding column of DataTable.
When I uncomment the grouping statements (and delete the * from the SELECT statement), I can get the count of how many rows each Number appeared in (useful for the sum column of the desired output). However, it does not give me the actual totals of how many times the Number matched each data column: I just get three copies of the number of rows where Number was found. How do I get the groupings to be by each actual column instead of the total number of matching rows?
Additionally, you may have noticed that I have some lines with comments regarding speeding things up. This query is slow, so I added a couple filters so testing it runs faster. I would very much like some way to make it run fast so that sending the results of the query from the complete set to a new table is not the only reasonable way to re-use this data, since I would like to have the ability to play around with the filters on DataTable for non-performance reasons. Is there a better way to structure the overall query so that it runs faster?
I think you want to unpivot using union all and then an aggregation:
select number, sum(a) as a, sum(b) as b, sum(c) as c, count(*) as `sum`
from ((select a as number, 1 as a, 0 as b, 0 as c from t
) union all
(select b, 0 as a, 1 as b, 0 as c from t
) union all
(select c, 0 as a, 0 as b, 1 as c from t
)
) abc
group by number
order by number;
I am dealing with a table Employee Complaint which has columns EmployeeId ComplaintSeverity and ComplaintByUser. ComplaintSeverity has four level 0,1,2, and 3.
So the table will look like this ,Example
ComplaintId|EmployeeId|ComplaintSeverity|usr_id
-----------------------------------
1 | 1 | 0 | 3
2 | 2 | 1 | 4
3 | 3 | 0 | 5
4 | 1 | 2 | 4
5 | 4 | 1 | 5
6 | 2 | 2 | 2
7 | 2 | 2 | 4
Any user can complaint employee with any of these level
When client search with severitylevel as 0,
The row should fetch as
ComplaintId|EmployeeId|ComplaintSeverity
----------------------------
3 | 3 | 0
for severitylevel as 1,
ComplaintId|EmployeeId|ComplaintSeverity
----------------------------
5 | 4 | 1
for severitylevel as 2,
ComplaintId|EmployeeId|ComplaintSeverity
----------------------------
4 | 1 | 2
6 | 2 | 2
EmployeeId 1 has been complained by 2 user with severitylevel 0,2 but his highest severity level is 2. so while searching for 0 level, 1 should not be displayed.
Can anyone help me?
The question was edited after the previous answer was submitted. The following would therefore be more accurate.
SELECT x.*
FROM my_table x
JOIN
( SELECT employeeid
, MAX(complaintseverity) severity
FROM my_table
GROUP
BY employeeid
) y
ON y.employeeid = x.employeeid
AND y.severity = x.complaintseverity
WHERE complaintseverity = 0 -- optional line
ORDER
BY employeeid;
You can try following query.
SELECT *
FROM
(
SELECT cs.`EmployeeId`, MAX(cs.`ComplaintSeverity`) severity
FROM ComplaintSeverity cs
GROUP BY cs.`EmployeeId`
) csdata
WHERE csdata.severity=1
Replace 1 with the severity level you want.
My question is similar to this SO post however the difference is that I do not have a sequential ID column.
I have a table in the following form
ID | length
0 | 5
0 | 7
0 | 10
1 | 3
1 | 8
1 | 12
2 | 1
2 | 2
2 | 4
2 | 5
and I want to get the difference between successive rows in the length column grouped by the ID. So it should give
ID | length | difference
0 | 5 | NULL
0 | 7 | 2
0 | 10 | 3
1 | 3 | NULL
1 | 8 | 5
1 | 12 | 4
2 | 1 | NULL
2 | 2 | 1
2 | 4 | 2
2 | 5 | 1
I'm not sure how to go about doing this. I tried giving each ID a separate ID that is sequential, but it turned out to be way to complicated and I could not get it to work. Can someone suggest a better way of doing it?
Assuming (id,length) is UNIQUE...
SELECT x.*
, x.length - MAX(y.length) diff
FROM my_table x
LEFT
JOIN my_table y
ON y.id = x.id
AND y.length < x.length
GROUP
BY x.id
, x.length;
select a.id, a.length, b.length, b.length - a.length as difference
from mytable a, mytable b
where a.id=b.id
and b.length = (select min(length) from mytable where id=a.id and length > a.length)
I have a table (innoDB) that has 3 columns: ID, ID_FATHER, ROWPOS. ID is auto_increment and ROWPOS has values from other table. I need ID_FATHER to be incremented by 1 if ROWPOS is not a sequence, if it is a sequence ID_FATHER should not increment.
Like this:
ID | ID_FATHER | ROWPOS
1 | 1 | 250
2 | 2 | 253
3 | 2 | 254
4 | 3 | 260
5 | 4 | 263
6 | 5 | 268
7 | 6 | 270
8 | 6 | 271
9 | 6 | 272
10 | 7 | 276
Is there a way to do that?
With this query:
INSERT INTO mytable (i, rowpos)
SELECT #i := IF(t.rowpos = #prev_rowpos + 1, #i, #i + 1) AS i
, #prev_rowpos := t.rowpos AS rowpos
FROM temp
JOIN (SELECT #prev_rowpos := NULL, #i := 0) v
ORDER BY t.rowpos
I am able to import into the tables I want. But the problem is in the TABLE.Service, as you can see with this solution the ID_FATHER is wrong because it only increments by 1
but in this case it actually should be 2 because invoice 1 doesn't have service.
How can I solve this problem without changing all my schema.
TABLE.temp
ROW|TYPE |INVOICE_temp
1 |xxx |10
2 |xxP |led tv
3 |xxP |mp3 Player
4 |xxx |11
5 |xxP |tv cable
6 |xxS |install
xxx = Invoice number
xxP = Product
xxs = service
TABLE.Invoice_Number TABLE.Product
ID|ID_FATHER|ROWPOS|NUM ID|ID_FATHER|ROWPOS|PROD
1 | 1 | 1 | 10 1 | 1 | 2 | led tv
2 | 2 | 4 | 11 2 | 1 | 3 | mp3 player
3 | 2 | 5 | tv cable
TABLE.Service
ID|ID_FATHER|ROWPOS|SERV
1 | 1 | 6 | install
I made some changes in the query to work as I needed.
You could do something like this:
INSERT INTO mytable (i, rowpos)
SELECT #i := IF(t.rowpos = #prev_rowpos + 1, #i, #i + 1) AS i
, #prev_rowpos := t.rowpos AS rowpos
FROM another_table t
JOIN (SELECT #prev_rowpos := NULL, #i := 0) v
ORDER BY t.rowpos
(Test just the SELECT query, get that working returning the resultset you want, before you preface it with the INSERT.)
For completeness, I will add that this technique is dependent on UNDOCUMENTED and non-guaranteed behavior in MysQL, using "user variables". I've successfully used this approach many times, but for "one off" type admin functions, not ever embedded as SQL in an application.
Note that the ORDER of the expressions in the SELECT list is important, they are evaluated in the order they appear in the SELECT list. (MySQL doesn't guarantee this behavior, but we do observe it. It's important that the check of the user variables containing values from the previous row to precede the assignment of the current row values to the user variables. That's why i is returned first, followed by rowpos. If you reversed the order of those in the SELECT list, the query would operate differently, and we wouldn't get the same results.
The purpose of the inline view (aliased as v) is to initialize the user variables. Since MySQL materializes that view query into a "derived table" before the outer query runs, those variables get initialized before they are referenced in the outer query. We don't really care what the inline view query actually returns, except that we need it to return exactly one row (because we reference it in a JOIN operation to the table we really want to query).
E.g.:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,rowpos INT NOT NULL
);
INSERT INTO my_table (rowpos) VALUES
(250),
(253),
(254),
(260),
(263),
(268),
(270),
(271),
(272),
(276);
SELECT x.*
, #i:=#i+ISNULL(y.id) i
FROM my_table x
LEFT
JOIN my_table y
ON y.id < x.id
AND y.rowpos = x.rowpos - 1
, (SELECT #i:=0) vals
ORDER
BY x.id;
+----+--------+------+
| id | rowpos | i |
+----+--------+------+
| 1 | 250 | 1 |
| 2 | 253 | 2 |
| 3 | 254 | 2 |
| 4 | 260 | 3 |
| 5 | 263 | 4 |
| 6 | 268 | 5 |
| 7 | 270 | 6 |
| 8 | 271 | 6 |
| 9 | 272 | 6 |
| 10 | 276 | 7 |
+----+--------+------+
Its very hard for to set a proper title, because I dont know how I describe my problem.
I have a table like this:
dlID | dl_seID | dlEpisode | dlFlag
___________________________________
1 | 1 | 1 | 0
2 | 1 | 2 | 1
3 | 1 | 3 | 1
4 | 2 | 1 | 1
5 | 2 | 2 | 0
6 | 3 | 1 | 0
What i want is a select query where I get something like this:
dlID | dl_seID | dlEpisode | dlFlag | dlFlagCount
_________________________________________________
1 | 1 | 1 | 0 | 2
2 | 1 | 2 | 1 | 2
3 | 1 | 3 | 1 | 2
4 | 2 | 1 | 1 | 1
5 | 2 | 2 | 0 | 1
6 | 3 | 1 | 0 | 0
dlFlagCount shoud be a counter of dlFlag = 1 where dl_seID = dl_seID.
Second try:
I need a value where I see how many Flags have the value 1 with the same dl_seID.
Is that possible?
I hope you guys know what I want^^
Regards
Try this:
select
a.*,
ifnull(b.ctflags,0)
from
tablea a left join
( select dl_seID, count(dlFlag) ctflags
from tablea
where dlFlag=1
group by dl_seID ) b on (a.dl_seID = b.dl_seID)
The left join is just to get the registry with 0 flags
See the fiddle: http://sqlfiddle.com/#!2/ef9b0/5
EDIT:
As op requested some explanation, here it goes:
What you asked is to count the amount of flags by the dl_seID and to do that you need to do this you separeta your problems, first you get the count for the dl_seID by flags, this is this subquery:
select dl_seID, count(dlFlag) ctflags
from tablea
where dlFlag=1
group by dl_seID
This became a 'separe table' or a new group of data, whatever you wanna call it. Then you have to join this with your original data (from your table) like the query for answer.
The left join part is because maybe there are some data that wont complain with where dlFlag=1 therefore if you want to get then as 0 you have to bring all values from table that exists or not on our created subgroup. And this ifnull(b.ctflags,0) is for theese data data exists on your table but has no flags (for your problem). If you use just b.ctflags it will bring null.
SELECT x.*
, COALESCE(y.flagcount,0) flagcount
FROM my_table x
LEFT
JOIN
( SELECT seID
, COUNT(*) flagcount
FROM my_table
WHERE flag = 1
GROUP
BY seid
) y
ON y.seid = x.seid;