take dynamic table name for inner join in SQL Query - sql

My SQL Query is,
SELECT EAL_TYPE,
EAL_ID,
ROW_NUMBER ()OVER(PARTITION BY EAL_TYPE,EAL_ID ORDER BY EAL_TYPE,EAL_ID,EAL_ACTION_TIME Desc)SL#,
EAL_STATUS,
EAL_ACTION_TYPE,
EAL_CMPCODE,
ESSET_Description,
EAL_SHOW_IN_ALERT,
ESSETRS_WEIGHTAGE,
Usr_UserRole,ESSET_HeaderTable_Prefix,ESSET_HeaderTable
FROM ESS_ACTION_LOG
LEFT JOIN ESS_ENTRYTYPE_MASTER ON
ESSET_ID=EAL_TYPE
LEFT JOIN User_Profile ON
Usr_LoginID='MK'
LEFT JOIN ESS_ENTRYTYPE_ROLE_SETTINGS ON
ESSETRS_CMPCODE = EAL_CMPCODE AND
ESSETRS_ID = EAL_TYPE AND
ESSETRS_ROLE_ID = Usr_UserRole
INNER JOIN ESSET_HeaderTable ON
ESSET_HeaderTable_Prefix+'_CMPCode'=EAL_CMPCODE AND
ESSET_HeaderTable_Prefix+'_Type'=EAL_TYPE AND
ESSET_HeaderTable_Prefix+'_ID'=EAL_ID
WHERE ESSETRS_WEIGHTAGE IS NOT NULL
In the above query, while executing it shows an error like 'ESSET_HeaderTable object name not found'. But that table name needs to come dynamically from another table. What's wrong with my query. Can anyone help me with this?

Related

Joining a Query and sub-query

I'm trying to join a query and a sub-query in Access but haven't the faintest idea on how to do that. Rather than saving the sub-query as a different query and then joining it to the main query.
Main query (with reference to sub-query):
SELECT tb200_IraDataIn.tb200_niarnum, tb206_IraAccount.tb206_IraAccDesc,
tb206_IraAccount.tb206_IraAccNum, tb15_Securities.tb15_IsActiveRegister,
tb15_Securities.tb15_NiarDesc, tb15_Securities.tb15_ManpikID, tb200_IraDataIn.tb200_Shovi, tb10_Afik.tb10_InvestTool,
tb206_IraAccount.tb206_IsActive, tb200_IraDataIn.tb200_Shovi,
tb200_IraDataIn.tb200_Shovi/SubQuery1.SumOftb200_Shovi AS Expr1
FROM SubQuery1
INNER JOIN (tb10_Afik
INNER JOIN (tb15_Securities
INNER JOIN (tb206_IraAccount
INNER JOIN tb200_IraDataIn
ON tb206_IraAccount.tb206_IraAccNum = tb200_IraDataIn.tb200_accountNumber)
ON tb15_Securities.tb15_NiarID = tb200_IraDataIn.tb200_niarnum)
ON (tb10_Afik.tb10_AfikID = tb200_IraDataIn.tb200_afik) AND (tb10_Afik.tb10_Erp =tb200_IraDataIn.tb200_ERP))
ON SubQuery1.tb206_IraAccNum = tb206_IraAccount.tb206_IraAccNum
WHERE (((tb15_Securities.tb15_IsActiveRegister)=Yes) AND ((tb10_Afik.tb10_InvestTool)=1
Or (tb10_Afik.tb10_InvestTool)=4 Or (tb10_Afik.tb10_InvestTool)=21 Or
(tb10_Afik.tb10_InvestTool)=3) AND ((tb206_IraAccount.tb206_IsActive)=Yes) AND
(([tb200_IraDataIn].[tb200_Shovi]/[SubQuery1].[SumOftb200_Shovi])>0.1));
subquery (saved as SubQuery1):
SELECT tb206_IraAccount.tb206_IraAccDesc, tb206_IraAccount.tb206_IraAccNum, Sum(tb200_IraDataIn.tb200_Shovi) AS SumOftb200_Shovi
FROM tb206_IraAccount
INNER JOIN tb200_IraDataIn ON tb206_IraAccount.tb206_IraAccNum = tb200_IraDataIn.tb200_accountNumber
WHERE (((tb206_IraAccount.tb206_IsActive)=Yes))
GROUP BY tb206_IraAccount.tb206_IraAccDesc, tb206_IraAccount.tb206_IraAccNum;
How can I put the sql statement of the sub-query inside the same Sql statement of the main query, unlike now?
Thanks!
Nest between parentheses. Nothing else in the main query changes.
SELECT
...
FROM (SELECT tb206_IraAccount.tb206_IraAccDesc, tb206_IraAccount.tb206_IraAccNum, Sum(tb200_IraDataIn.tb200_Shovi) AS SumOftb200_Shovi
FROM tb206_IraAccount
INNER JOIN tb200_IraDataIn ON tb206_IraAccount.tb206_IraAccNum = tb200_IraDataIn.tb200_accountNumber
WHERE (((tb206_IraAccount.tb206_IsActive)=Yes))
GROUP BY tb206_IraAccount.tb206_IraAccDesc, tb206_IraAccount.tb206_IraAccNum) AS SubQuery1
...

