I have one slowly changing dimension type 2, one fact table and another lookup table that links the two. isCurrent field in the dimension will be updated based on loadDate. I am trying to create a logic to load the second column of the table that links the fact and dimension but not having any luck yet. Here is what I am trying to achieve. The linking table is supposed to hold all IDs in column 1 and the corresponding LinkedID in column 2. And if the isCurrent flag changes, LinkedID column should update to the ID with isCurrent = Y. btw: ID is not employeeId, it is just a reference number that ties the dimension with the fact.
Here is an example of what the Link table should look like:
Record 1:
Dimension Table
ID Name State isCurrent
101 Alex TX Y
Link table:
ID LinkedID
101 101
Record 2:
Dimension table:
ID Name State isCurrent
101 Alex TX N
102 Alex CA Y
Link table
ID LinkedID
101 102
102 102
Related
id salary address
101 $400 NY
101 $200 NY
102 $102 TX
102 $127 TX
102 $391 TX
Now how to assign key for every repeating row based on id column
id salary address key
101 $400 NY 1
101 $200 NY 2
102 $102 TX 1
102 $127 TX 2
102 $391 TX 3
If I understood correctly, you want to get an ID based on the id occurrences, so you can define both as a unique key, right?
First, you can use SQL COUNT to find the number of times that an id appears on the table, then increment the result to get your id. Example in pseudocode:
sqlresult = sqlquery( COUNT(*) FROM `your_table` WHERE id = "wanted_id";);
key = sqlresult++;
Then you can define a composite key by following this post:
How can I define a composite primary key in SQL?
Sorry for not being more detailed, but right now I don't have much time, if you need I can help you better later on today.
Hope I could help.
I'm new to MySQL procedures and triggers and constraints and I'm wondering how I can set one of my columns to be dependent on another column. For example, I have a table of products and weekly sales similar to:
Product_ID Sales Ranking
1 13
2 12
3 543
4 435
5 97
I want my ranking column to be automatically updated whenever a value in the sales is updated or a new value is inserted (this table will only hold at most 50 products), which means product IDs will come and go. How can I set it so that the Ranking is always an x value such that x represents what position it would be it the product id was ordered by sales DESC?
Product ID is NOT an AI column or Primary Index
Sorry for not many clear title.
1st table "place":
ID PLACE AREA_1 AREA_2 AREA_3
1 Mago 23 45 67
2 Gelato 23 45 12
And so on
2nd table "area_1"
ID ID_AREA_1 AREA_1
1 23 Lazio
3rd table "area_2"
ID ID_AREA_2 AREA_2
1 45 Roma
4th table "area_3"
ID ID_AREA_3 AREA_3
1 12 Roma
2 67 Velletri
Of course with 3 INNER JOIN I can extract in this mode
ID PLACE AREA_1_NAME AREA_2_NAME AREA_3_NAME
1 Mago Lazio Roma Velletri
But I need to re-factory the database, losing 2nd, 3rd and 4th table and I need to obtain a new, unique table directly with
ID PLACE AREA_1_NAME AREA_2_NAME AREA_3_NAME
1 Mago Lazio Roma Velletri
1) I need absolutely to mantain inalterate the ID (id of place) of the 1st table (are used in others tables that I will mantain)
2) I don't need anymore the 2nd, 3rd and 4th tables
Could you help me to create the SQL statement for MySQL to prattically move the result of the JOINS to a new table?
Thank you very, very much!
table A
id name group_id email
1 a 1 a#g.com
2 b 3,4 b#g.com
3 c 1,3,4 c#g.com
4 d 2,5,1 d#g.com
table b
id user user_group_id
1 x 1,3
The table structure is as above
OUTPUT: if i search for user_group_id (from table B) for 1,3 in table A then i should get 4 email addresses i.e a#g.com,b#g.com,c#g.com,d#g.com. Since 1 is present in 3 rows in table A and 3 is present in 2 rows.
While I don't exactly understand what your problem is, I have the impression that your table structure is not properly normalized.
Your table b should have two entries:
id user user_group_id
1 x 1
2 x 3
In this case, you can properly join your tables and get all answers when querying for a certain user name.
There are two important fields kernel and property in the table mydata:
I want to CREATE a new table myresults with columns rightTwoKernelSymbols, property and frequency based only on mydata table so, that it displays the frequency of right two letters among kernel column. (property values should be equivalent, otherwise count separately)
So, using another words:
Here is 'mydata' table. I modify 'kernel' column by leaving only two right symbols in each cell.
Using this table, I want to count distinct pairs (kernel, property) and save the data into myresults table.
Example:
mydata Table
czzzaa - 123
abc80 - 123
aaaaaaaa - 123
zz5 - 123
abc80 - 456
modified Table
aa - 123
80 - 123
aa - 123
z5 - 123
80 - 456
myresults Table
aa - 123 - 2 // czzzaa and aaaaaaaa - total two times (123 is the same)
80 - 123 - 1
z5 - 123 - 1
80 - 456 - 1 //we don't count abc80 and abc80 together, because 123 is different from 456
OK, here is my answer on my own question. Please let me know if there are any pitfalls that weren't mentioned.
CREATE TABLE results AS SELECT right(kernel, 2) as two , property, COUNT(*) as count FROM mydata GROUP BY two, property ORDER BY count DESC;