Oracle complex Join - sql

I have three queries,query 1 and query 2 have to be inner joined on Customer_number
Resultant output from query1 and query2 will be further inner joined on 3 keys customer_number,account_number,bank_number.
Can anyone help me to write the queries for these.I am not able to derive it.
Thanks in advance,
Bhaskar
Query1 :
SELECT
CUST.BANK_NUMBER as CUSTBANKNUMBER,
CUST.CUSTOMER_NUMBER,
CUST.DATE_DECEASE,
FROM
SIGN_LND.T_CUSTOMER_MASTER_FILE_LND CUST
LEFT OUTER JOIN
SIGN_LND.T_CUST_USER_DEF_FIELDS_LND USER_DEF
ON (CUST.CUSTOMER_NUMBER = USER_DEF.CUSTOMER_NUMBER AND CUST.EFF_DT = USER_DEF.EFF_DT)
WHERE
CUST.EFF_DT = TO_DATE('2015-01-22','YYYY-MM-DD')
Query 2 :
SELECT
S.CUSTOMER_NUMBER,
S.ALIAS_ACCOUNT_NBR,
M.BANK_NUMBER,
M.ACCOUNT_NUMBER,
M.PRODUCT_CODE,
M.APPLICATION_NUMBER,
M.APPL_ID
FROM
(
SELECT T1.BANK_NUMBER,
T1.ACCOUNT_NUMBER,
T1.PRODUCT_CODE,
CAST (NULL AS VARCHAR(5)) AS SUB_PRODUCT_CODE,
T1.APPLICATION_NUMBER,
T1.APPL_ID
FROM SIGN_LND.T_LOAN_MASTER_FILE_LND T1
INNER JOIN SIGN_LND.T_LOAN_PRODUCT_DEFINITION_LND T2
ON T1.BANK_NUMBER = T2.BANK_NUMBER
AND T1.PRODUCT_CODE = T2.PRODUCT_CODE
AND T1.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
AND T2.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
WHERE T1.PRODUCT_CODE IS NOT NULL
AND T1.DATE_OPEN IS NOT NULL
UNION ALL
SELECT T1.BANK_NUMBER,
T1.ACCOUNT_NUMBER,
T1.PRODUCT_CODE,
T1.PRODUCT_CODE AS SUB_PRODUCT_CODE,
T1.APPLICATION_NUMBER,
T1.APPL_ID
FROM SIGN_LND.T_TA_MASTER_FILE_LND T1
INNER JOIN SIGN_LND.T_TA_PRODUCT_DEFINITION_LND T2
ON T1.BANK_NUMBER = T2.BANK_NUMBER
AND T1.PRODUCT_CODE = T2.PRODUCT_CODE
AND T1.ACCOUNT_TYPE = T2.ACCOUNT_TYPE
AND T1.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
AND T2.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
LEFT JOIN SIGN_LND.T_BONUS_INT_MASTER_FILE_LND T3
ON T1.ACCOUNT_NUMBER = T3.ACCOUNT_NUMBER
AND T1.BANK_NUMBER = T3.BANK_NUMBER
AND T3.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
WHERE T1.PRODUCT_CODE IS NOT NULL
AND T1.ACCOUNT_STATUS_CODE IS NOT NULL
AND T1.DATE_OPEN IS NOT NULL
UNION ALL
SELECT T1.BANK_NUMBER,
T1.ACCOUNT_NUMBER,
T1.PRODUCT_CODE,
T1.PRODUCT_CODE AS SUB_PRODUCT_CODE,
T1.APPLICATION_NUMBER,
T1.APPL_ID,
FROM SIGN_LND.T_TIME_MASTER_FILE_LND T1
INNER JOIN SIGN_LND.T_TM_PRODUCT_DEFINITION_LND T2
ON T1.BANK_NUMBER = T2.BANK_NUMBER
AND T1.PRODUCT_CODE = T2.PRODUCT_CODE
AND T1.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
AND T2.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
LEFT JOIN SIGN_LND.T_BONUS_INT_MASTER_FILE_LND T3
ON T1.ACCOUNT_NUMBER = T3.ACCOUNT_NUMBER
AND T1.BANK_NUMBER = T3.BANK_NUMBER
AND T3.EFF_DT=TO_DATE('2015-01-22','YYYY-MM-DD')
WHERE T1.PRODUCT_CODE IS NOT NULL
AND T1.ACCOUNT_STATUS_CODE IS NOT NULL
AND T1.DATE_OPEN IS NOT NULL
)M
INNER JOIN
(
SELECT
T1.ACCOUNT_NUMBER,
T1.CUSTOMER_NUMBER,
T1.RELATIONSHIP ,
T2.ALIAS_ACCOUNT_NBR,
T2.APPLICATION_NUMBER
FROM SIGN_LND.T_CUSTOMER_CROSS_REF_LND T1
INNER JOIN
SIGN_LND.T_ALIAS_ACCT_NBR_CRSS_REF_LND T2
ON T1.ACCOUNT_NUMBER=T2.ACCOUNT_NUMBER
WHERE ALIAS_ACCOUNT_NBR_TYPE='UK'
GROUP BY T1.ACCOUNT_NUMBER,
T1.CUSTOMER_NUMBER,
T1.RELATIONSHIP ,
T2.ALIAS_ACCOUNT_NBR,
T2.APPLICATION_NUMBER
)S
ON M.ACCOUNT_NUMBER=S.ACCOUNT_NUMBER
AND
M.APPLICATION_NUMBER=S.APPLICATION_NUMBER
)
Query3:
SELECT
SPLIT.BANK_NUMBER,
SPLIT.CUSTOMER_NUMBER,
SPLIT.DATE_MOVING_IN,
SPLIT.TITLE,
SPLIT.FIRST_NAME,
SPLIT.SECOND_NAME,
SPLIT.SURNAME,
SPLIT.CURR_APT_NUMBER_UPS,
SPLIT.CURR_HOUSE_NAME_UPS,
SPLIT.CURR_HOUSE_NBR,
SPLIT.CURR_STREET_UPS,
SPLIT.CURR_DISTRICT_UPS,
SPLIT.CURR_POST_TOWN_UPS,
SPLIT.POSTAL_CODE,
SPLIT.CURR_COUNTRY,
CUST_EXT.LOYALTY_CARD_NBR,
XREF.ACCOUNT_NUMBER,
XREF.RELATIONSHIP as RELATIONSHIP_RB,
RB_ALIAS_XREF.APPL_ID as RBXREFAPPID,RB_ALIAS_XREF.ALIAS_ACCOUNT_NBR AS ALIAS_ACCOUNT_NBR_RB,
PROD_OFFER.DATE_CLOSE
FROM
(
SIGN_LND.T_CIF_SPLIT_NAME_ADDR_UPS_LND SPLIT
LEFT OUTER JOIN
SIGN_LND.T_CUST_MASTER_EXTENSION_LND CUST_EXT
ON (SPLIT.CUSTOMER_NUMBER = CUST_EXT.CUSTOMER_NUMBER AND SPLIT.EFF_DT = CUST_EXT.EFF_DT)
)
LEFT OUTER JOIN
(
SIGN_LND.T_CUSTOMER_CROSS_REF_LND XREF
LEFT OUTER JOIN
(
SELECT * FROM SIGN_LND.T_ALIAS_ACCT_NBR_CRSS_REF_LND WHERE EFF_DT = TO_DATE('2015-01-22','YYYY-MM-DD') AND ALIAS_ACCOUNT_NBR_TYPE = 'RB'
) RB_ALIAS_XREF
ON (XREF.ACCOUNT_NUMBER = RB_ALIAS_XREF.ACCOUNT_NUMBER AND XREF.EFF_DT = RB_ALIAS_XREF.EFF_DT)
LEFT OUTER JOIN
SIGN_LND.T_PRODUCT_OFFER_PURCHASE_LND PROD_OFFER
ON (XREF.BANK_NUMBER = PROD_OFFER.BANK_NUMBER AND XREF.ACCOUNT_NUMBER = PROD_OFFER.ACCOUNT_NUMBER AND XREF.EFF_DT = PROD_OFFER.EFF_DT)
)
ON (SPLIT.CUSTOMER_NUMBER = XREF.CUSTOMER_NUMBER AND SPLIT.EFF_DT = XREF.EFF_DT)
WHERE SPLIT.EFF_DT = TO_DATE('2015-01-22','YYYY-MM-DD')``

