JOIN Nested DISTINCT - sql

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 ;

Related

How to convert inline SQL queries to JOINS in SQL SERVER to reduce load time

I need help in optimizing this SQL query.
In the main SELECT statement there are three columns which is dependent on the outer query result. This is why my query is taking a long time to return data. I have tried making left joins but this is not working properly.
Can anyone help me to resolve this issue?
SELECT
DISTINCT ou.OrganizationUserID AS StudentID,
ou.FirstName,
ou.LastName,
(
SELECT
STRING_AGG(
(ug.UG_Name),
','
)
FROM
Groups ug
INNER JOIN ApplicantUserGroup augm ON augm.AUGM_UserGroupID = ug.UG_ID
WHERE
augm.AUGM_OrganizationUserID = ou.OrganizationUserID
AND ug.UG_IsDeleted = 0
AND augm.AUGM_IsDeleted = 0
) AS UserGroups,
order1.OrderNumber AS OrderId -- UAT-2455
,
(
SELECT
STRING_AGG(
(CActe.CustomAttribute),
','
)
FROM
CustomAttributeCte CActe
WHERE
CActe.HierarchyNodeID = dpm.DPM_ID
AND CActe.OrganizationUserID = ps.OrganizationUserID
) AS CustomAttributes -- UAT-2455
,
(
SELECT
STRING_AGG(
(CActe.CustomAttributeID),
','
)
FROM
CustomAttributeCte CActe
WHERE
CActe.HierarchyNodeID = dpm.DPM_ID
AND CActe.OrganizationUserID = ps.OrganizationUserID
) AS CustomAttributeID
FROM
ApplicantData acd WITH (NOLOCK)
INNER JOIN ClientPackage ps WITH (NOLOCK) ON acd.ClientSubscriptionID = ps.ClientSubscriptionID
INNER JOIN [ClientOrder] order1 WITH (NOLOCK) ON order1.OrderID = ps.OrderID
AND order1.IsDeleted = 0
INNER JOIN OUser ou WITH (NOLOCK) ON ou.OrganizationUserID = ps.OrganizationUserID
It looks like this query can be simplified, and the dependent subqueries in your SELECT clause removed, Consider your second and third dependent subqueries. You can refactor them into one nondependent subquery with a LEFT JOIN. Using nondependent subqueries is more efficient because the query planner can run them just once, rather than once for each row.
You want two STRING_AGG() results from the same table. This subquery gives those two outputs for every possible combination of HierarchyNodeID and OrganizationUserID values. STRING_AGG() is an aggregate function like SUM() and so works nicely with GROUP BY.
SELECT HierarchyNodeID, OrganizationUserID,
STRING_AGG((CActe.CustomAttribute), ',') CustomAttributes -- UAT-2455,
STRING_AGG((CActe.CustomAttributeID), ',') CustomAttributeIDs -- UAT-2455
FROM CustomAttributeCte CActe
GROUP BY HierarchyNodeID, OrganizationUserID
You can run this subquery itself to convince yourself it works.
Now, we can LEFT JOIN that into your query. Like this. (For readability I took out the NOLOCKs and used JOIN: it means the same thing as INNER JOIN.)
SELECT DISTINCT
ou.OrganizationUserID AS StudentID,
ou.FirstName,
ou.LastName,
'tempvalue' AS UserGroups, -- shortened for testing
order1.OrderNumber AS OrderId, -- UAT-2455
uat2455.CustomAttributes, -- UAT-2455
uat2455.CustomAttributeIDs -- UAT-2455
FROM ApplicantData acd
JOIN ClientPackage ps
ON acd.ClientSubscriptionID = ps.ClientSubscriptionID
JOIN ClientOrder order1
ON order1.OrderID = ps.OrderID
AND order1.IsDeleted = 0
JOIN OUser ou
ON ou.OrganizationUserID = ps.OrganizationUserID
LEFT JOIN (
SELECT HierarchyNodeID, OrganizationUserID,
STRING_AGG((CActe.CustomAttribute), ',') CustomAttributes -- UAT-2455,
STRING_AGG((CActe.CustomAttributeID), ',') CustomAttributeIDs -- UAT-2455
FROM CustomAttributeCte CActe
GROUP BY HierarchyNodeID, OrganizationUserID
) uat2455
ON uat2455.HierarchyNodeID = dpm.DPM_ID
AND uat2455.OrganizationUserId = ps.OrganizationUserID
See how we collapsed your second and third dependent subqueries to just one, then used it as a virtual table with LEFT JOIN? We transformed the WHERE clauses from the dependent subqueries into an ON clause.
You can test this: run it with TOP(50) and eyeball the results.
When you're happy, the next step is to transform your first dependent subquery the same way.
Pro tip Don't use WITH (NOLOCK), ever, unless a database administration expert tells you to after looking at your specific query. If your query's purpose is a historical report and you don't care whether the most recent transactions in your database are represented exactly right, you can precede your query with this statement. It also allows the query to run while avoiding locks.
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
Pro tip Be obsessive about formatting your queries for readability. You, your colleagues, and yourself a year from now must be able to read and reason about queries like this.

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
...

