How to to insert data to a bridge table simultaneously with other connecting tables - c++

Hi all how I have a product, order and product order(bridge entity) table.
Im building a cart based order system c++ program where when user make a new order ,it will have product id listed on a list and every product will have its own orderID. Im kinda confused on the sql side on how to insert data that have brige entity and join them together
The table have the following attrbs
Table product
ProductID
Table Order1
OrderID,
Totalprice,
description,
agentid
Table product_order(bridge entity)
ProductID,
OrderID,
quantity
I'm planning to insert data only 1 time simultaneously inserting into the other table as well . Its possible to use join + insert?

Related

Transaction Management with Raw SQL and Models in a single transaction Django 1.11.49

I have an API which reads from two main tables Table A and Table B.
Table A has a column which acts as foreign key to Table B entries.
Now inside api flow, I have a method which runs below logic.
Raw SQL -> Joining table A with some other tables and fetching entries which has an active status in Table A.
From result of previous query we take the values from Table A column and fetch related rows from Table B using Django Models.
It is like
query = "Select * from A where status = 1" #Very simplified query just for example
cursor = db.connection.cursor()
cursor.execute(query)
results = cursor.fetchAll()
list_of_values = get_values_for_table_B(results)
b_records = list(B.objects.filter(values__in=list_of_values))
Now there is a background process which will enter or update new data in Table A and Table B. That process is doing everything using models and utilizing
with transaction.atomic():
do_update_entries()
However, the update is not just updating old row. It is like deleting old row and deleting related rows in Table B and then new rows are added to both tables.
Now the problem is if I run api and background job separately then everything is good, but when both are ran simultaneously then for many api calls the second query of Table B fails to get any data because the transaction executed in below manner:
Table A RAW Transaction executes and read old data
Background Job runs in a single txn and delete old data and enter new data. Having different foreign key values that relates it to Table B.
Table B Models read query executes which refers to values already deleted by previous txn, hence no records
So, for reading everything in a single txn I have tried below options
with transaction.atomic():
# Raw SQL for Table A
# Models query for Table B
This didn't worked and I am still getting same issue.
I tried another way around
transaction.set_autocommit(False)
Raw SQl for Table A
Models query for Table B
transaction.commit()
transaction.set_autocommit(True)
But this didn't work either. How can I read both queries in a single transaction so background job updates should not affect this read process.

Netsuite suiteql how to get all available tables to query?

I am using Postman and Netsuite's SuiteQL to query some tables. I would like to write two queries. One is to return all items (fulfillment items) for a given sales order. Two is to return all sales orders that contain a given item. I am not sure what tables to use.
The sales order I can return from something like this.
"q": "SELECT * FROM transaction WHERE Type = 'SalesOrd' and id = '12345'"
The item I can get from this.
"q": "SELECT * FROM item WHERE id = 1122"
I can join transactions and transactionline for the sale order, but no items.
"q": "SELECT * from transactionline tl join transaction t on tl.transaction = t.id where t.id in ('12345')"
The best reference I have found is the Analytics Browser, https://system.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2021_1/analytics/record/transaction.html, but it does not show relationships like an ERD diagram.
What tables do I need to join to say, given this item id 1122, return me all sales orders (transactions) that have this item?
You are looking for TransactionLine.item. That will allow you to query transaction lines whose item is whatever internal id you specify.
{
"q": "SELECT Transaction.ID FROM Transaction INNER JOIN TransactionLine ON TransactionLine.Transaction = Transaction.ID WHERE type = 'SalesOrd' AND TransactionLine.item = 1122"
}
If you are serious about getting all available tables to query take a look at the metadata catalog. It's not technically meant to be used for learning SuiteQL (supposed to make the normal API Calls easier to navigate), but I've found the catalog endpoints are the same as the SuiteQL tables for the most part.
https://{{YOUR_ACCOUNT_ID}}.suitetalk.api.netsuite.com/services/rest/record/v1/metadata-catalog/
Headers:
Accept application/schema+json
You can review all the available records, fields and joins in the Record Catalog page (Customization > Record Catalog).

Power Query M formula language foreign key checking

I am currently using the Power BI Power Query Editor writing queries using the Power Query M formula language. I currently have two tables named employee and business. Each row of the employee table has a business_id that connects to a row of the business table. I want to run a Table.SelectRows function to include only the rows where employee.business_id = business.id.
This is what I have so far:
let
Source = MySQL.Database(<DATABASE>, "business_database", [ReturnSingleDatabase=true, CreateNavigationProperties=false]),
business_database_employee_all = Source{[Schema="business_database",Item="employee"]}[Data],
employee_included = . . . Return all rows from employee where employee.business_id = business.id . . .
in
employee_included
Any sort of help with this one would be appreciated! I'm pretty set on using Table.SelectRows but I'm down to utilize better functions if it's recommended!
It sounds like you want to filter the employee table so that it only shows rows where there is a match in the business_id field to the business table
the best way is to merge the business table into the employee table, matching the business_id field, with inner join
#"Merged Queries" = Table.NestedJoin(#"PriorStepNameinEmployeeTable",{"business_id"},business,{"business_id"},"business",JoinKind.Inner)
another way
#"Select" = Table.SelectRows(#"PriorStepNameinEmployeeTable", each Table.Contains(business_table,_,{"business_id"}))

PowerBI DAX function to count number of occurrences using DISTINCT (Countif)

I am trying to create a column in PowerBI that counts how times a customer name occurs in a list.
I may be going in the wrong direction, but what I have so far is;
My SQL query returns a table of customer site visits (Query1), from which I have created a new table (Unique) and column (Customer) that lists the distinct names (cust_company_descr) from Query1;
Unique = DISTINCT(Query1[cust_company_descr])
What I need is a new column in the Unique table (Count) that will give me a count of how many times each customer name in the Query1 table appears.
If I were working in Excel, the solution would be to generate a list of unique values and do a COUNTIF, but I can't find a way to replicate this.
I have seen a few solutions that involve this sort of thing;
CountValues =
CALCULATE ( COUNTROWS ( TableName ); TableName[ColumnName] = " This Value " )
The issue I have with this is the Unique table contains over 300 unique entries, so I can't make this work.
Example (The Count column is what I'm trying to create)
Query 1
cust_company_descr
Company A
Company B
Company A
Company C
Company B
Company A
Unique
Company_____Count
Company A_____3
Company B_____2
Company C_____1
Any help is gratefully received.

SyncFramework: How to sync all columns from a table?

I create a program to sync tables between 2 databases.
I use this common code:
DbSyncScopeDescription myScope = new DbSyncScopeDescription("myscope");
DbSyncTableDescription tblDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Table", onPremiseConn);
myScope.Tables.Add(tblDesc);
My program creates the tracking table only with Primary Key (id column).
The sync is ok to delete and insert rows.
But updating don't. I need update all the columns and they are not updated (For example: a telephone column).
I read that I need to add the columns I want to sync MANUALLY with this code:
Collection<string> includeColumns = new Collection<string>();
includeColumns.Add("telephone");
...
includeColumns.Add(Last column);
And changing the table descripcion in this way:
DbSyncTableDescription tblDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Table", includeColumns, onPremiseConn);
Is there a way to add all the columns of the table automatically?
Something like:
Collection<string> includeColumns = GetAllColums("Table");
Thanks,
SqlSyncDescriptionBuilder.GetDescriptionForTable("Table", onPremiseConn) will include all the columns of the table already.
the tracking tables only stores the PK and filter columns and some Sync Fx specific columns.
the tracking is at row level, not column level.
during sync, the tracking table and its base table are joined to get the row to be synched.