This should work
WITH query1 as
(your query 1 code),
query2 as
(you query 2 code),
query3 as
(your query 3 code)
select * from query1
join query2 on query1.Customer_number=query2.Customer_number
join query3 on query3.customer_number=query2.customer_number
and query3.account_number=query2.account_number
and query3.bank_number=query2.bank_number

Related

Combining records using Hive window frame

I am having a following query which is executed in Hive for a single date. But the table is having massive number of rows containing dudate from 2000 onwards. I want to create a window function for this dudate for previous day and previous week. Basically trying to convert this query including all dates dynamically using window function.
SQL
SELECT R.rmid as rid, '2021-05-30' as pdate, Sum(odn) as Odn, Sum(gdn) as gdn, Sum(wdn) as wdn,0 as podn,0 as pgdn,0 as pwdn,0 as aodn,0 as agdn,0 as awdn
FROM table1 as cd
inner Join table2 c on c.mid = cd.mid
inner join table3 rsi on rsi.omid = c.mid and rsi.omtype = 1
inner join table4 rs on rs.rsmid = rsi.rsmid
inner join table5 r on r.rmid = rs.rmid
WHERE cd.dudate = date('2021-05-30')
GROUP BY R.rmid,pdate
Union
SELECT R.rmid as rid,'2021-05-30' as pdate, 0 as Odn,0 as gdn,0 as wdn,Sum(odn) as podn, Sum(gdn) as pgdn, Sum(wdn) as pwdn,0 as aodn,0 as agdn,0 as awdn
FROM table1 as cd
inner Join table2 c on c.mid = cd.mid
inner join table3 rsi on rsi.omid = c.mid and rsi.omtype = 1
inner join table4 rs on rs.rsmid = rsi.rsmid
inner join table5 r on r.rmid = rs.rmid
WHERE cd.dudate = date_add('2021-05-30', -1)
GROUP BY R.rmid,pdate
Union
SELECT R.rmid as rid,'2021-05-30' as pdate,0 as Odn,0 as gdn,0 as wdn,0 as podn,0 as pgdn,0 as pwdn,Sum(odn)/7 as aodn, Sum(gdn)/7 as agdn, Sum(wdn)/7 as awdn
FROM table1 cd
inner Join table2 c on c.mid = cd.mid
inner join table3 rsi on rsi.omid = c.mid and rsi.omtype = 1
inner join table4 rs on rs.rsmid = rsi.rsmid
inner join table5 r on r.rmid = rs.rmid
WHERE cd.dudate BETWEEN date_add('2021-05-30', -7) AND date_add('2021-05-30', -1)
GROUP BY R.rmid
Basically combining all rows for given date, previous day and 7 days before records.
Can anyone provide me an idea how to union values in windowframes?

