I am beginner in SQL trying to group by a result
SELECT
API.PROJECTNUMBER, Project.ProjectName, Project.PROJMGRID, Staff.Email
INTO
#TEMPUnmatchedProjectWithSMoD
FROM
{ProjectsIncidentsIntegration} API WITH(NOLOCK)
JOIN
{Project} project WITH(NOLOCK) ON Project.ProjectNumber = API.PROJECTNUMBER
JOIN
{COMMON_STAFF} Staff WITH(NOLOCK) ON Staff.EmplId = Project.PROJMGRID
WHERE
NOT EXISTS (SELECT 1
FROM #DBName.DBO.NCompasS_TBL_SMoDIncident SMoD WITH(NOLOCK)
WHERE SMoD.ProjectCode = API.PROJECTNUMBER)
SELECT *
FROM #TEMPUnmatchedProjectWithSMoD
GROUP BY Project.PROJMGRID
I get an error:
Database returned the following error:
Error in advanced query UnmatchedProjectWithSMoD2: The multi-part identifier "Project.PROJMGRID" could not be bound.
The problem appears to be in this query:
SELECT *
FROM #TEMPUnmatchedProjectWithSMoD
GROUP BY Project.PROJMGRID ;
There are two major issues:
SELECT * with GROUP BY is not correct. What should be done with the columns that are not aggregation keys? You might have some idea. SQL generates an error.
Project is not defined.
It is quite unclear what this code is supposed to be doing, so I cannot suggest anything useful. You can just select all rows from the temporary table using:
SELECT p.*
FROM #TEMPUnmatchedProjectWithSMoD p;
Related
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;
Using MariaDB
I am trying to update the membershipRenewDate(In officiantsDetails Table) using the MAX value from renewDate(in officiantsRenewals Table)
Inner Join on officiant_id (same column name for both tables)
I have something like this but am getting syntax error.
UPDATE officiantsDetails offd
SET offd.membershipRenewDate = offr.renewDate
FROM (SELECT TOP (1) renewDate, officiant_id FROM officiantsRenewals ORDER BY renewDate DESC ) as offr
WHERE offd.officiant_id = offr.officiant_id
You should be able to use a Common Table Expression (CTE) for this. You use the WITH clause to define your "subquery", and then INNER JOIN the CTE to the table you want to update. It would look something like this:
WITH Top1RenewDate AS
(
SELECT TOP (1)
renewDate,
officiant_id
FROM officiantsRenewals
ORDER BY renewDate DESC
)
UPDATE offd
SET offd.membershipRenewDate = offr.renewDate
FROM officiantsDetails offd
INNER JOIN Top1RenewDate offr ON offd.officiants_id = offr.officiants_id
This same syntax will also work for SQL Server, although you'd need to add a ; before the WITH keyword on that system.
The syntax you are using is not MariaDB. In MariaDB, you can use a JOIN:
UPDATE officiantsDetails offd JOIN
(SELECT renewDate, officiant_id
FROM officiantsRenewals
ORDER BY renewDate DESC
LIMIT 1
) offr
ON offd.officiant_id = offr.officiant_id
SET offd.membershipRenewDate = offr.renewDate;
The syntax you are using is more reminiscent of SQL Server.
I'm trying to make a currently working SQL query that I have in BigQuery more streamlines and am running into the following issue:
Error: ON clause must be AND of = comparisons of one field name from each table, with all field names prefixed with table name. Consider using Standard SQL .google.com/bigquery/docs/reference/standard-sql/), which allows non-equality JOINs and comparisons involving expressions and residual predicates.
Below is the query that is giving the error above. The first LEFT JOIN works. When I added the second one, right below, I started getting the error. What I'm trying to do is get the human readable own.o.firstname and own.o.lastname values rather than the owner_id value of the deal record (o.properties.hubspot_owner_id.value), but in order to do so I need to join some tables.
I had to use CAST on the ON clause of the second JOIN because the fields are of different types in each table's respective schema. If I don't do that, I get the following error: Error: Join keys o.properties.hubspot_owner_id.value (string) and o.ownerid (int64) have types that cannot be automatically coerced.
The WHERE clause is just a suppression list to not return entries that have been deleted from the database.
SELECT o.*
FROM (
SELECT
o.dealid,
o.properties.dealname.value,
stages.Label,
o.properties.closedate.value,
o.properties.hubspot_owner_id.value,
own.o.firstname,
own.o.lastname,
o.properties.amount.value,
o.properties.createdate.value,
o.properties.pipeline.value,
o.associations.associatedcompanyids,
ROW_NUMBER() OVER (PARTITION BY o.dealid ORDER BY o._sdc_batched_at DESC) as seqnum
FROM [sample-table:hubspot.deals] o
LEFT JOIN [sample-table:hubspot.sales_stages_lookup] stages ON o.properties.dealstage.value = stages.Internal_Value
LEFT JOIN [sample-table:hubspot.owners_reporting] own ON CAST(o.properties.hubspot_owner_id.value AS INTEGER) = CAST(own.o.ownerid AS INTEGER)) o
WHERE o.dealid NOT IN (SELECT objectid FROM [sample-table:hubspot_suppression_list.data] WHERE subscriptiontype = 'deal.deletion') AND seqnum = 1
Use standard SQL in BigQuery instead, which supports expressions as part of the ON clause:
#standardSQL
SELECT o.*
FROM (
SELECT
o.dealid,
o.properties.dealname.value AS dealname_value,
stages.Label,
o.properties.closedate.value AS closedate_value,
o.properties.hubspot_owner_id.value AS hubspot_owner_id_value,
own.o.firstname,
own.o.lastname,
o.properties.amount.value AS amount_value,
o.properties.createdate.value AS createdate_value,
o.properties.pipeline.value AS pipeline_value,
o.associations.associatedcompanyids,
ROW_NUMBER() OVER (PARTITION BY o.dealid ORDER BY o._sdc_batched_at DESC) as seqnum
FROM `sample-table.hubspot.deals` o
LEFT JOIN `sample-table.hubspot.sales_stages_lookup` stages ON o.properties.dealstage.value = stages.Internal_Value
LEFT JOIN `sample-table.hubspot.owners_reporting` own ON CAST(o.properties.hubspot_owner_id.value AS INT64) = CAST(own.o.ownerid AS INT64)) o
WHERE o.dealid NOT IN (SELECT objectid FROM `sample-table.hubspot_suppression_list.data` WHERE subscriptiontype = 'deal.deletion') AND seqnum = 1
For more on the differences between legacy and standard SQL in BigQuery, see the migration guide.
SELECT
`bqm`.`project`.`project_id` AS `project_id`,
`bqm`.`project`.`project_name` AS `project_name`,
`bqm`.`project`.`project_manager` AS `project_manager`,
`bqm`.`project`.`updated_by` AS `updated_by`,
`bqm`.`project`.`created_dt` AS `created_dt`,
`bqm`.`project`.`updated_dt` AS `updated_dt`,
`bqm`.`project`.`archive` AS `archive_status`,
( SELECT
`u`.`user_name` AS NAME
FROM
(`bqm`.`project` `p`
right JOIN `bqm`.`user` `u` ON (`p`.`created_by`=`u`.`user_id`))) as Name
FROM
`bqm`.`project`;
I'm not sure what DBMS you're using as the syntax doean't appear to validate, but i'd move the join outside the select clause like so;
SELECT
Prj.project_id AS project_id,
Prj.project_name AS project_name,
Prj.project_manager AS project_manager,
Prj.updated_by AS updated_by,
Prj.created_dt AS created_dt,
Prj.updated_dt AS updated_dt,
Prj.archive AS archive_status,
Usr.User_name as Name
FROM bqm.project as Prj
Join bqm.user as Usr on Usr.user_id = Prj.created_By;
The issue you're getting is because the sub-query in the select clause isn't linked to the "main" query, meaning for every row in the Project table you'll have every row in the sub-query. By moving the join to the main query as I've suggested above will eliminate this.
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 ;