I'm getting the ORA-00933 error referenced in the subject line for the following statement:
select
(select count(name) as PLIs
from (select
a.name,
avg(b.list_price) as list_price
from
crm.prod_int a, crm.price_list_item b
where
a.row_id = b.product_id
and a.x_sales_code_3 <> '999'
and a.status_cd not like 'EOL%'
and a.status_cd not like 'Not%'
and a.x_sap_material_code is not null
group by a.name)
where list_price = 0)
/
(select count(name) as PLIs
from (select
a.name,
avg(b.list_price) as list_price
from
crm.prod_int a, crm.price_list_item b
where
a.row_id = b.product_id
and a.x_sales_code_3 <> '999'
and a.status_cd not like 'EOL%'
and a.status_cd not like 'Not%'
and a.x_sap_material_code is not null
group by a.name))
as result from dual;
I've tried removing the aliases as suggested solution in other posts but that didn't change the problem. Any ideas? Thanks.
If you're running this in SQLPlus, it is possible that it misinterprets the division operator in the first column for the statement terminator character. Other tools may also be susceptible. Try moving the division operator, e.g. where list_price = 0) \
Answer is wrong, see comment by #Ben
Sub-queries to not have to be named... only if they're directly referenced, i.e. if there's more than one column with the same name in the full query
Subqueries have to be named. Consider changing:
from (select
...
group by a.name)
To:
from (select
...
group by a.name) SubQueryAlias
This does not directly answer your question, but I think the query can be simplified:
select case PLIs when 0 then -1 else PLIs_noprice / PLIs end from (
select
count(name) as PLIs,
count(case list_price when 0 then 1 end) as PLIs_noprice
from (
.... your innermost subselect, up to "group by" goes here ...
)
)
Somehow I can't paste your actual subselect code here, getting "Error submitting your post"... Not tested, as I don't have your tables.
Related
I am trying to define an SQL query in my SSRS report, but I am getting some syntax errors:
Error in SELECT clause: expression near 'WHEN'.
Missing FROM clause.
Error in SELECT clause: expression near ','.
Unable to parse query text.
The query is not that complicated, but rather a bit convoluted, but I'll try to convey at least the structure of it here:
select
(CASE WHEN columnA1 is null THEN columnA2 ELSE columnA1 END) as columnA,
(CASE WHEN columnB1 is null THEN columnB2 ELSE columnB1 END) as "custom_name_for_columnB"
from
(
(select a.columnA1, ...
from myTable a, ...
// join conditions
)
union
select * from
(select a.columnA1, ...
from myTable a, ...
// join conditions
order by someColumn) source
)
);
I don't think it really matters what the query does since I ran it in my DMBS successfully, so I'm pretty sure it's correct SQL syntax (I'm working on Oracle DB). I think what I'm not seeing is some syntax specific to SSRS. I'm completely new to it, so I don't know whether it supports the entire SQL syntax like CASE WHEN, unions etc.
As it complains about CASE (as if it doesn't recognize the syntax), try some more options:
COALESCE:
select coalesce(columnA1, columnA2) columnA,
coalesce(columnB1, columnB2) columnB
from ...
NVL:
select nvl(columnA1, columnA2) columnA,
nvl(columnB1, columnB2) columnB
from ...
DECODE:
select decode(columnA1, null, columnA2, columnA1) columnA,
decode(columnB1, null, columnB2, columnB1) columnB
from ...
Correct my query if I made too many changes, but I think there are a couple of things wrong:
SELECT (CASE WHEN t.columnA1 IS NULL THEN t.columnA2 ELSE t.columnA1 END) as columnA,
(CASE WHEN t.columnB1 IS NULL THEN t.columnB2 ELSE t.columnB1 END) as [custom_name_for_columnB]
FROM(
SELECT a.columnA1, ...
FROM myTable a, ...
JOIN conditions
UNION
SELECT * FROM
(
SELECT a.columnA1, ...
FROM m myTable a, ...
JOIN conditions
--ORDER BY someColumn --Can't have ORDER BY in subquery without TOP(n) / FOR XML.
) source
)t; --Needs an alias
I'm pretty much out of ideas on how to get this to work.I haven't really used SQL in several years so there's a lot I don't remember.
So here is what I would like to happen:
I return the rows where the Code field from table has the value 1208 AND estnumber = 1187216
Run a count on the selection, if 0 run a subquery
If >0 run a different subquery
I didn't get to the subquery part yet because I can't get this to work correctly at all. Right now I just want it to return text.
Here is the latest attempt, I'm actually using db2 but maybe we can ignore that for now and i'll work that part out later because it says the syntax isnt correct, but other validators disagree (if you dont know anything about db2 just use standard sql when giving advice)
SELECT
count(*) AS t
FROM
table
WHERE
(
ESTNUMBER = 1187216
AND CODE = 1208
)
AND CASE WHEN t = 0 THEN 'it is zero' ELSE 'it is not zero' END;
Are you trying to do something like this?
WITH c AS (
SELECT count(*) AS cnt
FROM table
WHERE ESTNUMBER = 1187216 AND CODE = 1208
)
SELECT s1.*
FROM subquery1 s1
WHERE (SELECT cnt FROM c) = 0
UNION ALL
SELECT s2.*
FROM subquery2 s2
WHERE (SELECT cnt FROM c) > 0;
This assumes that the columns returned by the subqueries are compatible (same number, same types).
There are better ways to write this query (notably using EXISTS and NOT EXISTS), but this conforms directly to how you asked the question.
The string value should come up in the select clause and not in the where filter.
SELECT
count(*) AS t,
(CASE WHEN count(*) = 0 THEN 'it is zero' ELSE 'it is not zero' END) display_str
FROM
table
WHERE
(
ESTNUMBER = 1187216
AND CODE = 1208
)
You're thinking like an imperative programmer, not a declarative one. That is, SQL doesn't have sequential execution: it's all or nothing.
So, here's the start, the bit that works:
SELECT count(*) AS t
FROM table
WHERE ESTNUMBER = 1187216 AND CODE = 1208
Now, to check for the value of count(*), you by now know that WHERE isn't going to work. That's because COUNT is an aggregate function. To look at the result of such of function, you use HAVING.
For your CASE to work, you can move it up into the area that can get count(*) results:
SELECT count(*) AS t
(CASE WHEN count(*) = 0 THEN 'it is zero' ELSE 'it is not zero' END) as msg
FROM table
WHERE ESTNUMBER = 1187216 AND CODE = 1208
Note that "t" is an alias you've given the result of count(*). In most SQL implementations, that alias can't be leveraged in the rest of the statement.
Now, for the either or kind of thing, it would be time to reconsider your approach and what you're really after. You'll probably ultimately have both result sets in your statement and choose how the results are served up.
Something like:
select a.id, a.ct, (case when a.ct=0 then b.amt else c.amt end) as amt
from (select id, count(*) as ct from table1) a
left join (select id, sum(amount) as amt from table2) b on a.id=b.id
left join (select id, sum(amount) as amt from table3) c on a.id=c.id
Hope this helps.
In this query, I am trying to select all distinct (alphanumeric) machine names and order them correctly (1,2,5,10,15 instead of 1,10,15,2,5). The CASE statement is proven to work when the LocalName is not joined by INNER JOIN, so I suspect this is where the problem lies.
SELECT DISTINCT MCGroup, VisionMachinePerformance.MCSAP, ZAssetRegister.LocalName
FROM [VisionMachinePerformance] INNER JOIN ZAssetRegister ON VisionMachinePerformance.MCSAP=ZAssetRegister.SAP_Number
ORDER BY
CASE WHEN PATINDEX('%[0-9]%',LocalName) > 1 THEN
LEFT(LocalName,PATINDEX('%[0-9]%',LocalName)-1)
ELSE LocalName END ,
CASE WHEN PATINDEX('%[0-9]%',LocalName) > 1 THEN
CAST(SUBSTRING(LocalName,PATINDEX('%[0-9]%',LocalName),LEN(LocalName)) as float)
ELSE NULL END
The error that is reported is "SQL Error (145): ORDER BY items must appear in the select list if SELECT DISTINCT is specified".
I have tried changing all references in the CASE statement to ZAssetRegister.LocalName and VisionMachinePerformance.LocalName without success.
Removing all of the CASE statement and ordering by LocalName does work, but with the wrong order as mentioned above (1,10,15,2,5).
Could anybody suggest how to make this work?
TIA!
You can separate both parts using a subquery:
SELECT * FROM (
SELECT DISTINCT MCGroup, VisionMachinePerformance.MCSAP, ZAssetRegister.LocalName
FROM [VisionMachinePerformance]
INNER JOIN ZAssetRegister ON VisionMachinePerformance.MCSAP=ZAssetRegister.SAP_Number
) DISTINCT_DATA
ORDER BY
CASE WHEN PATINDEX('%[0-9]%',LocalName) > 1
THEN LEFT(LocalName,PATINDEX('%[0-9]%',LocalName)-1)
ELSE LocalName END,
CASE WHEN PATINDEX('%[0-9]%',LocalName) > 1
THEN CAST(SUBSTRING(LocalName,PATINDEX('%[0-9]%',LocalName),LEN(LocalName)) as float)
ELSE NULL END
I'm trying to write a query containing subqueries, which contain aggregate functions. I get an error message telling me that I should include some of my fields in the GROUP BY clause of my main query, in which I don't use any aggregates.
I'm pretty new at SQL so this might be a silly mistake but I have been on it for days and I can't figure it out with the existing content.
WITH B AS
(
SELECT
A.Week
,A.ASIN
,A.GL
,sum(Case when Week = 'W1' then Total_GV_Price/Total_GVs else 0 end) as Wk1_AVG
,sum(Case when Week = 'W2' then Total_GV_Price/Total_GVs else 0 end) as Wk2_AVG
FROM A
WHERE Total_GVs > 0
GROUP BY A.Week, A.ASIN, A.GL
)
, C AS
(
SELECT
B.Week
,B.ASIN
,B.GL
,B.Wk1_AVG
,B.Wk2_AVG
,Case when NVL(Wk1_AVG,0) =0 then NULL else (Wk2_AVG - Wk1_AVG) / Wk1_AVG end as Price_var
FROM B
GROUP BY B.Week, B.ASIN, B.GL, B.Wk1_AVG, B.Wk2_AVG
)
SELECT C.*
FROM C
HAVING Price_var >= 0.3
So the error message is telling me: column "c.week" must appear in the GROUP BY clause or be used in an aggregate function. And I don't understand what i'm not grouping or what I should be grouping.
I appreciate any comments and thank you for your help !
I'm not sure, because I can't reproduce the situation. But I think the problem lies in the SELECT ... FROM C at the end. You are using HAVING, which is used for aggregated columns (i.e. SUM(...)). Because the last SELECT has no aggregation a simple WHERE would suffice.
...
SELECT
C.*
FROM
C
WHERE Price_var >= 0.3
Hope this helps...
I want to return multiple values from a query in oracle. For ex:
select count(*)
from tablename a
where asofdate='10-nov-2009'
and a.FILENAME in (case
when 1 = 1 then (select distinct filename from tablename
where asofdate='10-nov-2009' and isin is null)
else null
end);
I am getting error: ora 01427 single row subquery returns more than one row
Please advice.
Thanks, Deepak
A CASE statement cannot return more than one value, it is a function working on one value.
It is not required for your statement, this statement should work:
select count(*)
from tablename a
where asofdate='10-nov-2009'
and a.FILENAME in (select distinct filename
from tablename
where asofdate='10-nov-2009'
and isin is null);
Maybe you have another usage scenario in mind? Something like this:
Select *
From aTable
Where in CASE
WHEN Then
WHEN Then
ELSE END
Then using CASE may not be the right scenario. Maybe this helps you in the right direction:
Select *
From aTable
Where <Case1> and column1 in <Subselect1>
Or <Case2> and column1 in <Subselect2>
OR Not (<Case1> Or <Case2>) and column1 in <Subselect3>
But this will probably be quite some work for the optimizer ...
The distinct in your Case statement is attempting to return multiple values when only one is allowed, and your SELECT statement will only return one value in one row currently. If you're trying to get the count of each filename, do
SELECT FileName, Count(*)
FROM tablename
WHERE asofdate='10-nov-2009' and isin is null
GROUP BY FileName
Run this query:
select distinct filename from tablename
where asofdate='10-nov-2009' and isin is null
You'll see that it returns more than a single row which causes the ORA-01427.
For all I can tell, you're looking for something like:
select a.filename, count(*)
from tablename a
where a.asofdate = '10-nov-2009'
and exists (
select *
from tablename b
where b.isin is null
and a.asofdate = '10-nov-2009'
and a.filename = b.filename
)
group by a.filename
This would find the count of filenames for a day, for which there exists at least one row where isin is null.
If you edit your question and add an explanation of what you're looking for, you might get better answers.