Using 'LIKE' in SQL Statement doesn't seem to work - sql

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%'

Related

Ambigously defined column in a subquery

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
)

Missing Keyword ORA-00905

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.

MS-Access query - Including Null values in with other conditions

Please help as this is aquery with many parameters but when it said
" ((OrderProdDetails.DeliveryDate) Like IIf([Forms]![ReportCriteriaSelection]![NoDevDate]=-1,"*","")) " it should have included null values aswell but it is not if I am putting or Is null with this criteria then it is being applied for both conditions regardless of check box is checked or not. Please help getting the null values included when checkbox is checked.
Please have look on screenshots:
Query in design View
Criteria Selection Screen
Query:
SELECT OrderCustMain.OdrID, OrderCustMain.CustName, OrderCustMain.CustEmail,
OrderCustMain.OdrDate, OrderProdDetails.DeliveryDate, Date()-
[OrderCustMain]![OdrDate] AS [Days Past], OrderCustMain.OrderStatus,
OrderProdDetails.BrandName, OrderProdDetails.ModelName,
OrderProdDetails.Priority, SupplierDetails.SupEmail, OrderProdDetails.Status
FROM SupplierDetails INNER JOIN (OrderCustMain INNER JOIN OrderProdDetails
ON OrderCustMain.[OdrID] = OrderProdDetails.[OrdID]) ON
SupplierDetails.SupOrgName = OrderProdDetails.BrandName WHERE
(((OrderCustMain.OdrDate) Like IIf([Forms]![ReportCriteriaSelection]!
[NoStDate]=-1,"*","")) AND ((OrderProdDetails.DeliveryDate) Like
IIf([Forms]![ReportCriteriaSelection]![NoDevDate]=-1,"*","")) AND ((Date()-
[OrderCustMain]![OdrDate])>=[Forms]![ReportCriteriaSelection]![cboDaysPast])
AND ((OrderCustMain.OrderStatus) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus])) AND
((OrderProdDetails.BrandName) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritBrandName]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritBrandName]))) OR
(((OrderCustMain.OdrDate)>=[Forms]![ReportCriteriaSelection]!
[RepCritOdrStDate] And (OrderCustMain.OdrDate)<=[Forms]!
[ReportCriteriaSelection]![RepCritOdrEndDate]) AND
((OrderProdDetails.DeliveryDate)>=[Forms]![ReportCriteriaSelection]!
[RepCritDlvryStDate] And (OrderProdDetails.DeliveryDate)<=[Forms]!
[ReportCriteriaSelection]![RepCritDlvryEndDate]) AND ((Date()-
[OrderCustMain]![OdrDate])>=[Forms]![ReportCriteriaSelection]![cboDaysPast])
AND ((OrderCustMain.OrderStatus) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus])) AND
((OrderProdDetails.BrandName) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritBrandName]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritBrandName]))) OR (((OrderCustMain.OdrDate)
Like IIf([Forms]![ReportCriteriaSelection]![NoStDate]=-1,"*","")) AND
((OrderProdDetails.DeliveryDate)>=[Forms]![ReportCriteriaSelection]!
[RepCritDlvryStDate] And (OrderProdDetails.DeliveryDate)<=[Forms]!
[ReportCriteriaSelection]![RepCritDlvryEndDate]) AND ((Date()-
[OrderCustMain]![OdrDate])>=[Forms]![ReportCriteriaSelection]![cboDaysPast])
AND ((OrderCustMain.OrderStatus) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus])) AND
((OrderProdDetails.BrandName) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritBrandName]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritBrandName]))) OR
(((OrderCustMain.OdrDate)>=[Forms]![ReportCriteriaSelection]!
[RepCritOdrStDate] And (OrderCustMain.OdrDate)<=[Forms]!
[ReportCriteriaSelection]![RepCritOdrEndDate]) AND
((OrderProdDetails.DeliveryDate) Like IIf([Forms]![ReportCriteriaSelection]!
[NoDevDate]=-1,"*","")) AND ((Date()-[OrderCustMain]![OdrDate])>=[Forms]!
[ReportCriteriaSelection]![cboDaysPast]) AND ((OrderCustMain.OrderStatus)
Like IIf(IsNull([Forms]![ReportCriteriaSelection]![RepCritOdrStatus]),"*",
[Forms]![ReportCriteriaSelection]![RepCritOdrStatus])) AND
((OrderProdDetails.BrandName) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritBrandName]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritBrandName]))) ORDER BY
OrderCustMain.OdrID, OrderProdDetails.Priority;
The primary source for your trouble is that dates are not strings, so "Date Like some-string" will either fail or return unintended results.
You'll have to redesign this from scratch. Exactly how depends on the context.
Would something like this help?
WHERE Nz(OrderCustMain.OdrDate, #1/1/1900#) >= IIf([Forms]![ReportCriteriaSelection]!
[NoStDate]=-1,Nz(OrderCustMain.OdrDate, #1/1/1900#),[Forms]![ReportCriteriaSelection]!
[RepCritOdrStDate])
It might simplify the code.

SQL Update on joined tables with calculated fields

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.

return value from nested sql

I have the following SQL and i want theorder_item_wo_id column to return null if the item does not exist in the nested sql and return the value of a column oi.cop_workorder_id if it does.
How can i accomplish this?
SELECT
cop.wo_id,
CASE
WHEN EXISTS (SELECT oi.cop_workorder_id
FROM oseo_orderitem oi
WHERE oi.cop_workorder_id = cop.wo_id) THEN
oi.cop_workorder_id
ELSE null
END AS order_item_wo_id
FROM oseo_cop_wo cop
where cop.wo_id = '123';
It looks like the oi.cop_workorder_id does not get recognized outside of the nested sql.
ERROR: missing FROM-clause entry for table "oi"
Unless I am missing something, it seems you are overcomplicating this.
Just use a LEFT JOIN:
SELECT
cop.wo_id,
oi.cop_workorder_id AS order_item_wo_id
FROM oseo_cop_wo AS cop
LEFT JOIN oseo_orderitem AS oi
ON oi.cop_workorder_id = cop.wo_id
WHERE cop.wo_id = '123';
use can use the COALESCE function. It works similar to Oracle's NVL function. I don't have PostgreSQL available to write and test this, however.