Combine data of two columns in power bi and combine set for new table - datatables

I have two tables:
Table #1 - recipe:
Table #2 - cakes:
What I want in PowerBI is:
select material (done)
show table with all the recipes versions
show another table with all the cakes baked based on recipe (and previous versions)
So far so good, I've create a custom table with material, cakematerial and recipematerial.
Now I also would like to have another report/table showing orders with the same lot and order-prefix.
For example: Ordernr 2000-010 and ordernr 2000-020 have the same LOT (lt011c). I want these to be shown in one table and then another table with the recipes (and history) belonging to both orders.
So in this case: Recipenr L10-BACK01 and L11-BACK01 with complete version history.
Is that possible?
Datamodel in Power BI:

The answer was to change the relationships to both directions between the 3 tables. This would allow you to filter from any table related to Material

Related

sql many to many relationship table design

I am learning sql now and practicing the scenarios to design the tables. I have a one scenario where I could not find proper suitable table structure.
The scenarios is as follows, I want to store depedencies user journey in sql. For example, in customer creation journey, we need to create valid sector, language and country codes in the system. Another example, to create a new account (bank account), we need to create the sector, language and country followed by customer.
So far, I could think of following table design, but I am sure this is not good as there is no primary key and not following the normalization standards.
journey
dependent
order
CUSTOMER
SECTOR
0
CUSTOMER
LANGUAGE
1
CUSTOMER
COUNTRY
2
ACCOUNT
CUSTOMER
0
I understand that this is many to many relationship as one journey can have many dependent and one dependent can be associated with multiple journeys. I need help to efficiently design the tables in sql, please can anyone help on this.
You need from the intermediate/join table that should look like this -
Table name - journey_dependent
Coll(Jurney_FK) Coll(Dependent_FK)
journey_id dependent_id
You can check more here - https://www.baeldung.com/jpa-many-to-many#1-modeling-a-many-to-many-relationship
If journey and dependent values are PK in origin tables, you have 2 FK. You can create a composite PK on that table with that 2 columns.
Maybe order need to be in dependent table. If not, there is information on that table : order. So this is not a pur relationship table. So you could optionally had a technical PK column (auto increment) on it.

SQL One to Many Relationship in a Dashboard

Not sure if this is the right way to phrase the question but I have a database shown below
ProductA ProgramA
ProductB ProgramB
ProductC ProgramBoth
One issue I'm facing is when I put this into a dashboard and I use the dashboard to filter only ProgramA, I want to see both Product A and Product C. And when I filter ProgramB, I would like to see both Product B and Product C. Technically the user can select two of the programs in the dashboard drop down ("ProgramA + ProgramBoth), but they don't.
Am I pushing the limits of SQL? Is there a way around? As a note, I'm importing this from a Google Sheet, so I can change the underlying values if that's easier. In Google Sheets, I have a dropdown so only one value can be put in at a time (can be changed).
You are not pushing the limits of SQL :-)
What you are describing is a many-to-many relationship, and instead of thinking about a relationship of "both", think about it as a row for each relationship so for product C you will have 2 rows - one for program A and one for program B, something along the lines of:
CREATE TABLE Product_Programs
(
Product VARCHAR(10) NOT NULL REFERENCES Products(Product),
Program VARCHAR(10) NOT NULL REFERENCES Programs(Program),
PRIMARY KEY (Product, Program)
);
INSERT INTO Product_Programs (Product, Program)
VALUES ('ProductA', 'ProgramA'),
('ProductB', 'ProgramB'),
('ProductC', 'ProgramA'),
('ProductC', 'ProgramB');
Now you can easily query for any product participating in a program with
SELECT Product
FROM Product_Programs
WHERE Program = 'ProgramA';
Which will return both product A and product C.
HTH

multiple Access queries using same criteria

New to this...
I have a table QAQC_Studies that includes titles, dates, and subject matter
I have another table QAQC_Publications that includes citation information for multiple publications resulting from a single study in the first table.
Every 3 months I need to create a report to QC studies added by coworkers so I run the following query (with some additional attributes removed for brevity). The where clause is a list of study IDs they provide me (often 15-20 different studies).
SELECT QAQC_Studies.StudiesID,
QAQC_Studies.NSL,
QAQC_Studies.StudyTitle,
QAQC_Studies.Abstract,
QAQC_Studies.StudyStatus
FROM QAQC_Studies
WHERE [QAQC_Studies].[StudiesID]=26806 or 26845
I'd like to add to that report a list of the publications associated with each study.
How do I write the Where clause in the second query to reference those studies indicated in the first query?
You can use a subquery. Something like:
SELECT [QAQC_Publications].[QAQC_Field]
FROM [QAQC_Publications]
WHERE [QAQC_Publications].[StudiesID] --or whichever field the two tables
--share for publication/study connection
IN (SELECT QAQC_Studies.StudiesID
FROM QAQC_Studies
WHERE [QAQC_Studies].[StudiesID]=26806 or 26845)

Normalize a table with tightly coupled data

I currently have a table that stores the materials used to create an item
Item (material1, material2, material3)
Another material has recently been added. However, I want to normalize this table so that in the future, adding a new material won't need a new column to be added to the database. I extracted the materials into the following junction table
ItemJuncMaterial (id, itemId, materialid)
However, the catch is that the order of the materials matter. So this junction table won't allow me to run this query based on materials to get the item
select itemid from ItemJunMaterial where materialid in (1,2,3)
This can return items that use 2,3,1 or could even use two materials 1,2. Is there a better way to split up this table to normalize it and make it more dynamic?
You should consider a "bill of materials" (BOM) pattern (see here or here). The intersection of a BOM can include extra information about the composition, such as quantity of each component. You could easily include the sequence or priority of the component in that intersection just as easily.

Terminology am I doing a one to many or many to many? Left join? Right?

I need to research this but am confused about the terminology of what I should be researching.
Table1 fields:
salenumber
category
quantity
price
Table2 fields:
field
category
requirement
Table3 fields:
field
salenumber
value
I need to combine these.
Essentially one (salenumber, category, quantity, Price) can have a dynamic number of "fields" containing unique "data associated with it. I'm a little confused as to the terminology of what I am doing here. I'm all mixed up with left and right joins and many to one and many to many databases. If I simply knew the term for what I am trying to do it would help me to narrow down my research.
Joins are for your queries - which you run to find specific records from your database. You aren't there yet. First, you want to do table design, which is where you consider many-to-many and one-to-many relationships.
Start by thinking about what the tables represent. For example, a single sale can involve multiple different products (e.g. you buy a fork, a spoon, a knife, and a new car). Each product can be in a different category (utensils or motor vehicles, in this case). In your table design, you would decide whether a product belongs to only a single category or to multiple categories.
Let's assume there's just one category per product - then you can have many products in one category (fork, spoon and knife are all utensils), but a product can have only one category. In this case, Category to Product is a One-to-Many relationship.
How these connect is where the related fields in tables come in to play - so in the Product table you have 'Category', which refers to the Category table ('Fork' is of Category 'Utensil', and in the Category table 'Utensil' is an entry with additional information).
You probably want to look up some basic database lessons to help you out. There are some good free online classes and resources - just search for info about databases.
This may help understand joins: http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
If you have a row in Table_A with some id, and multiple rows in Table_B related referencing that id you can do a LEFT OUTER JOIN (often just LEFT JOIN). This will match the data in Table_A to Table_B - but you will have as many rows as there were in Table_B!
If you want all of the Table_B data related to the Table_A row in one row result you need an aggregate function. In SQL this is normally something like array_agg. When you do this you need to tell it what to GROUP BY, in my example it would be Table_A.id