Using coalesce with left joins to populate master table with most recent data - sql

I have 3 tables, 1 master and 2 tables containing the most recent data that should be populated (update) into the master table.
For this I need to create a transform view, that left joins the two tables with the master. For each value I should use the COALESCE function to always pick the value from one of the two tables with the most recent data, before the value in the master table.
Some fields only exist in the master table and some needs to be updated with the new data from the two other tables.
So far I've been using the COALESCE function like so:
COALESCE(System.tMirror_Roadpiece.Roadcode, System.tMirror_MasterTable.Roadcode)
This should select the value from the tMirror_Roadpiece table if it is not null.
My problem is that I am unsure how to correctly join the tables, while making sure my COALESCE functions work as intended.
Currently my joins look like this:
FROM [System].[tMirror_Roadpiece]
left join System.tMirror_MasterTable on System.tMirror_Roadpiece.Id = System.tMirror_MasterTable.Id
left join System.tMirror_Zipcodes on System.tMirror_MasterTable.Zipcode = System.tMirror_Zipcodes.Zipcode
But when I execute my SELECT statement I get no data back. How should I best structure my joins to update my master table with the data in the other two tables? One table is joined on the Id, while the other is joined on Zipcode.
My intended output is a master table with relevant data updated from the two other tables, while leaving the rest of the data in the master untouched.
Let me know if you need more information.
Edit: Sample data
Master table data (CSV):
123,1,2015-05-07T13:00:48.623,2015-05-07T13:00:48.623,3446,Hospitalsringen,Hospitalsringen,68,,,,5260,Odense S,,,0461,Odense,350455,"Hollufgård Hgd., Fraugde",1f,704050,590541.26,6135939.39,55.36141744,10.42832236,A,5,TN,200,100m_61359_5905,1km_6135_590,10km_613_59,2015-05-07T13:00:49.533,00000667-2566-47c9-9ba0-f5ec6b8ce50f,1,2015-05-07T13:00:48.533,2018-07-04T18:00:00.000,1083,Region Syddanmark,"Hollufgård Hgd., Fraugde",04613436__68_______,9148,Tornbjerg,1465,Fyns Politi,1135,Retten i Odense,0043,Odense Syd,Byzone,350455,1f,704050,04613436__68,22.1,d90e9338-0470-41bc-8ecb-6f71dd912ff0,11604d86-af45-11e7-847e-066cff24d637,Ekstern,B,V0,10.42845878,55.36232181,25,Rosengårdskolen,1,,02da5b79-929c-f1b5-e053-d280220ac400
Edit: SQL code
SELECT System.tMirror_MasterTable.[Infohub_RowId]
,System.tMirror_MasterTable.[Infohub_CreatedDate]
,System.tMirror_MasterTable.[Infohub_ValidityDate] AS [Infohub_ModifiedDate]
,System.tMirror_MasterTable.Infohub_HasChanged
,COALESCE(System.tMirror_Roadpiece.Id, System.tMirror_MasterTable.Id)
,[Status]
,System.tMirror_MasterTable.[Oprettet]
,System.tMirror_MasterTable.[Aendret]
,COALESCE(System.tMirror_Roadpiece.Vejkode, System.tMirror_MasterTable.Vejkode)
,COALESCE(System.tMirror_Roadpiece.Vejnavn, System.tMirror_MasterTable.Vejkode)
,COALESCE(System.tMirror_Zipcodes.Zipcode, System.tMirror_MasterTable.Zipcode)
,COALESCE(System.tMirror_Zipcodes.Zipcodenavn, System.tMirror_MasterTable.Zipcodenavn)
,COALESCE(System.tMirror_Roadpiece.Kommunekode, System.tMirror_MasterTable.Kommunekode)
,System.tMirror_MasterTable.[Kommunenavn]
,System.tMirror_MasterTable.[Regionskode]
,System.tMirror_MasterTable.[Regionsnavn]
,COALESCE(System.tMirror_Roadpiece.Navngivenvej_id, System.tMirror_MasterTable.Navngivenvej_id)
FROM
[System].[tMirror_Roadpiece] left join System.tMirror_MasterTable on System.tMirror_Roadpiece.Id = System.tMirror_MasterTable.Id
left join System.tMirror_Zipcodes on System.tMirror_MasterTable.Zipcode = System.tMirror_Zipcodes.Zipcode
WHERE
System.tMirror_MasterTable.Infohub_HasChanged = 1

