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

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.

Related

How to query 2 different schema tables with different column name same information in postgresql

When I query I do not get match list side by side and database reference fails, why?
I tried using one connection and two different schemas, first schema name table has same reference details with second table column, and I would like to compare side by side what equals exactly from rw.reference table 1, with table 2 qt.ref_number because they should match between both. I only get output data from top first query with rd.reference and second table does not list anything with, qt.ref_number, why postgresql does not cross database reference?
Query:
Select distinct
rd.reference,
region1_scorecard
From local_dev.user.rawdata rd
Inner join customerid as id
on rd.site=id.site
union all
Select distinct
qt.ref_number,
region2_decision
From local_dev.account.quote qt
Inner join rep_id id
on qt.application=id.application
Order by rd.reference

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

SQL Computed Column, CountIF 2 Tables

I have two tables
tblData_VendorMasterSSPaymentTerms
tblData_VendorMasterSSPaymentTermsCLM
tblData_VendorMasterSSPaymentTerms contains a field labled VMSSPayTerms_AribaContractID which the values exist in table tblData_VendorMasterSSPaymentTermsCLM
So in table tblData_VendorMasterSSPaymentTermsCLM I want to create a calculated column that counts how many records in tblData_VendorMasterSSPaymentTerms contains the Contract ID for that record.
This is what I have put together so far but it is still coming up with an error
SELECT Count(VMSSPayTerms_AribaContractID)
From tblData_VendorMasterSSPaymentTerms
Where VMSSPayTerms_AribaContractID=VMSSPayTermsCLM_ContractID
Can someone help me identify what I am doing wrong here?
You must join the tables, group by VMSSPayTermsCLM_ContractID and count:
select
c.VMSSPayTermsCLM_ContractID,
count(t.VMSSPayTerms_AribaContractID) counter
from tblData_VendorMasterSSPaymentTermsCLM c inner join tblData_VendorMasterSSPaymentTerms t
on t.VMSSPayTerms_AribaContractID = c.VMSSPayTermsCLM_ContractID
group by c.VMSSPayTermsCLM_ContractID

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

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

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;