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.
I am running a relatively basic query that finds pallet location information in a warehouse. The query finds the locn_id (foreign key) value for the current and destination location. the locn_id is just a DB ID and has a Display Value associated with it in a different table that I need to add to the query. However, I don't know how to join the table multiple times (once for each column)..
Essentially what I need to happen is similar to an Excel VLOOKUP funtion. I need a solution that can look at my query, take the locn_id value from the two columns and provide me with the dsp_locn value from a different table. Is this possible?
This is the current query:
Select lpn.prev_sub_locn_id "PREV LOCATION", lpn.dest_sub_locn_id "DEST LOCATION"
FROM LPN_DETAIL
INNER JOIN LPN ON LPN.LPN_ID = LPN_DETAIL.LPN_ID
and LPN_FACILITY_STATUS = '50' and lpn.prev_sub_locn_id is not null;
Outputs:
PREV LOCATION DEST LOCATION
0102308 0085795
0099674 0083978
0098148 0082971
0096834 0081068
0096822 0080925
0096931 0081502
0096081 0078225
These values however are useless since they are only DB id's. I need a column in the query that will show the dsp_locn value found in the LOCN_HDR table below:
Outputs for LOCN_HDR table:
LOCN_ID DSP_LOCN
0067714 C-E-091-A-1
0067715 C-E-091-A-2
0067717 C-E-091-B-1
0067718 C-E-091-B-2
0067726 C-E-093-A-1
Disclaimer, I am newish to SQL still so please try to keep it basic. I am also not able to be creating new tables in the DB. Any questions I'll be sure to respond. This is on Oracle SQL Server. Thanks for the help!
Try something like this, you sould include a join for each location in order to get its description. I assume there is always a description for any location.
Select ORIGIN_LOC.DSP_LOCN "PREV LOCATION", DESTINATION_LOC.DSP_LOCN "DEST LOCATION"
FROM LPN_DETAIL
INNER JOIN LPN ON LPN.LPN_ID = LPN_DETAIL.LPN_ID
INNER JOIN LOCN_HDR AS ORIGIN_LOC ON ORIGIN_LOC.LOCN_ID = lpn.prev_sub_locn_id
INNER JOIN LOCN_HDR AS DESTINATION_LOC ON DESTINATION_LOC.LOCN_ID = lpn.dest_sub_locn_id
and LPN_FACILITY_STATUS = '50' and lpn.prev_sub_locn_id is not null;
Hope it helps!
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.
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
Query below:
select
cu.course_id as 'bb_course_id',
cu.user_id as 'bb_user_id',
cu.role as 'bb_role',
cu.available_ind as 'bb_available_ind',
CASE cu.row_status WHEN 0 THEN 'ENABLED' ELSE 'DISABLED' END AS 'bb_row_status',
eff.course_id as 'registrar_course_id',
eff.user_id as 'registrar_user_id',
eff.role as 'registrar_role',
eff.available_ind as 'registrar_available_ind',
CASE eff.row_status WHEN 'DISABLE' THEN 'DISABLED' END as 'registrar_row_status'
into enrollments_comparison_temp
from narrowed_users_enrollments cu
full outer join enrollments_feed_file eff on cu.course_id = eff.course_id
Quick background: I'm taking the data from a replicated table and selecting it into narrowed_users_enrollments based on some criteria. In a script I'm taking a text feed file, with enrollment data, and inserting it into enrollments_feed_file. The purpose is to compare the most recent enrollment data with enrollments already in the database.
However the issue is that joining these tables results in about 160,000 rows when I'm really only expecting about 22,000. The point of doing this comparison is so that I can look for nulled values on either side of the join. For example, if the table on the right contains a null, then disable the enrollment record. If the table on the left contains a null, then add this student's enrollment.
I know it's a little off because I'm not using PKs or FKs. This is what is selected into the table:
Here's a screenshot showing a select * from the enrollments table on the left and a feed file on the right.
http://i.imgur.com/0ZPZ9HS.png
Here's a screenshot showing the newly created table from the full outer join.
http://i.imgur.com/89ssAkS.png
As you can see even though there there's only one matching enrollment(the matching jmartinez12 columns), there's 4 extra rows created for the same record on the left for the enrollments on the right. What I'm trying to get is for it to be 5 rows, with the first being how it is in the screenshot(matching pre-existing enrollment and enrollment in the feed file), BUT, the next 4 rows with the bb_* columns should be NULL up to the registrar_course_id.
Am I overlooking something simple here? I've tried a select distinct and I've added a where clause specifying when the course_ids are equal however that ensures that I won't get null rows which I need. I have also joined the tables on the user_id however the results are still the same.
One quick suggestion is to add the DISTNCT clause. If the records you are setting are complete duplicates that may cut it down to what you are expecting.
The fix was to also join on:
ON cu.course_id = eff.course_id AND cu.user_id = eff.user_id