please tell me what is the syntax problem in this query
SELECT sde
FROM TABLE_EW sde , CASE_W spr, DOCUMENT swp
JOIN swp.id, swp.YEAR ON (swp.id = sde.ID_DOCUMENT)
JOIN spr.ID, spr.STATE, spr.NUMBER ON (spr.ID_DOCUMENT = swp.ID)
WHERE sde.IDENT_TABLEEW LIKE '122337456464'
AND swp.YEAR LIKE 2015;
SQLDeleoper point problem to From Line
I think this is the query you meant to write:
SELECT sde.*,swp.id, swp.YEAR,spr.ID, spr.STATE
FROM TABLE_EW sde
JOIN DOCUMENT swp ON (swp.id = sde.ID_DOCUMENT)
JOIN CASE_W spr ON (spr.ID_DOCUMENT = swp.ID)
WHERE sde.IDENT_TABLEEW = '122337456464'
AND swp.YEAR = 2015;
As mentioned in the comments, you have A LOT of errors in your SQL code.
You use implicit and explicit joins together, AVOID the use of implicit joins syntax and use only the proper syntax like my example.
Also, only in the select you can specify the columns you want, I'm guessing what you've been trying to do is
JOIN spr.ID, spr.STATE -> wanted this columns.
You should write them in the select part.
Another problem is the join condition, you either use implicit joins, (from table,table2,table3..) and then the join condition is in the where clause or you use explicit joins and then the condition is in the ON clause. You can't use both!
Another problem is the unnecessary use of LIKE . When comparing to an exact match, use EQUALS sign.
Related
I've the following subquery in an sql query:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, NOMBRE AS NOMBREUNIDAD FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY ID_PLAN, ID_CURSO, NEDICION) ASIS
Problem I have I believe lies in that both table ALUMNOS and UNIDADES have a column named 'NOMBRE' so if I attempt to execute the query I obtain:
00000 - "column ambiguously defined"
To avoid that I thought about changing NOMBRE AS NOMBREUNIDAD to:
UNIDADES.NOMBRE AS NOMBREUNIDAD
But if I do that I get a:
00000 - "not a GROUP BY expression"
So, I don't know what to do so that subquery executes properly.
What should I change to properly execute query without changing the column name?
Aliases are pretty useful, if you use them. The simplify queries and make them easier to read and maintain. I'd suggest you to do so, as it'll also help query to work because Oracle doesn't know which table you actually meant when you selected those 4 columns - which tables do they belong to?
This is just a guess as I don't know your tables so you'll have to fix it yourself. Also, I literally JOINed tables; try to avoid comma-separating them in FROM clause and doing join in WHERE clause as it is supposed to filter data.
GROUP BY, as already commented, is probably useless. If you wanted to fetch distinct set of values, then use appropriate keyword: distinct.
SELECT DISTINCT n.id_plan,
s.id_curso,
u.nedicion,
u.nombre
FROM asisten n
JOIN alumnos s ON n.cod = s.cod
JOIN unidades u
ON u.idestructura = s.idestructura
AND u.cdundorg = s.cdundorg
WHERE UPPER (TRANSLATE (u.nombre, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
I managed to solve my problem:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, UNIDADES.NOMBRE AS NOMBREUNIDAD
FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY UNIDADES.NOMBRE,ID_PLAN, ID_CURSO, NEDICION
)
I have a common column (Name0) that I need to select from one of my tables. So, I alias it out, but then it starts throwing errors. The two columns I try to select show they could not be bound, followed by my aggregate having syntax errors. Finally, the grouping by set alias' show incorrect syntax. I am at a loss.
Select I.ARPDisplayName0,
SR.name0 = coalesce(SR.name0,concat('Total ',sum(1)))
From v_GS_INSTALLED_SOFTWARE I
inner join v_R_System SR on SR.resourceID = I.ResourceID
inner join v_GS_OPERATING_SYSTEM OS on OS.ResourceID = I.ResourceID
WHERE
ARPDisplayName0 = 'Adobe Acrobat 9 Pro' or
ARPDisplayName0 = 'Bomgar' or
Caption0 = 'Microsoft Windows 7 Professional'
Group By Grouping Sets (
(I.ARPDisplayName0,SR.name0),
(I.ARPDisplayName0),
(OS.Caption0,SR.Name0),
(OS.Caption0)
()
);
Specific errors:
Incorrect Syntax expecting (or Select" in coalesce(SR.name0,concat('Total ',sum(1))) and in the grouping sets alias.
The other error is:
the Multipart Identifier could not be bound in the SELECT statement "I.ARPDisplayName0" and "SR.name0"
The issues arise from just from a simple misunderstanding of how to use alias.
When providing the name of an alias, you can choose to use either an = assignment or the (optional) keyword as - it's really personal preference which you use but I prefer the latter option and keep = for actually assigning values to local variables.
The line SR.name0 = coalesce(SR.name0,concat('Total ',sum(1))) is causing an error since SQL Server interprets the SR. as an alias reference itself which can't be used in this context - the intended name is just "name0"; constructing it as coalesce(SR.name0,concat('Total ',sum(1))) as name0 would make the intent clearer.
Likewise, when using brackets [] around object names, the alias dot-notation is not included, so [I.ARPDisplayName0] is just I.[ARPDisplayName0]; in this case the brackets are completely optional as they are only required if there is a clash with a reserved word eg [date] or the use of certain characters such as a space [my column name]. Again it's personal preference but I prefer to only use them where necessary to remove "noise" and improve readability.
I seem to be able to do a simple query that uses the equal sign, but am unable to find any results when I use the 'LIKE' function. Am I missing something here?
SELECT ISARPapers.*, AuthorList.FirstName, AuthorList.LastName
FROM AuthorList INNER JOIN (ISARPapers INNER JOIN PaperAuthor
ON ISARPapers.PaperID = PaperAuthor.PaperID)
ON AuthorList.AuthorID = PaperAuthor.AuthorID
WHERE ISARPapers.PaperTitle LIKE '%Mianzi%'
be carrefuly with capital letters
... WHERE ISARPapers.PaperTitle LIKE '%Mianzi%'
maybe Mianzi does not exists but mianzi does.
if you do not care about capitals, try:
... WHERE lower(ISARPapers.PaperTitle) LIKE '%mianzi%'
First of all, I know there are already questions and answers about it, this thread being the one that is closest to what I need:
SQL Update to the SUM of its joined values
However, I get a syntax error (operator missing) that seems to occur close to the FROM clause. However I can't see it. Does it not like the FROM itself ? I am not used to using FROM in an update statement but it seems like it's valid from the QA I just linked :|
Any idea why there would be a syntax error there ?
I am using Access 2007 SP3.
Edit:
Wow, I forgot to post the query...
UPDATE r
SET
r.tempsmoy_requete_min = tmm.moy_mob_requete
FROM
rapports AS r INNER JOIN
(SELECT
id_fichier,
Round(Sum(temps_requete_min)/3,0) As moy_mob_requete,
Round(Sum(temps_analyse_min)/3,0) As moy_mob_analyse,
Round(Sum(temps_maj_min)/3,0) As moy_mob_maj,
Round(Sum(temps_rap_min)/3,0) As moy_mob_rap,
Round(Sum(temps_ddc_min)/3,0) As moy_mob_ddc
FROM maintenances
WHERE
periode In (10,9,8) And
annee=2011
GROUP BY id_fichier) AS tmm ON rapports.id_rapport = tmm.id_fichier
WHERE
1=0
The WHERE 1=0 part is because I want to test further the subquery before running it.
Edit: This is some simpler query I am trying. I get a different error this time. It now tells me that tempsmoy_requete_min (and probably all other left operands) are not part of an aggregate function... which is the point of my query. Any idea ?
UPDATE
rapports INNER JOIN maintenances ON rapports.id_rapport = maintenances.id_fichier
SET
rapports.tempsmoy_requete_min = Round(Sum(temps_requete_min)/3,0),
rapports.tempsmoy_analyse_min = Round(Sum(temps_analyse_min)/3,0),
rapports.tempsmoy_maj_min = Round(Sum(temps_maj_min)/3,0),
rapports.tempsmoy_rap_min = Round(Sum(temps_rap_min)/3,0),
rapports.tempsmoy_ddc_min = Round(Sum(temps_ddc_min)/3,0)
WHERE
maintenances.periode In (10,9,8) And
maintenances.annee=2011 AND
1=0
I tried adapting your first query sample, and was able to make your error go away. However then I encountered a different error ('Operation must use an updateable query').
It may be possible to overcome that error, too. However, I found it easier to use a domain function instead of a join to retrieve the replacement value.
UPDATE rapports
SET tempsmoy_requete_min = Round(DSum("temps_requete_min",
"maintenances",
"periode In (10,9,8) AND annee=2011 "
& "AND id_fichier='" & id_rapport
& "'")/3, 0);
If this suggestion works for tempsmoy_requete_min with your data, you will have to extend it to the other fields you want to replace. That won't be pretty. You could make it less ugly with a saved query which you then use as the "Domain" parameter for DSum() ... that could allow you to use a simpler "Criteria" parameter.
UPDATE r
should be
UPDATE rapports
You can't reliably use an alias in the update target.
Today while inside a client's production system, I found a SQL Server query that contained an unfamiliar syntax. In the below example, what does the *= operator do? I could not find any mention of it on MSDN. The query does execute and return data. As far as anyone knows, this has been in the system since they were using SQL Server 2000, but they are now running 2005.
declare #nProduct int
declare #iPricingType int
declare #nMCC int
set #nProduct = 4
set #iPricingType = 2
set #nMCC = 230
--Build SQL for factor matrix
Select distinct
base.uiBase_Price_ID,
base.nNoteRate,
base.sDeliveryOpt,
IsNull(base.nPrice,0) as nPrice,
IsNull(base.nPrice,0) + Isnull(fact.nFactor,0) as nAdjPrice,
base.iProduct_ID,
fact.iPosition as fiPosition,
base.iPosition,
CONVERT(varchar(20), base.dtDate_Updated, 101) + ' ' + CONVERT(varchar(20), base.dtDate_Updated, 108) as 'dtDate_Updated',
fact.nFactor,
fact.nTreasFactor,
product.sProduct_txt ,
pfi.sPFI_Name,
mccprod.nServicing_Fee,
fact.nNoteRate as fNoteRate,
mcc.nLRA_Charge as nLRA
From
tbl_Base_Prices base, tbl_Factors fact, tbl_Product product, tbl_PFI pfi, tbl_MCC mcc, tbl_MCC_Product mccprod
Where
base.iProduct_ID = #nProduct
And base.iProduct_ID *= fact.iProduct_ID
And base.iPosition *= fact.iPosition
And base.nNoteRate *= fact.nNoteRate
And base.iPricing_Type = #iPricingType
And fact.iMCC_ID = #nMCC
And fact.iProduct_ID = #nProduct
And mcc.iMCC_ID = #nMCC
And mcc.iPFI_ID = pfi.iPFI_ID
And mccprod.iMCC_ID = #nMCC
And mccprod.iProduct_ID = #nProduct
And base.iProduct_ID = product.iProduct_ID
and fact.iPricing_Type= #iPricingType
Order By
base.nNoteRate, base.iPosition
Remove this code immediately and replace with a left join. This code does not always interpret correctly (Sometimes SQL Server decides it is a cross join) even in SQL Server 2000 and thus can give incorrect results! Also it is deprecated for the future (Using Outer Joins, SQL Server 2000 documentation archived from the original).
I'm going to add that in adjusting to left joins you should remove all of those other implicit joins as well. The implicit join syntax has been obsolete since 1992, there is no excuse for it still being in production code. And mixing implicit and explicit joins can give unexpected results.
It is a left outer join, =* is a right outer join.
E.g. the following are equal;
SELECT * FROM Table1 LEFT OUTER JOIN Table2 ON Table1.ID = Table2.FK_ID
SELECT * FROM Table1, Table2 WHERE Table1.ID *= Table2.FK_ID
The non-ANSI syntax for outer joins (*= and =*) is on the official list of deprecated features that will be removed in the next version of SQL.
The following SQL Server Database
Engine features will not be supported
in the next version of SQL Server. Do
not use these features in new
development work, and modify
applications that currently use these
features as soon as possible.
The replacement feature is the ANSI compliant syntax of JOIN.
It's a shorthand join syntax. Take a look at this thread which covers this topic.
Transact-SQL shorthand join syntax?
I believe those are "non-ANSI outer join operators". Your database compatibility level must be 80 or lower.
That's the older ANSI (ANSI-89) syntax left outer join operator. I'd recommend not using it - the ANSI syntax is more verbose and is much more readable.