SQL command not properly ended - problem with joining? - sql

I'm trying to read data and calculate the count of LASNRO and sum of LASKMKEALV for each LYTUNNUS, but I get the error message of:
ORA-00933: SQL-komento ei päättynyt oikein 00933.
00000 - "SQL command not properly ended"
*Cause:
*Action:
Error at Line: 7 Column: 20
I'm using Oracle SQL Developer, if it matters.
SELECT
TUNNUS,
LYTUNNUS,
COUNT(LASNRO),
SUM(LASKMKEALV)
FROM
XX.XX AS T1
INNER JOIN
YY.YY AS T2 ON T1.TUNNUS = T2.ASTUNNUS
GROUP BY
LYTUNNUS
ORDER BY
COUNT(LASKMKEALV) DESC;

Oracle does not allow as for table aliases. So the FROM clause should be:
FROM XX.XX T1 JOIN
YY.YY T2
ON T1.TUNNUS = T2.ASTUNNUS
The query you seem to want is:
SELECT TUNNUS,
COUNT(LASNRO),
SUM(LASKMKEALV)
FROM XX.XX T1 INNER JOIN
YY.YY T2
ON T1.TUNNUS = T2.ASTUNNUS
GROUP BY LYTUNNUS
ORDER BY COUNT(LASKMKEALV) DESC;

WITH
with_order_col AS (
SELECT
tunnus,
lytunnus,
COUNT(lasnro) AS lasnro_count,
SUM(laskmkealv) AS laskmkealv_sum,
COUNT(laskmkealv) AS laskmkealv_count
FROM xx.xx t1
JOIN yy.yy t2 ON t1.tunnus = t2.astunnus
GROUP BY
tunnus, -- you must GROUP BY every column that is not an aggregate function
lytunnus
)
SELECT
tunnus,
lytunnus,
lasnro_count,
laskmkealv_sum
FROM with_order_col
ORDER BY
laskmkealv_count DESC
;

Related

PostgreSQL multiple join issue

Iam trying to create query with multiple JOINS, but Im receiving error message regarding syntax on second JOIN.
ERROR: syntax error at or near "INNER"
LINE 19: INNER JOIN table2 AS jobinfo ON gaccess....
^
SQL state: 42601
Character: 418
Can you help me what am I doing wrong?
SELECT
access.id,
access.user_id,
access.device_id,
access.origin,
access.creation_date
FROM
table1 access
INNER JOIN (
SELECT
device_id,
MAX (creation_date) AS creation_date
FROM
table1
GROUP BY
device_id
) gaccess ON access.device_id = gaccess.device_id
AND access.creation_date = gaccess.creation_date;
INNER JOIN
table2 AS jobinfo
ON gaccess.device_id = jobinfo.id
You have a type - before the last INNER JOIN there is ; - you need to remove it.
SELECT access.id,
access.user_id,
access.device_id,
access.origin,
access.creation_date
FROM table1 access
INNER JOIN
(
SELECT device_id
,MAX(creation_date) AS creation_date
FROM table1
GROUP BY device_id
) gaccess
ON access.device_id = gaccess.device_id
AND access.creation_date = gaccess.creation_date
INNER JOIN table2 AS jobinfo
ON gaccess.device_id = jobinfo.id;

How do I clarify a multi-part identifier when updating a column from one database to another?

Database2 is getting an error
multi-part identifier could not be bound
from the query and I can't figure out why this is the case. I've looked at other posts but am having trouble finding an exact answer to what I'm doing wrong.
My goal is to replace a column of data from Database 2, to Database 1 that is between a time period.
Here's an example of what I am doing:
UPDATE Table1
SET Database1.dbo.Table1.Value1 = Database2.dbo.Table1.Value1
FROM Database1.dbo.Table1
INNER JOIN Database2.dbo.Table1.Value1 ON (Database1.dbo.Table1.Value1 = Database2.dbo.Table1.Value1)
WHERE Database1.dbo.Table1.Value1.Date_Time BETWEEN '2002-12-09 14:00:00.000' AND '2013-06-20 14:00:00.000';
This is just a syntax issue. Alias both tables.
UPDATE d1_table
SET d1_table.Value1 = d2_table.Value1
FROM Database1.dbo.Table1 d1_table
INNER JOIN Database2.dbo.Table1 d2_table
ON d1_table.Value1 = d2_table.Value1)
WHERE d1_table.Value1 BETWEEN '2002-12-09 14:00:00.000' AND '2013-06-20 14:00:00.000';
You need to join tables ot columns with tables
UPDATE t1
SET
t1.Value1 = t2.Value1
FROM Database1.dbo.Table1 t1
INNER JOIN (SELECT Value1 FROM Database2.dbo.Table1) t2
ON ( t1.Value1 = t2.Value1)
WHERE t1.Date_Time BETWEEN '2002-12-09 14:00:00.000' AND '2013-06-20 14:00:00.000';

sql oracle - group by with join

from this question, i tried to add join in the query, but i have this error
ORA-00979: not a GROUP BY expression
SELECT ee.UP, max(ee.CODE_DEPT), dpt.LIBELLE_DEPT
FROM ESP_ENSEIGNANT ee
INNER JOIN ESP_DEPT dpt
ON ee.CODE_DEPT = dpt.CODE_DEPT
group by ee.CODE_DEPT;
You need to include the columns which are not in the agg function. i.e ee.UP & dpt.Libelle_Dept as well.

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;

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