Related

I need to Fetch Unique Record from master and with its repeated record in detail

Hello Stackoverflow i am facing problem in join records from master and detail table
using Sql Query
Master Table is Represented By Following Data
I Have Data in Detail Table
What I need in output is
What i tried is
I need sql select query to get the result shown above
SELECT distinct(master.voucherkey), detail.Amount FROM master ,detail
where master.voucherkey = detail.voucherkey
i.e. repeated voucherkey once and unique record many time thanks
You need to join the tables together rather than trying to select from both:
SELECT master.voucherkey, detail.Amount
FROM master
LEFT JOIN detail
ON master.voucherkey = detail.voucherkey
SQL cannot return merged rows like the picture.

Outputting the data from several sql tables without having a common value

I have a select query which combined several tables. PRODUCTION_ORDER_RESULTS, PRODUCTION_ORDERS and SERVICE_GUARANTY_NEW have common value however STOCKS table does not.
SELECT PR_ORDERS.ARRIVED_CITY,
PR_ORDERS.MONTAJ_DATE,
PR_ORDER_RESULT.TRANSFER_DATE,
PR_ORDERS.P_ORDER_ID,
PR_ORDER_RESULT.P_ORDER_ID,
SG.SALE_CONSUMER_ID,
SG.IS_SERI_SONU,
S.BRAND_ID,
S.PROPERTY
FROM workcube_test_1.PRODUCTION_ORDER_RESULTS PR_ORDER_RESULT,
workcube_test_1.PRODUCTION_ORDERS PR_ORDERS,
workcube_test_1.SERVICE_GUARANTY_NEW SG,
workcube_test_1.STOCKS S
WHERE PR_ORDER_RESULT.P_ORDER_ID = PR_ORDERS.P_ORDER_ID
AND PR_ORDER_RESULT.PR_ORDER_ID = SG.PROCESS_ID
when I run the query, it shows the output as below.
The problem here is there are four data rows returned from PRODUCTION_ORDER_RESULTS, PRODUCTION_ORDERS, SERVICE_GUARANTY_NEW and once I have added the STOCKS table, arrived_city, montaj_date, transfer_date columns are side by side with STOCKS table's rows, but the columns value should be null, not filled with data.
The way I tried is UNION of STOCKS table, however unioned table values are ignored, can not use them in html blocks.
there needs to be at least one more join condition among tables where there's for STOCKS table, I think there might exist such a column STOCK_ID within a table such as PRODUCTION_ORDER_RESULTS in order to join with STOCKS table. I think this should be the reason for multiple returning rows. If there's no common column, then the returning data will be produced as many as the number of records within STOCK table due to existing CROSS JOIN logic within the current query. So rearrange your query as
SELECT PR_ORDERS.ARRIVED_CITY,
PR_ORDERS.MONTAJ_DATE,
PR_ORDER_RESULT.TRANSFER_DATE,
PR_ORDERS.P_ORDER_ID,
PR_ORDER_RESULT.P_ORDER_ID,
SG.SALE_CONSUMER_ID,
SG.IS_SERI_SONU,
S.BRAND_ID,
S.PROPERTY
FROM workcube_test_1.PRODUCTION_ORDER_RESULTS PR_ORDER_RESULT
JOIN workcube_test_1.PRODUCTION_ORDERS PR_ORDERS
ON PR_ORDER_RESULT.P_ORDER_ID = PR_ORDERS.P_ORDER_ID
JOIN workcube_test_1.SERVICE_GUARANTY_NEW SG
ON PR_ORDER_RESULT.PR_ORDER_ID = SG.PROCESS_ID
JOIN workcube_test_1.STOCKS S
ON PR_ORDER_RESULT.STOCK_ID = S.ID

UPDATE statement won't update anything

