How can I distinguish the independent variable and dependent variable for predict? - data-analysis

I am doing a task for data analysis that user will have a hearing test,then the algorithm will generate a EQ setting(earphone) like:
| id | sb1 | sb2 | sb3 | act |
|--- | --- | --- | --- | ---- |
| 1 | row | row | row | test |
| 1 | row | row | row | modify |
| 1 | row | row | row | modify |
| 1 | row | row | row | test |
| 1 | row | row | row | modify |
| 1 | row | row | row | test |
| 2 | row | row | row | test |
| 2 | row | row | row | test |
| 2 | row | row | row | modify |
| 2 | row | row | row | modify |
sb1~3 are the EQsetting,and act is user's action
{'test':"hearing test",'modify':"edit the EQsetting from the origin EQsetting "},
and I want to make the test EQsetting and modify EQsetting have less loss,
but the data have many times with test and modify for an user,
how can I distinguish the independent variable and dependent variable?

Related

Oracle 12 - How to identify duplicates with increment?

I want to differentiate duplicate values in my query by adding an increment or a count of some sort.
The idea is to concatenate my two columns to create a new unique reference.
I tried this :
SELECT
value,
COUNT(*) OVER (PARTITION BY value) value_incr
FROM table
and got the following result :
| value | value_incr |
| --- | --- |
| a | 1 |
| b | 3 |
| b | 3 |
| b | 3 |
| c | 2 |
| c | 2 |
| d | 1 |
But what I would like to get is :
| value | value_incr |
| --- | --- |
| a | 1 |
| b | 1 |
| b | 2 |
| b | 3 |
| c | 1 |
| c | 2 |
| d | 1 |
Is there a way to differentiate my duplicates in Oracle 12 ?
My best solution for now is to add a ROWNUM column, but it's not really satisfying.
Thank you

MySql compare row values and pick one if the condition statisfied

I am trying to write a sql logic which will compare rows for a user and based on a condition it will pick one.
For eg.
+-------+------+--+
| UseId | Code | |
+-------+------+--+
| 1 | A | |
| 2 | B | |
| 3 | C | |
| 4 | D | |
| 4 | E | |
| 5 | F | |
| 5 | G | |
+-------+------+--+
The output im trying to get is, if a user has 2 records compare the code like if code =D and code=E for user 4, then retain row with code E
for user 5 , if Code=F and Code=G then retain row with code F
so the output should look like
+-------+------+--+
| UseId | Code | |
+-------+------+--+
| 1 | A | |
| 2 | B | |
| 3 | C | |
| 4 | E | |
| 5 | F | |
| | | |
+-------+------+--+

How to separate multiple data in one row to multiple rows in SQL

I have following tables in my application:
Table-1
--------------------------------------
| id | column_2(array_type) |
--------------------------------------
| 1 | [20,21,22] |
--------------------------------------
Table-2
---------------------------------------
| id | column_2(array_type) |
---------------------------------------
| 3 | [31,32] |
---------------------------------------
Now I want to create a view as follows:
-----------------------------------------------------------------
| id | Table_1_id | Table_2_id | column_2 |
-----------------------------------------------------------------
| 1 | 1 | nil | 20 |
-----------------------------------------------------------------
| 2 | 1 | nil | 21 |
-----------------------------------------------------------------
| 3 | 1 | nil | 22 |
-----------------------------------------------------------------
| 4 | nil | 3 | 31 |
-----------------------------------------------------------------
| 5 | nil | 3 | 32 |
-----------------------------------------------------------------
The view should have single row entry for every item in column_2'.
Am unable to split the column_2's array content to individual row and assign corresponding table's id to it.
Any help or reference would be very helpful. I do not have much experience with SQL.
Couldn't find much on this in google.

add foreign key with on update cascade

Let's say, I have 2 tables
user
+----+--------+
| id | status |
+----+--------+
| 1 | A |
| 2 | A |
+----+--------+
article
+----+-----+--------+
| id | uid | status |
+----+-----+--------+
| 1 | 1 | A |
| 2 | 2 | A |
| 3 | 2 | A |
| 4 | 2 | A |
| 5 | 1 | A |
| 6 | 2 | A |
| 7 | 2 | A |
| 8 | 1 | A |
| 9 | 2 | A |
| 10 | 2 | A |
+----+-----+--------+
How can I add a foreign key that if I run this query:
UPDATE user SET status='B' WHERE id=1 OR id=2;
the result will be:
user
+----+--------+
| id | status |
+----+--------+
| 1 | B |
| 2 | B |
+----+--------+
article
+----+-----+--------+
| id | uid | status |
+----+-----+--------+
| 1 | 1 | B |
| 2 | 2 | B |
| 3 | 2 | B |
| 4 | 2 | B |
| 5 | 1 | B |
| 6 | 2 | B |
| 7 | 2 | B |
| 8 | 1 | B |
| 9 | 2 | B |
| 10 | 2 | B |
+----+-----+--------+
Or in other words, if I update column user.status, MySQL will automatically update column article.status with the respective value.
How can I create this foreign key?
That job is not foreign key could finish. Use an update trigger, but for better Database compatibility, do this action on you code is preferred.
Trigger code:
CREATE TRIGGER SetArticleStatus AFTER UPDATE ON user
FOR EACH ROW
BEGIN
UPDATE `article`
SET status = NEW.status
WHERE uid = NEW.id
END
Since your FOREIGN KEY is only uid and doesn't know about status the thing you want can't be accomplished using only foreign-keys. This looks like a good use-case for a on-update-trigger.

how to alter a mysql table to add a column which contains a common value for all rows by checking another columns repeating value

how to alter a mysql table to add a column which contains a common value for all rows by checking another columns repeating value.
+----+----------+--------------+
| id | inode | name |
+----+----------+--------------+
| 1 | 12059010 | IwnsuAaJUFaa |
| 2 | 12059015 | IwnsuAaJUFab |
| 3 | 12059016 | IwnsuAaJUFac |
| 4 | 12059017 | IwnsuAaJUFad |
| 5 | 12059018 | IwnsuAaJUFae |
| 6 | 12059019 | IwnsuAaJUFaf |
| 7 | 12059020 | IwnsuAaJUFag |
| 8 | 12059021 | IwnsuAaJUFah |
| 9 | 12059022 | IwnsuAaJUFai |
| 10 | 12059023 | IwnsuAaJUFaj |
| 11 | 12059013 | iPhZIWtZdSaa |
| 12 | 12059015 | iPhZIWtZdSab |
| 13 | 12059016 | iPhZIWtZdSac |
| 14 | 12059017 | iPhZIWtZdSad |
| 15 | 12059018 | iPhZIWtZdSae |
| 16 | 12059019 | iPhZIWtZdSaf |
| 17 | 12059020 | iPhZIWtZdSag |
| 18 | 12059021 | iPhZIWtZdSah |
| 19 | 12059022 | iPhZIWtZdSai |
| 20 | 12059023 | iPhZIWtZdSaj |
+----+----------+--------------+
i need a fourth column having a common number for all rows group by same "inode" number.
Add the column using something like
ALTER TABLE [tablename] ADD [columname] [columntype]
Change the content of that column using UPDATE.