Why doesn't my query return correct results?

I have used these Temp table to return total no of SOlved Cases and Total Number of Pending Cases from same table grouped by DIstrict e.g.
District TotalSolvedCases TotalPendingCases
A 3 1
B 8 6
C 7 1
I have done this but this doesn't return correct Result
SELECT *
INTO #Table1
FROM (
SELECT COUNT(Cases.pk_Cases_CaseID) TotalCases,
Districts.DistrictName
FROM Cases
INNER JOIN ConcernedOffices ON ConcernedOffices.pk_ConcernedOffices_ID = Cases.fk_ConcernedOffices_Cases_ConcernedOfficeID
INNER JOIN Districts ON Districts.pk_Districts_DistrictID = ConcernedOffices.fk_Districts_ConcernedOffices_DistrictID
INNER JOIN CaseHearings ON CaseHearings.fk_Cases_CaseHearings_CaseID = Cases.pk_Cases_CaseID
WHERE CaseHearings.IsClosingDate = 1
GROUP BY Districts.DistrictName
) d
SELECT *
INTO #Table2
FROM (
SELECT COUNT(Cases.pk_Cases_CaseID) TotalPedningCases,
Districts.DistrictName
FROM Cases
INNER JOIN ConcernedOffices ON ConcernedOffices.pk_ConcernedOffices_ID = Cases.fk_ConcernedOffices_Cases_ConcernedOfficeID
INNER JOIN Districts ON Districts.pk_Districts_DistrictID = ConcernedOffices.fk_Districts_ConcernedOffices_DistrictID
INNER JOIN CaseHearings ON CaseHearings.fk_Cases_CaseHearings_CaseID = Cases.pk_Cases_CaseID
WHERE CaseHearings.IsClosingDate = 0
GROUP BY Districts.DistrictName
) d
SELECT #Table1.TotalCases AS TotalSolvedCases,
#Table2.TotalPedningCases,
#Table1.DistrictName
FROM #Table1
INNER JOIN #Table2 ON #Table2.DistrictName = #Table1.DistrictName
GROUP BY #Table1.TotalCases,
#Table2.TotalPedningCases,
#Table1.DistrictName
You only need one SELECT, use case expressions to do conditional counting:
SELECT COUNT(case when CaseHearings.IsClosingDate = 1 then 1 end) TotalCases,
COUNT(case when CaseHearings.IsClosingDate = 0 then 1 end) TotalPedningCases,
Districts.DistrictName
FROM Cases
INNER JOIN ConcernedOffices ON ConcernedOffices.pk_ConcernedOffices_ID = Cases.fk_ConcernedOffices_Cases_ConcernedOfficeID
INNER JOIN Districts ON Districts.pk_Districts_DistrictID = ConcernedOffices.fk_Districts_ConcernedOffices_DistrictID
INNER JOIN CaseHearings ON CaseHearings.fk_Cases_CaseHearings_CaseID = Cases.pk_Cases_CaseID
GROUP BY Districts.DistrictName

