How to dynamically add new records from one table into another in PowerPivot? - relational-database

I have two tables with a Many to One Cardinality between table1 and table2 using [CustCode]
Table1
+-----+--------+----------+--------+
|Order|CustCode| CustName | Sales$ |
+-----+--------+----------+--------+
| 1 | 1A | Jack | 5,000 |
| 2 | 2A | Jill | 20,000 |
| 3 | 3A | Bill | 3,000 |
| 4 | 3B | Bill | 2,700 |
+-----+--------+----------+--------+
Table2
+--------+--------+------------+
|CustCode|CustName| Type| Type2|
+--------+--------+-----+------+
| 1A | Jack | OEM | 1 |
| 2A | Jill | DIST| 1 |
| 3A | Bill | DIST| 1 |
+--------+--------+-----+------+
Table1 is brought in using PowerQuery from a source which updates daily. Table 2 is a table in the existing workbook. Both are pulled into a "Data Model" in the same workbook using PowerPivot.
How can I dynamically take new records that appear in table1 (i.e., Order 4) with its new CustCode and place it into Table2 and place the same values for the Type and Type 2 fields because it's the same Customer (Bill) and his Type is a DIST and his Type2 is 1?

Related

Mysql lookup table (Mysql performance issue)

I've made a look up table for all my tables. What I'm trying to do is what it is supposed to do, correspond the ID's to the matched values.
I use mysql work bench, server 5.7(newest version doesn't work for me).
I have thousands of values and 6 tables. For a simple explanation lets say I have 3 tables.
------------------table1------------------
| t1ID | Person | Purchase| Code |
| 1 | Jon | 50 | 111 | /* Code = t3ID */
| 2 | Dan | 100 | 222 | /* Purchase = Buyer in table2 */
| 3 | Pete | 200 | 333 |
(Many more)
------------------table2------------------
| t2ID | Buyer | Date | Barcode |
| 1 | 200 | 1/1/20 | ABC111 | /* Buyer = Purchase in table1 */
| 2 | 100 | 2/1/20 | ABC222 | /* Barcode = Item_ID in table3 */
| 3 | 50 | 3/1/20 | ABC333 |
(Many more)
------------------table3---------
| t3ID | Item | Item_ID | /* t3ID = Code*/
| 111 | Laser | ABC111 | /* Item_ID = Barcode */
| 222 | Phones | ABC222 |
| 333 | Tables | ABC333 |
(Many more)
I get this... I need this...
-------------Lookup_Table------------- -------------Lookup_Table----------
| lookID | t1ID | t2ID | t3ID | | lookID | t1ID | t2ID | t3ID |
| 1 | 1 | 1 | 111 | | 1 | 1 | 3 | 333 |
| 2 | 2 | 2 | 222 | | 2 | 2 | 2 | 222 |
| 3 | 3 | 3 | 333 | | 3 | 3 | 1 | 111 |
This table is connected by foreign keys and these values were added manually here because the original tables came from CSV files.
My problem is performance or maybe I'm doing it wrong on mysql. When I query by the table ID its works perfectly fine but the values will not match since I added the tableID's afterwards, there are repeated values in certain fields and all the values on the tables are random and they are only connected by those specific values shown above.
When I query select or any other one like "on table1.Purchase = table2.Buyer" or "where", to make the comparison and add them to the table properly, the localhost server crashes. It loses connection to mysql server. Also, if its directly compared between the tables, it works but takes over 5 minutes to do that comparison.
If I limit the rows between 0-10000 is fine, above that or if I don't limit it just crashes. I can't limit since I have over 20000 rows, for now.
example is
update Lookup_Table lk inner join table1 t1 on t1.t1ID = lk.t1ID
inner join table2 t2 on t2.Buyer = t1.Purchase
set lk.t2ID = t2.t2ID;
Same thing between the other tables. Any idea if I'm doing it wrong or if there's another way of doing this? I've tried so many different things and no luck.

MYSQL Insert into table's column based on column name

For some reason I am having difficulty wording this question but I will try my best. I've been searching for 2 days on and off now and haven't found a good solution to the issue.
I have a Table called InventoryNode;
_________________________________________________
| InvID | ID | Slot | ItemID1 | ItemID2 | ItemID3 |
|-------|----|------|---------|---------|---------|
| 1 | 1 | Neck | 10 | 22 | 66 |
| 1 | 2 | Head | 26 | 23 | 56 |
| 1 | 3 | Leg | 19 | 21 | 76 |
And another table called Inventory which stores the Node ID in each column
_____________________________
| ID| Neck | Head | Leg | ... |
|---|------|------|-----|-----|
| 1 | 1 | 2 | 3 | 66 |
If there a way I can insert the node ID's into the Inventory table based off the InvID and populate all the columns with the correct name with the Node's ID?
Something like this?
INSERT INTO Inventory INNER JOIN InventoryNode ON
(Inventory.ID = InventoryNode.InvID) WHERE Inventory.column_name =
InventoryNode.Slot SET InventoryNode.InvID

how to combine or merge two resultsets in mysql?

I have two resultsets A and B
The table A looks like this
+------+---------+------------+
| a_id | item no | total sold |
+------+---------+------------+
| 1 | 101 | 23 |
| 2 | 102 | 34 |
| 4 | 405 | 54 |
| 5 | 506 | 65 |
| 6 | 104 | 23 |
+------+---------+------------+
The table B looks like this
+------+---------+----------+
| b_id | item no | location |
+------+---------+----------+
| 1 | 101 | A1 |
| 2 | 102 | A2 |
| 3 | 103 | A3 |
| 4 | 104 | A4 |
+------+---------+----------+
I want to achieve the output as follows
+------+---------+------------+----------+
| a_id | item no | total sold | location |
+------+---------+------------+----------+
| 1 | 101 | 23 | A1 |
| 2 | 102 | 34 | A2 |
| 4 | 405 | 54 | NULL |
| 5 | 506 | 65 | NULL |
| 6 | 104 | 23 | A4 |
+------+---------+------------+----------+
I want to append the column 'LOCATION' to table A and display the location value for each item no which is present in table B. If the ITEM NO in table A does not have a location value, then the LOCATION value has to be NULL (i.e EMPTY).
Since I am a beginner, I don't know how to achieve it. I tried using UNION but I failed to write a proper query
What you are looking for is a Join. Specifically a Left Outer Join so you get all of the results from your Left table (TableA) and only those results from your right table (TableB) that match.
When specifying a join, you use the ON clause to tell the DB which fields are related between the tables:
SELECT
a_id,
a.itemno,
a.totalsold,
b.location
FROM
tableA
LEFT OUTER JOIN tableB ON
tableA.itemno = tableB.itemno
You should use LEFT JOIN (left cause you want to get nulls when the record is not found in the second table)
SELECT a.a_id, a.itemno, a.totalsold, b.location
FROM tableA AS a LEFT JOIN tableB AS b ON
a.itemno = b.itemno

Conditionally move MySQL data between rows in same table

Working in Redmine, I need to copy(not move) data from certain rows to other rows based on matching project id numbers with time entries.
I have included a diagram of the table "custom_values" and my understanding of the design below(CURRENT DATA):
+----+-----------------+---------------+-----------------+-------+
| id | customized_type | customized_id | custom_field_id | value |
+----+-----------------+---------------+-----------------+-------+
| 1 | Project | 1 | 1 | 01 |
| 2 | TimeEntry | 1 | 4 | 01 |
| 3 | Project | 2 | 1 | 02 |
| 4 | TimeEntry | 2 | 4 | 02 |
| 5 | Project | 3 | 1 | 03 |
| 6 | TimeEntry | 3 | 4 | |
| 7 | Project | 4 | 1 | 04 |
| 8 | TimeEntry | 4 | 4 | |
+----+-----------------+---------------+-----------------+-------+
At the risk of oversimplifying,
"id" = The primary key for each entry in custom_values
"customized_type" = Specifies which db table the row is referring to.
"customized_id" = Specifies the primary key for the db table entry previously specified in "customized_type".
"custom_field_id" = Specifies which custom field the row is referring to. Redmine admins can arbitrarily add and remove custom fields.
"value" = The data contained within the custom field specified by
"custom_field_id"
In my situation, the values listed in "value" are representing unique customer id numbers. The customer id numbers did not always get entered with each time entry. I need to copy the customer numbers from the project rows to the matching time entry rows. Each time entry has a project_id field.
So far, here is my mangled SQL query:
SELECT
custom_field_id,
custom_values.value AS 'CUSTOMER_NUMBER',
custom_values.customized_id AS 'PROJECT_ID_NUMBER',
custom_values.customized_type,
time_entries.comments AS 'TIME_ENTRY_COMMENTS'
FROM
redmine_tweaking.custom_values
LEFT JOIN
redmine_tweaking.time_entries ON custom_values.customized_id = time_entries.project_id
WHERE
custom_values.customized_type='Project' AND custom_values.custom_field_id=1;
The query I have so far allows me to see that I have the time entries connected properly to their matching projects, but that is all I have been able to figure out. So in other words, this SQL statement does not exactly solve my problem.
Plus, even if it did work, I think the way I laid it out looks like 200 lbs of bird poop. There must be a better/more optimized way to do this.
Any help would be greatly appreciated. I am relatively new and I have been pouring hours into solving this problem.
UPDATE:
Ok, here is the time_entries table:
+----+------------+---------+----------+-------+----------+-------------+------------+-------+--------+-------+---------------------+---------------------+
| id | project_id | user_id | issue_id | hours | comments | activity_id | spent_on | tyear | tmonth | tweek | created_on | updated_on |
+----+------------+---------+----------+-------+----------+-------------+------------+-------+--------+-------+---------------------+---------------------+
| 1 | 1 | 1 | 1 | .25 | test | 9 | 2015-11-04 | 2015 | 11 | 45 | 2015-11-04 08:18:12 | 2015-11-04 10:18:12 |
| 2 | 2 | 1 | 1 | .25 | test2 | 9 | 2015-11-04 | 2015 | 11 | 45 | 2015-11-04 09:18:12 | 2015-11-04 12:18:12 |
+----+------------+---------+----------+-------+----------+-------------+------------+-------+--------+-------+---------------------+---------------------+
As opposed to the original table that I first posted, the expected output would show this:
+----+-----------------+---------------+-----------------+-------+
| id | customized_type | customized_id | custom_field_id | value |
+----+-----------------+---------------+-----------------+-------+
| 1 | Project | 1 | 1 | 01 |
| 2 | TimeEntry | 1 | 4 | 01 |
| 3 | Project | 2 | 1 | 02 |
| 4 | TimeEntry | 2 | 4 | 02 |
| 5 | Project | 3 | 1 | 03 |
| 6 | TimeEntry | 3 | 4 | 03 |
| 7 | Project | 4 | 1 | 04 |
| 8 | TimeEntry | 4 | 4 | 04 |
+----+-----------------+---------------+-----------------+-------+

Remove duplicates SQL while ignoring key and selecting max of specified column

I have the following sample data:
| key_id | name | name_id | data_id |
+--------+-------+---------+---------+
| 1 | jim | 23 | 098 |
| 2 | joe | 24 | 098 |
| 3 | john | 25 | 098 |
| 4 | jack | 26 | 098 |
| 5 | jim | 23 | 091 |
| 6 | jim | 23 | 090 |
I have tried this query:
INSERT INTO temp_table
SELECT
DISTINCT #key_id,
name,
name_id,
#data_id FROM table1,
I am trying to dedupe a table by all fields in a row.
My desired output:
| key_id | name | name_id | data_id |
+--------+-------+---------+---------+
| 1 | jim | 23 | 098 |
| 2 | joe | 24 | 098 |
| 3 | john | 25 | 098 |
| 4 | jack | 26 | 098 |
What I'm actually getting:
| key_id | name | name_id | data_id |
+--------+-------+---------+----------+
| 1 | jim | 23 | NULL |
| 2 | joe | 24 | NULL |
| 3 | john | 25 | NULL |
| 4 | jack | 26 | NULL |
I am able to dedupe the table, but I am setting the 'data_Id' value to NULL by attempting to override the field with '#'
Is there anyway to select distinct on all fields and while keeping the value for 'data_id'? I will take the highest or MAX data_id # if possible.
If you only want one row returned for a specific value (in this case, name), one option you have is to group by that value. This seems like a good approach because you also said you wanted the largest data_id for each name, so I would suggest grouping and using the MAX() aggregate function like this:
SELECT name, name_id, MAX(data_id) AS data_id
FROM myTable
GROUP BY name, name_id;
The only thing you should be aware of is the possibility that a name occurs multiple times under different name_ids. If that is possible in your table, you could group by the name_id too, which is what I did.
Since you stated you're not interested in the key_id but only the name, I just excluded it from the query altogether to get this:
| name | name_id | data_id |
+-------+---------+---------+
| jim | 23 | 098 |
| joe | 24 | 098 |
| john | 25 | 098 |
| jack | 26 | 098 |
Here is the SQL Fiddle example.
RENAME TABLE myTable to Old_mytable,
myTable2 to myTable
INSERT INTO myTable
SELECT *
FROM Old_myTable
GROUP BY name, name_id;
This groups my tables by the values I want to dedupe while still keeping structure and ignoring the 'Data_id' column