I have two tables called Components and PDF_MSDS. The Component table contains columns f_chem_name, f_component_id and f_chem_name and it contains below sample data.
Components table
SELECT F_Cas_Number, F_Component_Id, F_Chem_Name
FROM Components
WHERE F_Chem_Name = 'CHMNM_17816'
OUTPUT
F_Cas_Number F_Component_Id F_Chem_Name
-------------------------------------------
CAS_5861 PRD1000826 CHMNM_17816
Sample data of PDF_MSDS table
F_PRODUCT F_CAS_NUMBERS F_COMPONENT_IDS
----------------------------------------------------------------------
360 CAS_5779¿CAS_5861¿CAS_2614¿ 3E000685¿3E002268¿3E004960¿3E005217¿PRD1000826¿
Now I want display the f_product value from the PDF_MSDS table by comparing F_Cas_Number and F_Component_Id in Components table with F_CAS_NUMBERS and F_COMPONENT_IDS in the PDF_MSDS table for the given f_chem_name in Components table.
But I am not able compare directly using join with F_Cas_Number in Components table with F_CAS_NUMBERS in PDF_MSDS table and also F_Component_Id in Components table with F_COMPONENT_IDS in PDF_MSDS because multiple cas_numbers and component_ids are stored in F_CAS_NUMBERS and F_COMPONENT_IDS columns in PDF_MSDS table.
How can I search and compare cas_numbers and component_ids and select particular record.Please help.F_Chem_Name is input parameter for Procedure.
You can use LIKE in your JOIN conditions.
JOIN TableB
ON TableA.Column LIKE '%'+TableB.Column+'%'
Related
I have two tables
table A contain many ids, which one is related to the table B.
I have to make a query on the table A to select only some ids and then take the data from the table B using the selected ids from the table A
How can i do that? (MYSQL)
Based on your description, here is a simple structure of how the query might go, although I recommend you share some sample results & the desired output.
SELECT * FROM tableA
JOIN tableB
ON tableA.id_song == tableB.id_song
WHERE "your condition HERE"
I have two tables that have different data that I need to merge. They do have similarities such as: Order number, Name, type or product. But they have separate data as well like: Order date, and Engravings.
Would I do two separate Append queries in Access into a merged table? Or one Append queries? Or just keep the data separate?
I am new to Access and trying to find the best way to approach this.
Merging the two tables into one completely defeats the purpose of using a database and you're better off using excel at that point. You want to split the data as much as possible along logical lines so that you can find, say... all the orders that Mr X has ever made for a specific product. And in that case you're going to want to have separate tables for customers, orders, engravings and the like.
The best practice from a design standpoint is to place fields that each table has in common into a third "master" table, then create relationships from that table to the existing tables and delete the data that has been transferred to the main table (except for the primary keys, which have to be common with your master table).
To create the master table, use a Make Table query to generate the master table based on one of your tables, then an append query to add any products in the master table that might not be common to both, based on the other table. Finally, delete queries for each table would rid you of redundant data in both original tables.
However, I strongly suggest you use Microsoft's tutorials and download the NorthWind sample database so you can get an idea of what a properly structured database looks like. The beginner's learning curve for access is very steep and having well built example databases is almost a requisite.
Make a backup of your database(s) and play with it until it turns out right. Do not make the mistake of playing with live data until you know what you're doing.
As you have similar fields on either table, take the Order number field from both tables using a union query. Something like:
SELECT tbl_Delivery_Details.OrderNo
FROM tbl_Delivery_Details
GROUP BY tbl_Delivery_Details.OrderNo
UNION
SELECT tbl_Delivery_Header.[Order number]
FROM tbl_Delivery_Header
GROUP BY tbl_Delivery_Header.[Order number];
This would take the order numbers from the delivery details table and from the delivery header table and merge them into one list with only one instance of each order number. Save the query.
You could then use this query in a new query. Bring in your 2 tables to this query and insert the fields from either table that you require.
As users add records to the tables they will be added to the union selet query when it is next run.
PB
It depends on what you want to do. Let's assume you have tables A (with 50 records) and B (with 75) records, and both tables have a similar column called OrderID.
Appending Rows:
If you want to create a table with 125 total records by combining records (rows) from A and records (rows) from B, run the following two queries:
Query 1:
SELECT A.ORDER_NUMBER, A.TEXT_FIELD1 as DATA INTO C
FROM A;
Query 2:
INSERT INTO C ( ORDER_NUMBER, DATA )
SELECT B.ORDER_NUMBER, B.TEXT_FIELD2
FROM B;
Appending Columns: If you want to create a table with 75 total records where you are appending columns from A to the columns in B, then run the following query:
SELECT B.ORDER_NUMBER, A.TEXT_FIELD1, B.TEXT_FIELD2 INTO C
FROM A RIGHT JOIN B ON A.ORDER_NUMBER = B.ORDER_NUMBER;
... in a similar way, you can append columns in B to columns in A in a new table C with a total of 50 records by running the following query:
SELECT A.ORDER_NUMBER, A.TEXT_FIELD1, B.TEXT_FIELD2 INTO C
FROM A LEFT JOIN B ON A.ORDER_NUMBER = B.ORDER_NUMBER;
I have some tables in databases. The table contains more than 150 columns for some custom field operations which may/may not be used by all. Instead of creating static 150 unused column fields, it can be created dynamically using add column.
Can somebody justify, which one is better? When to use dynamic, when static and why?
You can look for below normalization for maintaining custom fields, I have used in multiple web/window application successfully.
FormMasterTable
FormId, Name: this will be identifier and will be passed from Form to DB query to identify itself
datatypeMaster Table
datatypeId, datatypeName
The abobe will define all custom fields you are going to support i.e. customer can create to customize a form.
Table for Form-Customer-CustomFieldMapping say Custom Field Master Table for managing column definition and mapping with customer.
customerId, FormName/FormId, FieldName, customFieldId, datatypeId(FK:datatypeMaster), length ....
Next step is to create all tables to store all datatypes defined in datatypeMaster table i.e. one table for each datatype, some of the examples I can think of....
Custom field detail table for numeric say CustomNumberFields
customFieldId FieldValue(numeric(max))
Custom field detail table for string say CustomTextFields
customFieldId FieldValue(varchar(max))
Custom field detail table for Dates say CustomDateFields
customFieldId FieldValue(datetime)
and so on....
Now you can use all in one query with Inner Join to get all custom columns of a customer.
Sample Query to get customfields fields
select MP.FieldName, CFN.FieldValue from Form-Customer-CustomFieldMapping MP
JOIN CustomNumberFields CFN on MP.CustomFieldId and CFN.CustomFieldId where formId=<<paramformId>> and customerId=<<paramcustomerId>>
UNION
select MP.FieldName, CFN.FieldValue from Form-Customer-CustomFieldMapping MP
JOIN CustomTextFields CFN on MP.CustomFieldId and CFN.CustomFieldId where formId=<<paramformId>> and customerId=<<paramcustomerId>>
UNION
select MP.FieldName, CFN.FieldValue from Form-Customer-CustomFieldMapping MP
JOIN CustomDateFields CFN on MP.CustomFieldId and CFN.CustomFieldId where formId=<<paramformId>> and customerId=<<paramcustomerId>>
I have a table "PackagingType" with 2 columns (materialID | name) and another table "materials" with ID and material details.
For each name from the "packagingtype" I want to assign what materials are available (e.g. in material ID column I would have 1,3,4,5).
I need to match these materialsID with materials table and pull out the details.
What would be the correct way to do it? Not sure if storing data as 1,3,4,5 is the right was and what would be the syntax look like?
In relational database design you don't combine values into one cell. There are exceptions but are few and far between. This would would not be normalized data and would make future query and analysis difficult/complex. So the PackagingType should have the same name multiple times for different materialIDs.
So the table would have data like
MaterialID Name
1 PackageA
3 PackageA
4 PackageA
5 PackageA
Then to get results with the material description you'd simply do a join.
SELECT PT.MaterialID, PT.Name, M.Detail
FROM PackagingType PT
INNER JOIN Materials M
on PT.MaterialID = M.MaterialID
I am trying to run an update query based on 2 fields in a seperate table. I know how to do it based on one field, add the two tables, create a join between the two related fields and run the update. However when I try and run it with two joins it says it cannot execute because it contains ambiquous joins. Here's a brief example of what I'm trying to achieve
Table 1 contains name, location and number of items.
Table 2 contains name, location and and empty field for the number of items.
When i try update table 2 with the information from table 1, with a join between the 2 name fields, it updates the same number of items for each different location.
UPDATE:
I've fixed it, I think I was linking the joins incorrectly.
Here's the finished SQL statement:
UPDATE Tbl_Hourly_Pick_Performance
LEFT JOIN Tbl_Temp_Count_Info
ON (Tbl_Hourly_Pick_Performance.[Sign On]=Tbl_Temp_Count_Info.[Picker ID])
AND (Tbl_Hourly_Pick_Performance.[Pick Floor]=Tbl_Temp_Count_Info.Floor)
SET Tbl_Hourly_Pick_Performance.[No of Stores] = Tbl_Temp_Count_Info.Count;