postgresql: join syntax error when joining 2 select statements

I am querying the aact database from clinicaltrials.gov. The database model is right here: https://aact.ctti-clinicaltrials.org/schema. I have two schemas that I am selecting from (ctgov, proj_cdek_standard_orgs). I am trying to join two select statements. edit: I have now tried aliasing my subqueries, but that still does nothing. I get the following error:
(SELECT ctgov.sponsors.name, ctgov.sponsors.nct_id, ctgov.sponsors.id, ctgov.studies.phase
FROM ctgov.sponsors, ctgov.studies
WHERE ctgov.sponsors.nct_id=ctgov.studies.nct_id) A
FULL [OUTER] JOIN
(SELECT proj_cdek_standard_orgs.cdek_synonyms.id, proj_cdek_standard_orgs.cdek_synonyms.name
FROM proj_cdek_standard_orgs.cdek_synonyms) B
ON
A.name = B.name;
I can do both select statements perfectly fine on their own, but I try the query and I get this error:
ERROR: syntax error at or near "t1" LINE 7: ) t1
What did I do wrong and how do I use joins without getting syntax errors?
Please use below query,
SELECT ctgov.sponsors.name, ctgov.sponsors.nct_id, ctgov.sponsors.id,
ctgov.studies.phase, proj_cdek_standard_orgs.cdek_synonyms.id,
proj_cdek_standard_orgs.cdek_synonyms.name
FROM ctgov.sponsors, ctgov.studies, proj_cdek_standard_orgs.cdek_synonyms
WHERE ctgov.sponsors.nct_id=ctgov.studies.nct_id
and proj_cdek_standard_orgs.cdek_synonyms.name = ctgov.sponsors.name;
But the right way is to use traditional joins,
SELECT ctgov.sponsors.name, ctgov.sponsors.nct_id, ctgov.sponsors.id,
ctgov.studies.phase, proj_cdek_standard_orgs.cdek_synonyms.id,
proj_cdek_standard_orgs.cdek_synonyms.name
FROM ctgov.sponsors
INNER JOIN ctgov.studies
ON (ctgov.sponsors.nct_id=ctgov.studies.nct_id)
INNER JOIN proj_cdek_standard_orgs.cdek_synonyms
ON (proj_cdek_standard_orgs.cdek_synonyms.name = ctgov.sponsors.name);
You can change it to LEFT or FULL OUTER JOIN according to your requirement.
You have to provide an alias to the sub-queries. Also you should not use implicit joins as you have used in first subquery, always try to use explicit joins.
SELECT
*
FROM
(
SELECT
ctgov.sponsors.name, ctgov.sponsors.nct_id, ctgov.sponsors.id, ctgov.studies.phase
FROM ctgov.sponsors
JOIN ctgov.studies
ON ctgov.sponsors.nct_id=ctgov.studies.nct_id
) t1
FULL JOIN
(
SELECT
proj_cdek_standard_orgs.cdek_synonyms.id, proj_cdek_standard_orgs.cdek_synonyms.name
FROM proj_cdek_standard_orgs.cdek_synonyms
) t2
ON
t1.name = t2.name;

HQL 'With statement' cause an additional join

The HQL With statement produce strange behavior. I expect the following query:
select
s.id, s.nom,s.dateDebutPublication,s.dateFinPublication,r.codeUtilisateur
from
Sondage s left outer join
s.repondant r with r.codeUtilisateur=:utilisateur
to produce this sql request:
select ...
from
SON_SONDG sondage0_
left outer join
SON_REPND repondant1_
on sondage0_.SEQ_SONDG=repondant1_.SEQ_SONDG
and (
repondant1_.CODE_UTILS=?
)
instead, it create an extra join, which return way more lines
left outer join
SON_REPND repondant1_
on sondage0_.SEQ_SONDG=repondant1_.SEQ_SONDG
left outer join
SON_REPND repondant2_
on repondant1_.SEQ_REPND=repondant2_.SEQ_REPND
and (
repondant2_.CODE_UTILS=?
)
what would be the correct way to write de HQL query so it return my expected sql query?

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