Not able to use pivot function in oracle sql develope - sql

this is the table which I need to pivot(age range must be an attribute and percentage as its row)
[1]: https://i.stack.imgur.com/TyQpV.jpg
already coded:
SELECT SECOND_RESPONSE, 25-30, 30-35, 18-25, 45-50, 40-45, 35-40, 55-60 FROM
(SELECT SECOND_RESPONSE FROM SEC_ANALYSIS_AGE)
PIVOT
(
MAX(PERCENTAGE) FOR FIRST_RESPONSE IN (25-30, 30-35, 18-25, 45-50, 40-45, 35-40, 55-60)
) AS PIV;
Output
ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Error at Line: 24 Column: 3

I would recommend an include FIRST_RESPONSE, PERCENTAGE columns select statement. Would be this one:
SELECT * FROM
(SELECT SECOND_RESPONSE, FIRST_RESPONSE, PERCENTAGE FROM SEC_ANALYSIS_AGE)
PIVOT
(
MAX(PERCENTAGE) FOR FIRST_RESPONSE IN ('25-30' as "25-30", '30-35' as "30-35", '18-25' as "18-25", '45-50' as "45-50", '40-45' as "40-45", '35-40' as "35-40", '55-60' as "55-60")
)
thanks

Related

Using sum function in sql view in oracle database

I am trying to create a view using some o group function like sum , avg etc. it is giving error.
create view sample_view as
select A,B,C,D,E,
(a + b+c+d+e)/5 as Mean_value,
GREATEST(a,b,c,d,e)-LEAST(a,b,c,d,e) as range_value,
avg(mean_value)
from samples;
I am getting below error.
Error starting at line : 37 in command -
create view sample_view as select A,B,C,D,E, (a + b+c+d+e)/5 as Mean_value, GREATEST(a,b,c,d,e)-LEAST(a,b,c,d,e) as range_value, avg(mean_value) from samples
Error report -
ORA-00937: not a single-group group function
00937. 00000 - "not a single-group group function"
*Cause:
*Action:
Is there anyway to create view in oracle database using group function. I also tried sum() and count(). all those also giving error.
Root Cause for your problem is:
You tried to execute a SELECT statement that included a GROUP BY function (ie: AVG Function,MIN Function, MAX Function, SUM Function, COUNT Function), but was missing the GROUP BY clause.Try to add Group BY clause and run the query.
You can try the below - since avg() is an aggregate function you can not add this with other columns without adding group by the clause -
create view sample_view as
select A,B,C,D,E, (a + b+c+d+e)/5 as Mean_value,
GREATEST(a,b,c,d,e)-LEAST(a,b,c,d,e) as range_value,
avg((a + b+c+d+e)/5) over()
from samples;

Nested select statements in impala sql

I have the following SQL query in impala
SELECT currentdate,close
FROM ( SELECT * FROM spyprice)
Where currentdate between '2015-01-16' and '2016-06-17';
And it is giving me the error:
Starting Impala Shell without Kerberos authentication
ERROR: AnalysisException: Syntax error in line 15:
WHERE currentdate BETWEEN '2015-01-16' and '2016-06-17'
^
Encountered: WHERE
Expected: AS, DEFAULT, IDENTIFIER
CAUSED BY: Exception: Syntax error
Anyone knows what's going on?
Thanks in advance!
#James Xiang The right syntax of the query statement is :
SELECT a.currentdate, a.close FROM
(
SELECT * FROM spyprice
) a
Where a.currentdate between '2015-01-16' and '2016-06-17';

MATCH_RECOGNIZE - ORA-00933: SQL command not properly ended

with curse as (select luna,an,valoare from cursl where moneda='eur')
SELECT *
FROM curse MATCH_RECOGNIZE (
ORDER BY an,luna
MEASURES strt.an as start_an, strt.luna as start_luna,
strt.valoare as euro_inc,LAST(scade.valoare) as euro_sf,
count(*) as nr_luni,
strt.valoare-LAST(scade.valoare) as dif
ONE ROW PER MATCH
PATTERN (strt scade{3,})
DEFINE
scade AS scade.valoare < PREV(scade.valoare)
) a
ORDER BY a.start_an,a.start_luna;
when running this I receive :
ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
According to Oracle-Base:
Oracle 12c has added the MATCH_RECOGNIZE clause

Pivot query causing "incorrect syntax" error in the PIVOT clause

I am getting the error, "incorrect syntax near 'Basic'" when I try to execute the following query:
WITH BaseQuery AS (
SELECT Region,Essbase_Channel,Product,COUNT(New_reconnects)
FROM NDW.dbo.SOS_Detail SOS
WHERE SOS.EntityID IN ('000310','000700','000815','000854')
AND Division ='NORTHEAST DIVISION' AND Month_Name ='MAR'
GROUP BY Month_Name,Product,Region,Essbase_Channel,EntityID,Division
)
SELECT * FROM BaseQuery
PIVOT (COUNT(New_reconnects) FOR Product IN ('BASIC','HSI','CDV','H1','X1')) AS PVT
ORDER BY Product,Region,Essbase_Channel
My goal would be to have the first column based on Essbase_Channel, the pivot columns to be Product values: BASIC, HSI, CDV, H1 and the values in the pivot to be COUNT(New_reconnects).
How should I change the syntax of the query to avoid this error?
Change to:
FOR Product IN ([BASIC],[HSI],[CDV],[H1],[X1])

Advantage Database 8.1 SQL IN clause

Using Advantage Database Server 8.1 I am having trouble executing a successful query. I am trying to do the following
SELECT * FROM Persons
WHERE LastName IN ('Hansen','Pettersen')
To check for multiple values in a column. But I get an error when I try to execute this query in Advantage.
Edit - Error
poQuery: Error 7200: AQE Error: State = 42000; NativeError = 2115; [iAnywhere Solutions][Advantage SQL Engine]Expected lexical element not found: ( There was a problem parsing the
WHERE clause in your SELECT statement. -- Location of error in the SQL statement is: 46
And here is the SQL i'm executing
select * from "Pat Visit" where
DIAG1 IN = ('43644', '43645', '43770', '43771', '43772', '43773', '43774',
'43842', '43843', '43845', '43846', '43847', '43848', '97804', '98961',
'98962', '99078')
Done
Does anyone have any Idea how I could do something similar in advantage that would be efficient as well?
Thanks
You have an extraneous = in the statement after the IN. It should be:
select * from "Pat Visit" where
DIAG1 IN ('43644', '43645', <snip> )