I've been creating a recipe database for a class project using SSMS. I have one table with all the recipe names that I'd inserted into a lookup table (RecipeDetails), but I'd left out their associated IDs, so I wanted to write a bit of code to update my table with those values. I feel like this code should work fine (it seemed to work in the past, but I screwed up something unrelated and had to restore my most recent database backup), but now it just isn't. It says it's affected all the rows I expect it to affect, but those rows still list the RecipeID as NULL.
I'm pulling my data from RecipesTable, which includes the names of each recipe and an ID for each. In RecipeDetails I have a column for RecipeName, RecipeID, Ingredient, IngredientID, and an ID for each row in the table. As of now, I have all my recipes in the table, but not their associated ID. I would like to move the ID's over from one table to another.
UPDATE rd
SET rd.RecipeID = rt.RecipeID
FROM RecipeDetail AS rd
FULL JOIN RecipesTable AS rt ON rd.RecipeID = rt.RecipeID
WHERE rt.RecipeName = rd.RecipeName;
You should use an inner join rather than an outer join.
UPDATE rd
SET rd.RecipeID = rt.RecipeID
FROM RecipeDetail rd JOIN
RecipesTable rt
ON rd.RecipeName = rt.RecipeName;

JOIN of 4 tables, how to restrict SELECT columns to one table only?

I am working on ABAP program - user input is to query column ANLAGE and output is to get all records from table EADZ (and only fields of EADZ) based on ANLAGE.
Statement and joins should work like this:
Input ANLAGE, find in table EASTL, gets LOGIKNR
Input LOGIKNR, find in table EGERR, gets EQUNR
Input EQUNR, find in table ETDZ, gets LOGIKZW
Input LOGIKZW, find in table EADZ, gets all records (this is the final output)
Here is the code I tried:
DATA: gt_cas_rezy TYPE STANDARD TABLE OF eadz,
lv_dummy_eanl LIKE eanl-anlage.
SELECT-OPTIONS: so_anl FOR lv_dummy_eanl NO INTERVALS NO-EXTENSION.
SELECT * FROM eadz
INNER JOIN etdz ON eadz~logikzw EQ etdz~logikzw
INNER JOIN egerr ON etdz~equnr EQ egerr~equnr
INNER JOIN eastl ON egerr~logiknr EQ eastl~logiknr
INTO CORRESPONDING FIELDS OF TABLE #gt_cas_rezy
WHERE eastl~anlage IN #so_anl.
I got the records from table EADZ except that the date fields are empty (even though, they are filled in database table). I am assuming there is a problem with JOINs since in statement like this I join all the fields of all 4 tables into one "record" and then to corresponding fields of internal table.
How to get the values of date fields?
You can find the answer in the documentation.
If a column name appears multiple times and no alternative column name was granted, the last column listed is assigned.
In your case, at least two tables share the same column name. Therefore the values from the last mentioned table are used in the join.
You can solve this by listing the columns explicitly (or eadz~* in your case), giving an alias if required.
SELECT EADZ~* FROM EADZ INNER JOIN ETDZ ON EADZ~LOGIKZW = ETDZ~LOGIKZW
INNER JOIN EGERR ON ETDZ~EQUNR = EGERR~EQUNR
INNER JOIN EASTL ON EGERR~LOGIKNR = EASTL~LOGIKNR
INTO CORRESPONDING FIELDS OF TABLE #gt_cas_rezy
WHERE EASTL~ANLAGE IN #SO_ANL.
If you require additional fields, you can add them explicily with e.g. EADZ~*, EASTL~A.

Inner joining two tables returns empty result

I am trying to get two tables that aren't related to pull a column of data each.
I have one table called AlphaData and one called TLAuth. Each includes a column that is labelled invoice and I need to pull both columns so I can at least start a comparison. TLAuth will include some of the invoice numbers from AlphaData, but not all of them.
Right now I am using the following code:
SELECT Alphadata.Invoice, TLAuth.Invoice
FROM Alphadata
INNER JOIN TlAuth
ON TLauth.TLAUthID = Alphadata.TLAUthID;
But every time I run this it comes up totally blank. There is definitely data in there, I can pull one column of data from each, but not both at the same time. I have even setup a relationship (1 to Many from TL Auth to Alphadata) and it doesn't seem to work so any help would be grand.
If the tables could not match you should use left join
SELECT Alphadata.Invoice, TLAuth.Invoice
From Alphadata
LEFT JOIN TlAuth ON TLauth.TLAUthID=Alphadata.TLAUthID;