Multiple join with SQL

I have a problem with my SQL query.
I want to have a multiple join, but the error is not helpful.
The following is my query:
SELECT bn_ms_bm_bankmaster.CMP_CUSTCODE AS Id_client
,BN_CS_MP_MASTERPROFILE.CMP_NAME AS Nom_prenom
,PR_GN_AD_ENTITYADDRESS.PMP_MUNCIPCODE
FROM bn_ms_bm_bankmaster
INNER JOIN BN_CS_MP_MASTERPROFILE ON bn_ms_bm_bankmaster.CMP_CUSTCODE = BN_CS_MP_MASTERPROFILE.CMP_CUSTCODE
INNER JOIN PR_GN_AD_ENTITYADDRESS ON bn_ms_bm_bankmaster.CMP_CUSTCODE = PR_GN_AD_ENTITYADDRESS.CMP_CUSTCODE
This query give back to me the flowing error:
Impossible to add table bn_ms_bm_bankmaster with Microsoft Query
Any help will welcome.
Can you try with below.
select * from(Select bn_ms_bm_bankmaster.CMP_CUSTCODE AS Id_client,BN_CS_MP_MASTERPROFILE.CMP_NAME as Nom_prenom,PR_GN_AD_ENTITYADDRESS.PMP_MUNCIPCODE FROM bn_ms_bm_bankmaster INNER JOIN BN_CS_MP_MASTERPROFILE ON bn_ms_bm_bankmaster.CMP_CUSTCODE=BN_CS_MP_MASTERPROFILE.CMP_CUSTCODEINNER JOIN PR_GN_AD_ENTITYADDRESS ON bn_ms_bm_bankmaster.CMP_CUSTCODE=PR_GN_AD_ENTITYADDRESS.CMP_CUSTCODE);
Please try this.
Select A.CMP_CUSTCODE AS Id_client,
B.CMP_NAME as Nom_prenom,
C.PMP_MUNCIPCODE
FROM bn_ms_bm_bankmaster AS A
INNER JOIN BN_CS_MP_MASTERPROFILE AS B ON A.CMP_CUSTCODE= B.CMP_CUSTCODE
INNER JOIN PR_GN_AD_ENTITYADDRESS AS C ON B.CMP_CUSTCODE = C.CMP_CUSTCODE

Add values of one column

I am trying to get a record by adding values of one column based on key value. Here is the query I have:
SELECT
PM_ProductPayment.ProjectId, SalesDetail.SalesPerson,
PM_ProductCost.ProductCost, dbo.PM_ProductPayment.ProductPayment,
dbo.PM_ProductPayment.PaymentDate
FROM
dbo.PM_ProductPayment
INNER JOIN
dbo.PM_Product ON dbo.PM_ProductPayment.ProjectId = dbo.PM_Product.ProductId
INNER JOIN
dbo.PM_ProductCost ON dbo.PM_ProductPayment.ProductId = dbo.PM_ProductCost.ProductId
INNER JOIN
dbo.SalesDetail ON dbo.PM_Product.SalesPersonId = dbo.SalesDetail.ID
The result I am getting is:
Now, here I want to get single row by adding "Payment" for each product and last payment date.
Please optimize my query or suggest any other better way to do that..
Thanks,
SELECT pay.ProjectId,
sum(pay.ProductPayment),
max(pay.PaymentDate)
FROM dbo.PM_ProductPayment pay
INNER JOIN dbo.PM_Product pro ON pay.ProjectId = pro.ProductId
INNER JOIN dbo.PM_ProductCost cos ON pay.ProductId =cos.ProductId
INNER JOIN dbo.SalesDetail sal ON pro.SalesPersonId = sal.ID
GROUP BY pay.ProjectId

Ambiguous outer join in MS Access

