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

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

Related

How to fix SQL query to Left Join a subquery with Where clause?

I'm new to SQL and I'm not certain why I am getting this error. I am trying to left join a sub-query to another query in sql developer.
This is the first query,
SELECT DISTINCT
tl.species,
ag.age
FROM
age_list ag,
tree_list tl
WHERE
ag.tree_id = tl.tree_id
And then the sub-query I would like to left join where the tree_id = tree_number is,
SELECT DISTINCT
sl.tree_spon,
sl.tree_number
FROM spon_list sl
WHERE
sl.tree_spon < 10
When trying to do this I've tried to use,
SELECT DISTINCT
tl.species,
ag.age,
q1.tree_spon
FROM
age_list ag,
tree_list tl
LEFT OUTER JOIN (SELECT DISTINCT
sl.tree_spon,
sl.tree_number
FROM spon_list sl
WHERE sl.tree_spon < 10) q1 on q1.tree_number = tree_list.tree_id
WHERE
ag.tree_id = tl.tree_id
Whatever I change in terms of the alias' for the columns and tables I always get the error, "ORA-00904: invalid identifier error", and that "tree_list.tree_id is invalid identifier", though separately the queries run fine.
Can anyone help, is it an issue with both queries joining on the tl.tree_id?
You can use the ANSI join syntax throughout (rather than mixing in legacy comma joins), joining on ag.tree_id = sl.tree_number (or tl.tree_id = sl.tree_number but they're both equal given the previous join) and putting the filter on sl.tree_spon < 10 into the ON clause as well:
SELECT DISTINCT
tl.species,
ag.age,
sl.tree_spon,
sl.tree_number
FROM age_list ag
INNER JOIN tree_list tl
ON (ag.tree_id = tl.tree_id)
LEFT OUTER JOIN spon_list sl
ON (ag.tree_id = sl.tree_number AND sl.tree_spon < 10)
Change tree_list.tree_id to tl.tree_id

take dynamic table name for inner join in SQL Query

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?

How do I fix the syntax of a sub query with joins?

I have the following query:
SELECT tours_atp.NAME_T, today_atp.TOUR, today_atp.ID1, odds_atp.K1, today_atp.ID2, odds_atp.K2
FROM (players_atp INNER JOIN (players_atp AS players_atp_1 INNER JOIN (today_atp INNER JOIN odds_atp ON (today_atp.TOUR = odds_atp.ID_T_O) AND (today_atp.ID1 = odds_atp.ID1_O) AND (today_atp.ID2 = odds_atp.ID2_O) AND (today_atp.ROUND = odds_atp.ID_R_O)) ON players_atp_1.ID_P = today_atp.ID2) ON players_atp.ID_P = today_atp.ID1) INNER JOIN tours_atp ON today_atp.TOUR = tours_atp.ID_T
WHERE (((tours_atp.RANK_T) Between 1 And 4) AND ((today_atp.RESULT)="") AND ((players_atp.NAME_P) Not Like "*/*") AND ((players_atp_1.NAME_P) Not Like "*/*") AND ((odds_atp.ID_B_O)=2))
ORDER BY tours_atp.NAME_T;
I'd like to add a field to this query that provides me with the sum of a field in another table (FS) with a few criteria applied.
I've been able to build a stand alone query to get the sum of FS by ID_T as follows:
SELECT tbl_Ts_base_atp.ID_T, Sum(tbl_Ts_mkv_atp.FS) AS SumOfFS
FROM tbl_Ts_base_atp INNER JOIN tbl_Ts_mkv_atp ON tbl_Ts_base_atp.ID_Ts = tbl_Ts_mkv_atp.ID_Ts
WHERE (((tbl_Ts_base_atp.DATE_T)>Date()-2000 And (tbl_Ts_base_atp.DATE_T)<Date()))
GROUP BY tbl_Ts_base_atp.ID_T, tbl_Ts_mkv_atp.ID_Ts;
I now want to match up the sum of FS from the second query to the records of the first query by ID_T. I realise I need to do this using a sub query. I'm confident using these when there's only one table but I consistently get 'syntax errors' when there are joins.
I simplified the first query down to remove all the WHERE conditions so it was easier for me to try and error check but no luck. I guess the resulting SQL will also be easier for you guys to follow:
SELECT today_atp.TOUR, (SELECT Sum(tbl_Ts_mkv_atp.FS)
FROM tbl_Ts_mkv_atp INNER JOIN (tbl_Ts_base_atp INNER JOIN today_atp ON tbl_Ts_base_atp.ID_T = today_atp.TOUR) ON tbl_Ts_mkv_atp.ID_Ts = tbl_Ts_base_atp.ID_Ts AS tt
WHERE tt.DATE_T>Date()-2000 And tt.DATE_T<Date() AND tt.TOUR=today_atp.TOUR
ORDER BY tt.DATE_T) AS SumOfFS
FROM today_atp
Can you spot where I'm going wrong? My hunch is that the issue is in the FROM line of the sub query but I'm not sure. Thanks in advance.
It's difficult to advise an appropriate solution without knowledge of how the database tables relate to one another, but assuming that I've correctly understood what you are looking to achieve, you might wish to try the following solution:
select
tours_atp.name_t,
today_atp.tour,
today_atp.id1,
odds_atp.k1,
today_atp.id2,
odds_atp.k2,
subq.sumoffs
from
(
(
(
(
today_atp inner join odds_atp on
today_atp.tour = odds_atp.id_t_o and
today_atp.id1 = odds_atp.id1_o and
today_atp.id2 = odds_atp.id2_o and
today_atp.round = odds_atp.id_r_o
)
inner join players_atp as players_atp_1 on
players_atp_1.id_p = today_atp.id2
)
inner join players_atp on
players_atp.id_p = today_atp.id1
)
inner join tours_atp on
today_atp.tour = tours_atp.id_t
)
inner join
(
select
tbl_ts_base_atp.id_t,
sum(tbl_ts_mkv_atp.fs) as sumoffs
from
tbl_ts_base_atp inner join tbl_ts_mkv_atp on
tbl_ts_base_atp.id_ts = tbl_ts_mkv_atp.id_ts
where
tbl_ts_base_atp.date_t > date()-2000 and tbl_ts_base_atp.date_t < date()
group by
tbl_ts_base_atp.id_t
) subq on
tours_atp.tour = subq.id_t
where
(tours_atp.rank_t between 1 and 4) and
today_atp.result = "" and
players_atp.name_p not like "*/*" and
players_atp_1.name_p not like "*/*" and
odds_atp.id_b_o = 2
order by
tours_atp.name_t;

Having trouble with microsoft query syntax

I wrote a SQL query that works, but when I try to translate it to query on Microsoft query I'm running into a lot of problems syntactically.
This is the query I wrote originally:
select
sum(PJC_FCTR_VL)
from
(select
max(PJC_FCTR_VL)
from
(select
R_CATEGORY_70075,
PRDC_KEY
from PRDC_DIM
where R_CATEGORY_70075 like '%RAMEN%')sub,
HS_PNLS_PJC_SBST_20150404 t1,
HSHLD_DIM t2,
HS_PRCH_SBST_20150404 t3
where
t1.PNLS_TYP_ID = 1
and t3.HSHLD_ID = t2.HSHLD_ID
and sub.PRDC_KEY = t3.PRDC_KEY
group by
t1.HSHLD_ID) subqry
I tried to break this query down more to see where I'm messing up on the Microsoft query and this is what I've got:
select
max(HS_PNLS_PJC_SBST_20150404.PJC_FCTR_VL)
from
{
PRDC_DIM.R_CATEGORY_70075, PRDC_DIM.PRDC_KEY
FROM AODR2QPNLSFWPR.ADMIN.PRDC_DIM PRDC_DIM
WHERE (PRDC_DIM.R_CATEGORY_70075 Like '%RAMEN%')} HS_PNLS_PJC_SBST_20150404
I end up getting an error that says "expected OJ after {" even though I'm not really doing an outer join. I was wondering if someone could help me edit the SQL so that it would satisfy the Microsoft syntax.
Try something like.....
select sum(Max_PJC_FCTR_VL)
from
( select t1.HSHLD_ID, max(PJC_FCTR_VL) AS Max_PJC_FCTR_VL --<-- use two part name here
from ( select R_CATEGORY_70075,
PRDC_KEY
from PRDC_DIM
where R_CATEGORY_70075 like '%RAMEN%'
)sub
INNER JOIN HS_PRCH_SBST_20150404 t3 ON sub.PRDC_KEY = t3.PRDC_KEY
INNER JOIN HSHLD_DIM t2 ON t3.HSHLD_ID = t2.HSHLD_ID
INNER JOIN HS_PNLS_PJC_SBST_20150404 t1 ON t1.HSHLD_ID = t2.HSHLD_ID --<-- only guessing here
AND t1.PNLS_TYP_ID = 1
group by t1.HSHLD_ID
) subqry

JOIN Nested DISTINCT

I am writing SQL statements with MS Access and I am wondering how I can nest a SELECT DISTINCT statement in my JOIN?
At the moment I am writing 2 queries to get an output (2 steps):
1st query is a simple DISTINCT statement.
2nd query is a JOIN on the query created in 1.
How can I nest a DISTINCT statement in the join in order to perform the action in a single step?
SELECT DISTINCT tblFinalIssuerNames_ReverseRepos.ISIN, tblFinalIssuerNames_ReverseRepos.IssuerCode
FROM tblFinalIssuerNames_ReverseRepos;
SELECT QSel_CollateralReposII.ISIN, QSel_CollateralReposII.MarketValueUSD, QSelDistinctISINs.IssuerCode
FROM QSel_CollateralReposII INNER JOIN QSelDistinctISINs ON QSel_CollateralReposII.ISIN = QSelDistinctISINs.ISIN;
I was thinking about something like the below but the syntax is wrong…
SELECT QSel_CollateralReposII.ISIN, QSel_CollateralReposII.MarketValueUSD, tblFinalIssuerNames_ReverseRepos.IssuerCode
FROM QSel_CollateralReposII INNER JOIN tblFinalIssuerNames_ReverseRepos ON SELECT DISTINCT QSel_CollateralReposII.ISIN = tblFinalIssuerNames_ReverseRepos.ISIN;
I can't test what is allowed and what is not in Access but you can replace QSelDistinctISINs in your query with the query that defines it, by just putting it inside parenthesis and giving it an alias - d below. This is valid SQL syntax:
SELECT
r.ISIN,
r.MarketValueUSD,
d.IssuerCode
FROM
QSel_CollateralReposII r
INNER JOIN
( SELECT DISTINCT
ISIN,
IssuerCode
FROM
tblFinalIssuerNames_ReverseRepos
) d
ON r.ISIN = d.ISIN ;