Problems with Sql query join

I am struggling with a sql query. I want to include the sum from an other table.
SELECT DISTINCT
tblProject.CompanyID,
tblCompany.Name,
tblCompany.AvtalsKund,
tblProject.ProjectName,
tblProject.Estimate,
tblProject.ProjectStart,
tblProject.Deadline,
CONVERT(VARCHAR(8), tblProject.Deadline, 2) AS [YY.MM.DD] ,
tblProject.PreOffered,
tblProject.ProjectType,
tblProjectType.ProjType,
tblOrdered.FirstName + + tblOrdered.LastName as OrderedFullName,
tblProject.ProjectID,
tblProject.RegDate,
tblProject.ProjectNr,
tblProject.ProjectNr
FROM tblProject
INNER JOIN tblCompany ON tblProject.CompanyID = tblCompany.CompanyID
---> INNER JOIN (SELECT tblTimeRecord.ProjectID, SUM(CONVERT(float,replace([Hours],',','') ))
FROM tblTimeRecord group by tblTimeRecord.ProjectID) as b
ON b.ProjectID = tblProject.ProjectID
INNER JOIN tblTimeRecord ON tblTimeRecord.ProjectID = tblProject.ProjectID
INNER JOIN tblProjectType ON tblProject.ProjectType = tblProjectType.ProjTypeID
LEFT OUTER JOIN tblOrdered ON tblProject.OrderedBy = tblOrdered.OrderedID
LEFT OUTER JOIN tblRel_WorkerProject ON tblProject.ProjectID = tblRel_WorkerProject.ProjectID
LEFT OUTER JOIN tblPerson ON tblPerson.PersonID = tblRel_WorkerProject.WorkerID
LEFT OUTER JOIN tblRel_StatusWorkerProject ON tblProject.ProjectID = tblRel_StatusWorkerProject.ProjectID
I want to include this sum-block from table tblTimeRecord.
I get a sum of timerapports with this code
SELECT tblTimeRecord.ProjectID,
SUM(CONVERT(float,replace([Hours],',','') ))
FROM tblTimeRecord where ProjectID=1312 group by tblTimeRecord.ProjectID
Guess i do it in join?
Got it working with this.
SELECT DISTINCT
tblProject.ProjectID,
Summa,
tblProject.CompanyID,
tblCompany.Name,
tblCompany.AvtalsKund,
tblProject.ProjectName,
tblProject.Estimate,
tblProject.ProjectStart,
tblProject.Deadline,
CONVERT(VARCHAR(8), tblProject.Deadline, 2) AS [YY.MM.DD] ,
tblProject.PreOffered,
tblProject.ProjectType,
tblProjectType.ProjType,
tblOrdered.FirstName + + tblOrdered.LastName as OrderedFullName,
tblProject.ProjectID,
tblProject.RegDate,
tblProject.ProjectNr,
tblProject.ProjectNr
FROM tblProject
INNER JOIN tblCompany ON tblProject.CompanyID = tblCompany.CompanyID
INNER JOIN (SELECT tblTimeRecord.ProjectID, SUM(CONVERT(float,replace([Hours],',','') )) as Summa FROM tblTimeRecord group by tblTimeRecord.ProjectID) as b
ON b.ProjectID = tblProject.ProjectID
INNER JOIN tblTimeRecord ON tblTimeRecord.ProjectID = tblProject.ProjectID
INNER JOIN tblProjectType ON tblProject.ProjectType = tblProjectType.ProjTypeID
LEFT OUTER JOIN tblOrdered ON tblProject.OrderedBy = tblOrdered.OrderedID
LEFT OUTER JOIN tblRel_WorkerProject ON tblProject.ProjectID = tblRel_WorkerProject.ProjectID
LEFT OUTER JOIN tblPerson ON tblPerson.PersonID = tblRel_WorkerProject.WorkerID
LEFT OUTER JOIN tblRel_StatusWorkerProject ON tblProject.ProjectID = tblRel_StatusWorkerProject.ProjectID
There are two ways to do this.
You can use a WITH clause to create the aggregate table then join this to the main query.
Or do it this way:
SELECT m.BLAH
,m.FOO
,x.AMOUNT
FROM MAINTABLE m
LEFT JOIN
(
SELECT FOO
,SUM(AMOUNT) as AMOUNT
FROM OTHERTABLE
GROUP BY FOO
) x
ON m.FOO = x.FOO
I prefer the second way.

