We're integrating our erp and crm. We want to compare lookup codes. The erp has codes in the code table, code_value column. The crm has a lookup table with a delimiter of // in the column. For example, the crm has state with the state abbreviation and // then the state long name. VA// VIRGINIA. It has the country code split with the first 15 characters and a //. I need to do left and right joins on the code_value column and the crm entry column on the characters before the // to find codes that don't match so we can update those to match. How do I join on the characters before the // delimiter? The position will change. There are many other codes to compare but the pattern is the same. The charters before the // delimiter in the crm match the erp. I need something like join on the length of the column before the // characters.
version sql server 2008r
erp
code table
code_value column
united states of america
Equatorial Guinea
VA
VI
crm
lookup table
entry
united states o//f america
Equatorial Guin//ea
VA// VIRGINIA
VI// VIRGIN ISLANDS
SELECT LOOKUP_CODE, CODE_VALUE_KEY, CODE_VALUE, SHORT_DESC, MEDIUM_DESC, LONG_DESC,
FIELDNAME, LOOKUPSUPP, ENTRY, MASTERVALUE, U_ENTRY, recid
FROM Bridge_Test.DBO.CODE_TABLE PW LEFT JOIN GoldMine_test.dbo.LOOKUP GM ON PW.MEDIUM_DESC = GM.ENTRY --need to join on first 15 characters
Just add LEFT() function to the on join part
it will look like this LEFT(GM.ENTRY, 15)
SELECT LOOKUP_CODE, CODE_VALUE_KEY, CODE_VALUE, SHORT_DESC, MEDIUM_DESC, LONG_DESC, FIELDNAME, LOOKUPSUPP, ENTRY, MASTERVALUE, U_ENTRY, recid
FROM Bridge_Test.DBO.CODE_TABLE PW
LEFT JOIN GoldMine_test.dbo.LOOKUP GM
ON LEFT(PW.MEDIUM_DESC, CharIndex('//', GM.ENTRY) -1) = LEFT(GM.ENTRY, CharIndex('//', GM.ENTRY ) -1)
Related
My data set is a list of house sales. I am trying to update the address column so that any NULL values get replaced with an actual address. I am getting these replacement addresses from other inputs within the data. There are other sales of the same house so they share a "parcelid". I am doing an UPDATE with COALESCE and an INNER JOIN to complete the task. I am doing so with this code.
UPDATE house
SET property_address =
COALESCE(house1.property_address,house2.property_address)
FROM house AS house1
JOIN house AS house2
ON house1.parcelid = house2.parcelid
AND house1.uniqueid != house2.uniqueid
WHERE house1.property_address IS NULL
When I look at my 'house' table though, every single entry in the 'property_address' line has been updated to the same address. When I did some digging that address is the first address that occurs when I look at the join with this code.
SELECT
house1.parcelid,
house2.parcelid,
house1.property_address,
house2.property_address,
COALESCE(house1.property_address,house2.property_address)
FROM house AS house1
JOIN house AS house2
ON house1.parcelid = house2.parcelid
AND house1.uniqueid != house2.uniqueid
WHERE house1.property_address IS NULL
How to I get it to one ignore any addresses that are not null and two update to the appropriate address and not just keep copying the same address. Thanks for the help. (This is my first time asking a question here so any pointers on how to properly format would be appreciated)
Is it possible to get the date column from the different table based on conditions in a single query?
In SPE_common_data is the Main table, there is spe_content_type column has JOURNAL PAPER or CONFERENCE PAPER are values.
If the column value is JOURNAL PAPER – use SPE_journal_data table and use spe_journal_pub_online_date as active date to publish paper
IF the column value is CONFERENCE PAPER – use SPE_conference_data table to find spe_meeting_code for that paper
4.And based on that spe_meeting_code go to Petro_meetings table and find early_start_date for that meeting code and use that date to publish that paper.
Is this possible for everything in a single query?
Try to union two queries by the set_content_type value, for example like this:
SELECT
cmn.spe_manuscript_num,
cmn.spe_content_type,
jrn.spe_journal_pub_online_date AS pub_online_date
FROM spe_common_data cmn
JOIN spe_journal_data jrn ON cmn.spe_manuscript_num = jrn.spe_manuscript_num
WHERE cmn.spe_content_type = 'JOURNAL PAPER'
UNION ALL
SELECT
cmn.spe_manuscript_num,
cmn.spe_content_type,
pm.early_start_date AS pub_online_date
FROM spe_common_data cmn
JOIN spe_conference_data cnf ON cmn.spe_manuscript_num = cnf.spe_manuscript_num
JOIN petro_meetings pm ON cnf.spe_meeting_code = pm.spe_meeting_code
WHERE cmn.spe_content_type = 'CONFERENCE PAPER'
I have two tables of addresses that I am trying to match to. Both are structured with Street (including number), city, state and zipcode. In my query, I am trying to join the two tables Like this:
JOIN [AxSupport].[dbo].Address ADR ON LPA.City = ADR.City
and LPA.State = ADR.State
and ADR.ZipCode = LPA.ZIPCODE
and ADR.COUNTRY = LPA.COUNTRY
and ADR.Street = LPA.STREET
I tested with one customer that has 2020 address records records and the joined result only returns 79 records. If I change the street comparison to Soundex(ADR.Street) = SoundEx(LPA.Street) I get 2660 records. Is there another way of formatting the streets to get an accurate return?
I created 3 tables TstInvoice, TstProd, TstPersons and added some data:
INVOICE_NBR CLIENT_NR VK_CONTACT
A10304 003145 AT
A10305 000079 EA
A10306 004458 AT
A10307 003331 JDJ
PROD_NR INVOICE_NBR
P29366 A10304
P29367 A10304
P29368 A10305
P29369 A10306
P29370 A10306
P29371 A10307
PERS_NR INITIALEN STATUS PERSOON
0001 AT 7 Alice Thompson
0002 EA 1 Edgar Allen
0003 JDJ 1 John Doe Joe
0004 AT 1 Arthur Twins
The parameter that is passed to the crystal report is the INVOICE_NBR.
On my crystal report I put some fields from the databases and one sql expression:
(
SELECT "TstPersons"."PERSOON" FROM "TstPersons"
WHERE "TstPersons"."INITIALEN" = "TstInvoice"."VK_CONTACT" AND "TstPersons"."STATUS" = 1
)
The full query that is generated:
SELECT "TstInvoice"."INVOICE_NBR", "TstInvoice"."CLIENT_NR", "TstPersons"."STATUS", "TstPersons"."PERSOON", "TstProd"."PROD_NR", "TstProd"."INVOICE_NBR", (
SELECT "TstPersons"."PERSOON" FROM "TstPersons"
WHERE "TstPersons"."INITIALEN" = "TstInvoice"."VK_CONTACT" AND "TstPersons"."STATUS" = 1
)
FROM ("GCCTEST"."dbo"."TstInvoice" "TstInvoice" INNER JOIN "GCCTEST"."dbo"."TstProd" "TstProd" ON "TstInvoice"."INVOICE_NBR"="TstProd"."INVOICE_NBR") INNER JOIN "GCCTEST"."dbo"."TstPersons" "TstPersons" ON "TstInvoice"."VK_CONTACT"="TstPersons"."INITIALEN"
WHERE "TstInvoice"."INVOICE_NBR"='A10304'
The result is as shown in the screenshot:
As you can see the TstPersons.PERSOON field is populated with Alice Thompson and the sql expression field is correctly populated with Arthur Twins. However, I would like only to see the prod_nr once. With this query it produces the prod numbers twice because of the double entry for "AT" despite the fact that I ask for only status 1. I could just delete the old entry but I want to know if it's possible this way.
* edit * I added the status = 1 to the "record selection formula editor" and that seems to work. Not need the sql expression field at all. Not sure if this is the correct way to go though.
So now it looks like this:
SELECT "TstInvoice"."INVOICE_NBR", "TstInvoice"."CLIENT_NR", "TstPersons"."STATUS", "TstPersons"."PERSOON", "TstProd"."PROD_NR", "TstProd"."INVOICE_NBR"
FROM ("GCCTEST"."dbo"."TstInvoice" "TstInvoice" INNER JOIN "GCCTEST"."dbo"."TstProd" "TstProd" ON "TstInvoice"."INVOICE_NBR"="TstProd"."INVOICE_NBR") INNER JOIN "GCCTEST"."dbo"."TstPersons" "TstPersons" ON "TstInvoice"."VK_CONTACT"="TstPersons"."INITIALEN"
WHERE "TstInvoice"."INVOICE_NBR"='A10304' AND "TstPersons"."STATUS"=1
You have a very weak join in your query due to the duplicate values found in the INITIALEN column. Using the STATUS = 1 criteria is a work-around more than a solution because if you ever need to report on an invoice where the contact has a status other than 1, you will need to modify the report's design to allow your join to work because the STATUS value is not found on the invoice to allow a proper join to occur.
You are also running a risk of this work-around breaking down completely should you have another contact with both the same initials and status values as another.
The correct way to solve this problem would be to join TstInvoice to TstPersons through a field that has unique values. The PERS_NR column appears to be a good choice for this.
This is also going to require a redesign of the TstInvoice table to include the PERS_NR column as a Foreign Key.
A stronger join between invoices and persons would also remove the need for that sub-query in you selection statement. This would simplify your query down to the following:
SELECT "TstInvoice"."INVOICE_NBR", "TstInvoice"."CLIENT_NR", "TstPersons"."STATUS", "TstPersons"."PERSOON", "TstProd"."PROD_NR", "TstProd"."INVOICE_NBR"
FROM "GCCTEST"."dbo"."TstInvoice" "TstInvoice"
INNER JOIN "GCCTEST"."dbo"."TstProd" "TstProd"
ON "TstInvoice"."INVOICE_NBR"="TstProd"."INVOICE_NBR"
INNER JOIN "GCCTEST"."dbo"."TstPersons" "TstPersons"
ON "TstInvoice"."PERS_NR"="TstPersons"."PERS_NR"
WHERE "TstInvoice"."INVOICE_NBR"='A10304'
I am building an access database for air freight for my company. I have a table with all the airports, and I have another table with air freight costs. The following are my fields for each table (a * means that it is part of a key)
tblAirports
AirportID*(Autonumber)
AirportCode(Text - 3 letter IATA code)
AirportCity(Number - connects to a cities table)
tblAirFreight
OriginAirport* (Number - connects to tblAirports)
DestAirport* (Number - connects to tblAirports)
Company* (Number - connects to a table of companies)
50 kgs.
100 kgs.
500 kgs.
Air freight is connected to the airport table with referential integrity on both origin and destination services.
Right now, not even a straightforward select query will work on the air freight table. I want to type in an origin airport code (SEA, JFK, etc.) and/or destination airport code, and the query to return the corresponding rates for air freight. How do I do this?
It sounds like you want to get the airport details for each of the airport columns in the freight table so you just join to the airports table twice. then you filter based on the parameters supplied. This is only rough to point you in the right direction, you will probably need to play with the where clause a bit.
select *
from
tblAirFreight f
inner join tblAirports o
on o.AirportID = f.OriginAirport
inner join tblAirports d
on d.AirportID = f.DestAirport
where
(f.OriginAirport = #YourOriginAirport or isnull(#YourOriginAirport) = 1)
and (f.DestAirport = #YourDestAirport or isnull(#YourDestAirport) = 1)