Trying to create an outer join on two other joined tables when recieving this error - I just dont see how to create two separate queries to make it work. Subqueries don't seem to work either, any help appreciated. I get errors for the below query, thanks.
SELECT
CardHeader.CardID, CardHeader.CardDescription, CardHeader.GloveSize,
CardHeader.GloveDescription, CardDetail.Bin, CardDetail.ItemID, Items.ItemDescription,
Items.VCatalogID, CardDetail.ChargeCode, CardDetail.Quantity, Items.Cost, CardColors.ColorID
FROM
((Items
INNER JOIN
(CardHeader INNER JOIN CardDetail ON CardHeader.CardID = CardDetail.CardID) ON Items.ItemID = CardDetail.ItemID)
LEFT JOIN
CardColors ON CardDetail.ItemID = CardColors.ItemID)
INNER JOIN
Colors ON CardColors.ColorID = Colors.ID
ORDER BY
CardHeader.CardID;
I tried the following which runs but asks for the following parameters (which it shouldnt)
CardHeader.ID, MainQry.CardID
SELECT
MainQry.ID, MainQry.CardDescription, MainQry.GloveSize,
MainQry.GloveDescription, MainQry.Bin, MainQry.ItemID,
MainQry.ItemDescription, MainQry.VCatalogID, MainQry.ChargeCode,
MainQry.Quantity, MainQry.Cost, SubQry.ColorID
FROM
(SELECT
CardHeader.ID, CardHeader.CardDescription, CardHeader.GloveSize,
CardHeader.GloveDescription, CardDetail.Bin,
CardDetail.ItemID, Items.ItemDescription, Items.VCatalogID,
CardDetail.ChargeCode, CardDetail.Quantity, Items.Cost
FROM
Items
INNER JOIN
(CardHeader
INNER JOIN
CardDetail ON CardHeader.CardID = CardDetail.CardID) ON Items.ItemID = CardDetail.ItemID
) AS MainQry
LEFT JOIN
(SELECT
CardColors.ItemID, CardColors.ColorID
FROM
CardColors
INNER JOIN
Colors ON CardColors.ColorID = Colors.ID) AS SubQry ON MainQry.ItemID = SubQry.ItemID
ORDER BY
MainQry.CardID;
The second SQL statement can be corrected by reference to the first statement and the error. The error is that both CardHeader.ID and MainQry.CardID are prompting for a parameter, which indicates that the inner statement should include CardHeader.CardID, rather than CardHeader.ID

SQL query works in SQL Server, fails in Excel (Microsoft Query)

I have the following query which works as intended :
SELECT
SERVICE_HISTORY.ServiceMode, SERVICE_HISTORY.CreatedDate,
SERVICE_HISTORY.CreatedBy, SERVICE_HISTORY.Branch,
SERVICE_HISTORY.Comments
FROM
DEBA_US.dbo.SERVICE_HISTORY
JOIN
(SELECT MAX(SERVICE_HISTORY.CreatedDate) AS maxDate, CUSTOMER.AccNo
FROM DEBA_US.dbo.CUSTOMER
INNER JOIN (DEBA_US.dbo.SERVICE_HISTORY
INNER JOIN DEBA_US.dbo.CAR ON SERVICE_HISTORY.ROW_PK = CAR.ROW_PK) ON CUSTOMER.ROW_PK = CAR.ROW_PK
WHERE
CUSTOMER.AccNo LIKE 'CUS-1234'
AND CAR.DateSubmitted IS NULL
GROUP BY
CUSTOMER.AccNo) AS testQuery ON testQuery.maxDate = SERVICE_HISTORY.CreatedDate
The query is to gives me the latest (max) service history date for a given customer.
When I execute the query in SQL Server, it works perfectly fine, but when I put the same query into EXCEL 2010 (Microsoft Query) it give me the error:
No Column name was specified for Column 1 of 'testQuery'
Invalid column name 'maxDate'
Statement could not be prepared
I'm not able to fix the query to get pass the error. Can someone please tell me why Excel isn't working with the above query? Thanks
You need to put testQuery and maxDate inside single quotations
SELECT
SERVICE_HISTORY.ServiceMode, SERVICE_HISTORY.CreatedDate,
SERVICE_HISTORY.CreatedBy, SERVICE_HISTORY.Branch,
SERVICE_HISTORY.Comments
FROM
DEBA_US.dbo.SERVICE_HISTORY
JOIN
(SELECT MAX(SERVICE_HISTORY.CreatedDate) AS 'maxDate', CUSTOMER.AccNo
FROM DEBA_US.dbo.CUSTOMER
INNER JOIN (DEBA_US.dbo.SERVICE_HISTORY
INNER JOIN DEBA_US.dbo.CAR ON SERVICE_HISTORY.ROW_PK = CAR.ROW_PK) ON CUSTOMER.ROW_PK = CAR.ROW_PK
WHERE
CUSTOMER.AccNo LIKE 'CUS-1234'
AND CAR.DateSubmitted IS NULL
GROUP BY
CUSTOMER.AccNo) AS 'testQuery' ON testQuery.maxDate = SERVICE_HISTORY.CreatedDate
The only thing you need to do is to add square brackets around the maxDate like following:
SELECT
SERVICE_HISTORY.ServiceMode, SERVICE_HISTORY.CreatedDate,
SERVICE_HISTORY.CreatedBy, SERVICE_HISTORY.Branch,
SERVICE_HISTORY.Comments
FROM
DEBA_US.dbo.SERVICE_HISTORY
JOIN
(SELECT MAX(SERVICE_HISTORY.CreatedDate) AS [maxDate], CUSTOMER.AccNo
FROM DEBA_US.dbo.CUSTOMER
INNER JOIN (DEBA_US.dbo.SERVICE_HISTORY
INNER JOIN DEBA_US.dbo.CAR ON SERVICE_HISTORY.ROW_PK = CAR.ROW_PK) ON CUSTOMER.ROW_PK = CAR.ROW_PK
WHERE
CUSTOMER.AccNo LIKE 'CUS-1234'
AND CAR.DateSubmitted IS NULL
GROUP BY
CUSTOMER.AccNo) AS testQuery ON testQuery.maxDate = SERVICE_HISTORY.CreatedDate