Invoke a column twice with different conditions

I really appreciate any help with this matter :)
Am Working on a Report now and I had faced some troubles
I have this Query and it work fine , now I want to add a coulmn that is already exist in the query(from the same table) , but this time i'll change the condition of it , BTW the conditions in both of the 2 column are based on one other column
like for example If I have this :
Select Price from ITM1 WHERE PriceList = '1'
and also this
Select Price from ITM1 WHERE PriceList = '10'
how I can write in the same query and let them display in two different column ?
I will put the Query here in case if some one can help me through it :
you can see THE Column Price & PriceList in the lower part of it ,Bolded.
I just need to make the samething again but with a new coulmn name thats it.
Using the IN Operator will give you what you want. However, there are other changes that you can make to your query which would boost performance - but it's out of scope to the question. I'm unclear as to what you're trying to do with the different "columns" Please help explain. Else see #Dave.Gugg's answer which does just that.
SELECT T0.ItemCode,
T0.ItemName,
T0.CardCode,
T0.CodeBars,
T2.UgpCode,
T3.AltQty,
T3.BaseQty,
CASE
WHEN T4.Uomentry = - 1
THEN T0.[BuyUnitMsr]
ELSE t4.UomName
END AS 'UoMName',
T4.UomEntry,
T0.U_CAT_CODE,
T0.U_CAT_NAME,
T1.CardName,
(
SELECT TOP (1) dbo.PDN1.U_AC_QTY_ORDER
FROM dbo.PDN1
INNER JOIN dbo.OPDN ON dbo.PDN1.DocEntry = dbo.OPDN.DocEntry
WHERE (dbo.PDN1.ItemCode = T0.ItemCode)
AND (dbo.OPDN.CardCode = T0.CardCode)
ORDER BY dbo.OPDN.DocDate DESC
) AS OQuantity,
(
SELECT TOP (1) PDN1_1.U_AC_QTY_BONUS
FROM dbo.PDN1 AS PDN1_1
INNER JOIN dbo.OPDN AS OPDN_1 ON PDN1_1.DocEntry = OPDN_1.DocEntry
WHERE (PDN1_1.ItemCode = T0.ItemCode)
AND (OPDN_1.CardCode = T0.CardCode)
ORDER BY OPDN_1.DocDate DESC
) AS BQuantity,
ITM1.Price,
T0.U_DISC_PER
FROM dbo.OITM AS T0
INNER JOIN dbo.OCRD AS T1 ON T0.CardCode = T1.CardCode
INNER JOIN dbo.OUGP AS T2 ON T0.UgpEntry = T2.UgpEntry
INNER JOIN dbo.UGP1 AS T3 ON T2.UgpEntry = T3.UgpEntry
INNER JOIN dbo.ITM1 ON T0.ItemCode = dbo.ITM1.ItemCode
AND dbo.ITM1.PriceList IN ('1', '10')
LEFT JOIN dbo.OUOM AS T4 ON T3.UomEntry = T4.UomEntry
WHERE (T0.Series = '65')
AND (
T4.UomEntry = 3
OR T4.UomEntry = '-1'
)
If you want a different column (this may perform better than two joins):
SELECT T0.ItemCode,
T0.ItemName,
T0.CardCode,
T0.CodeBars,
T2.UgpCode,
T3.AltQty,
T3.BaseQty,
CASE
WHEN T4.Uomentry = - 1
THEN T0.[BuyUnitMsr]
ELSE t4.UomName
END AS 'UoMName',
T4.UomEntry,
T0.U_CAT_CODE,
T0.U_CAT_NAME,
T1.CardName,
(
SELECT TOP (1) dbo.PDN1.U_AC_QTY_ORDER
FROM dbo.PDN1
INNER JOIN dbo.OPDN ON dbo.PDN1.DocEntry = dbo.OPDN.DocEntry
WHERE (dbo.PDN1.ItemCode = T0.ItemCode)
AND (dbo.OPDN.CardCode = T0.CardCode)
ORDER BY dbo.OPDN.DocDate DESC
) AS OQuantity,
(
SELECT TOP (1) PDN1_1.U_AC_QTY_BONUS
FROM dbo.PDN1 AS PDN1_1
INNER JOIN dbo.OPDN AS OPDN_1 ON PDN1_1.DocEntry = OPDN_1.DocEntry
WHERE (PDN1_1.ItemCode = T0.ItemCode)
AND (OPDN_1.CardCode = T0.CardCode)
ORDER BY OPDN_1.DocDate DESC
) AS BQuantity,
CASE
WHEN ITM1.PriceList = '1'
THEN ITM1.Price
ELSE '0'
END AS Price1,
CASE
WHEN ITM1.PriceList = '10'
THEN ITM1.Price
ELSE '0'
END AS Price2,
T0.U_DISC_PER
FROM dbo.OITM AS T0
INNER JOIN dbo.OCRD AS T1 ON T0.CardCode = T1.CardCode
INNER JOIN dbo.OUGP AS T2 ON T0.UgpEntry = T2.UgpEntry
INNER JOIN dbo.UGP1 AS T3 ON T2.UgpEntry = T3.UgpEntry
INNER JOIN dbo.ITM1 ON T0.ItemCode = dbo.ITM1.ItemCode
AND dbo.ITM1.PriceList IN ('1', '10')
LEFT JOIN dbo.OUOM AS T4 ON T3.UomEntry = T4.UomEntry
WHERE (T0.Series = '65')
AND (
T4.UomEntry = 3
OR T4.UomEntry = '-1'
)
You should be able to just join to the table a second time, but you will need to make the joins outer:
SELECT T0.ItemCode ,
T0.ItemName ,
T0.CardCode ,
T0.CodeBars ,
T2.UgpCode ,
T3.AltQty ,
T3.BaseQty ,
CASE WHEN T4.Uomentry = -1 THEN T0.[BuyUnitMsr]
ELSE t4.UomName
END AS 'UoMName' ,
T4.UomEntry ,
T0.U_CAT_CODE ,
T0.U_CAT_NAME ,
T1.CardName ,
( SELECT TOP ( 1 )
dbo.PDN1.U_AC_QTY_ORDER
FROM dbo.PDN1
INNER JOIN dbo.OPDN ON dbo.PDN1.DocEntry = dbo.OPDN.DocEntry
WHERE ( dbo.PDN1.ItemCode = T0.ItemCode )
AND ( dbo.OPDN.CardCode = T0.CardCode )
ORDER BY dbo.OPDN.DocDate DESC
) AS OQuantity ,
( SELECT TOP ( 1 )
PDN1_1.U_AC_QTY_BONUS
FROM dbo.PDN1 AS PDN1_1
INNER JOIN dbo.OPDN AS OPDN_1 ON PDN1_1.DocEntry = OPDN_1.DocEntry
WHERE ( PDN1_1.ItemCode = T0.ItemCode )
AND ( OPDN_1.CardCode = T0.CardCode )
ORDER BY OPDN_1.DocDate DESC
) AS BQuantity ,
dbo.ITM1.Price ,
ITM1Second.Price,
T0.U_DISC_PER
FROM dbo.OITM AS T0
INNER JOIN dbo.OCRD AS T1 ON T0.CardCode = T1.CardCode
INNER JOIN dbo.OUGP AS T2 ON T0.UgpEntry = T2.UgpEntry
INNER JOIN dbo.UGP1 AS T3 ON T2.UgpEntry = T3.UgpEntry
LEFT OUTER JOIN dbo.ITM1 ON T0.ItemCode = dbo.ITM1.ItemCode
AND dbo.ITM1.PriceList = '10'
LEFT OUTER JOIN dbo.ITM1 ITM1Second ON T0.ItemCode = ITM1Second.ItemCode
AND ITM1Second.PriceList = '1'
LEFT OUTER JOIN dbo.OUOM AS T4 ON T3.UomEntry = T4.UomEntry
WHERE ( T0.Series = '65' )
AND ( T4.UomEntry = 3
OR T4.UomEntry = '-1'
)

SQL Add database to all table references

Instead of typing 'NXT_US.dbo.' over and over, how else could I do this?
SELECT DISTINCT
NXT_US.dbo.icsp.prod, NXT_US.dbo.icsp.descrip##1, NXT_US.dbo.icsp.descrip##2, NXT_US.dbo.icsw.whse, NXT_US.dbo.sastn.descrip AS branch, NXT_US.dbo.icsw.qtyonhand, NXT_US.dbo.icsp.unitstock AS UOM, NXT_US.dbo.apsv.vendno AS VendorNumber,
NXT_US.dbo.apsv.name AS VendorName, NXT_US.dbo.icsp.prodcat, NXT_US.dbo.sasta.descrip, NXT_US.dbo.icsw.prodline, NXT_US.dbo.icsw.statustype, NXT_US.dbo.icsw.lastinvdt, NXT_US.dbo.oeel.invoicedt, NXT_US.dbo.icsw.avgcost, NXT_US.dbo.oeel.vendno,
NXT_US.dbo.icsw.avgcost * NXT_US.dbo.icsw.qtyonhand AS ExtCost, NXT_US.dbo.oeel.prodcost, NXT_US.dbo.oeel.orderno
INTO InventoryView
FROM NXT_US.dbo.sasta INNER JOIN
NXT_US.dbo.icsp ON NXT_US.dbo.sasta.codeval = NXT_US.dbo.icsp.prodcat LEFT OUTER JOIN
NXT_US.dbo.icswu ON NXT_US.dbo.icsp.prod = NXT_US.dbo.icswu.prod LEFT OUTER JOIN
NXT_US.dbo.icsw LEFT OUTER JOIN
NXT_US.dbo.oeel ON NXT_US.dbo.icsw.lastinvdt = NXT_US.dbo.oeel.invoicedt AND NXT_US.dbo.icsw.whse = NXT_US.dbo.oeel.whse AND NXT_US.dbo.icsw.prod = NXT_US.dbo.oeel.shipprod RIGHT OUTER JOIN
NXT_US.dbo.sastn RIGHT OUTER JOIN
NXT_US.dbo.icsd ON NXT_US.dbo.sastn.cono = NXT_US.dbo.icsd.cono AND NXT_US.dbo.sastn.codeval = NXT_US.dbo.icsd.divno ON NXT_US.dbo.icsw.whse = NXT_US.dbo.icsd.whse ON NXT_US.dbo.icsp.prod = NXT_US.dbo.icsw.prod LEFT OUTER JOIN
NXT_US.dbo.apsv ON NXT_US.dbo.icsw.arpvendno = NXT_US.dbo.apsv.vendno
WHERE (NXT_US.dbo.sasta.cono = '1') AND (NXT_US.dbo.sasta.codeiden = 'c') AND (NXT_US.dbo.icsw.statustype = 's') AND (NXT_US.dbo.icsp.cono = '1') AND (NXT_US.dbo.sastn.codeiden = 'V') and NXT_US.dbo.icsw.whse = 'G010'
Alias each table the first time you use it in your FROM clause. Then you can use the alias in your SELECT, WHERE, GROUP BY clauses. Here is the example (but use better aliases than the generic ones I put here):
SELECT DISTINCT
t2.prod, t2.descrip##1, t2.descrip##2, t4.whse, t6.descrip AS branch, t4.qtyonhand, t2.unitstock AS UOM, t8.vendno AS VendorNumber,
t8.name AS VendorName, t2.prodcat, t1.descrip, t4.prodline, t4.statustype, t4.lastinvdt, t5.invoicedt, t4.avgcost, t5.vendno,
t4.avgcost * t4.qtyonhand AS ExtCost, t5.prodcost, t5.orderno
INTO InventoryView
FROM NXT_US.dbo.sasta AS t1 INNER JOIN
NXT_US.dbo.icsp AS t2 ON t1.codeval = t2.prodcat LEFT OUTER JOIN
NXT_US.dbo.icswu AS t3 ON t2.prod = t3.prod LEFT OUTER JOIN
NXT_US.dbo.icsw AS t4 LEFT OUTER JOIN
NXT_US.dbo.oeel AS t5 ON t4.lastinvdt = t5.invoicedt AND t4.whse = t5.whse AND t4.prod = t5.shipprod RIGHT OUTER JOIN
NXT_US.dbo.sastn AS t6 RIGHT OUTER JOIN
NXT_US.dbo.icsd AS t7 ON t6.cono = t7.cono AND t6.codeval = t7.divno ON t4.whse = t7.whse ON t2.prod = t4.prod LEFT OUTER JOIN
NXT_US.dbo.apsv AS t8 ON t4.arpvendno = t8.vendno
WHERE (t1.cono = '1') AND (t1.codeiden = 'c') AND (t4.statustype = 's') AND (t2.cono = '1') AND (t6.codeiden = 'V') and t4.whse = 'G010'