I'm trying to run some code with a subquery but I'm not able to figure out why I'm getting an error.
select efile.*
from
(select can,
sum(case when tax_year = 2018 then IND_RETURNS_IN_SEASON+IND_RETURNS_EXT_SEASON else 0 end) as TY18_efiles,
sum(case when tax_year = 2019 then IND_RETURNS_IN_SEASON+IND_RETURNS_EXT_SEASON else 0 end) as TY19_efiles
from pcg_dm.ENT_AGG_CUST_MEASURES
where can in
(select distinct can
from pcg_dm.ENT_AGG_CUST_MEASURES
where tax_year in (2019,2019)
)
group by 1
order by 1
limit 2000
) AS efile
join
(select can, st.STATE AS TY18_state
from pcg_dm.ENT_AGG_CUST_ATTRIBUTES AS st
where tax_year = 2018
limit 2000
)AS 18State
on
efile.can=18state.can
I get the following error.
[Code: 4856, SQL State: 42601] [Vertica]VJDBC ERROR: Syntax error at or near "18"
Any idea what the issue is?
You gave an alias name for the table in Line number 18 after the from clause. Try removing that and re-run the query.
Related
Helo!
How I make the following syntax of postgresql in SQL server without create subquery
PGSQL:
SELECT
COUNT(*) AS "QUANTIDADE TOTAL",
COUNT(*) FILTER(WHERE SEXO = 'Masculino') AS "MASCULINO"
FROM FUNCIONARIOS;
I tried but got an error:
Message 156, Level 15, State 1, Line 4
Incorrect syntax next to 'WHERE' keyword.
Try
SELECT COUNT(x) as [Quantudate Total],
SUM (CASE WHEN SEXO = 'Masculino' THEN 1
ELSE 0
END) AS MASCULINO
FROM FUNCIONARIOS
For better performance always better to use one specific field in the aggregate function than using a , eg use COUNT(id) than COUNT()
Conditional aggregation would work...
SELECT
COUNT(*) AS "QUANTIDADE TOTAL",
SUM(case when SEXO = 'Masculino' then 1 end) AS "MASCULINO"
FROM FUNCIONARIOS;
The code below works fine.
(SELECT (CASE
WHEN (PA.ACTOR_KIND = 5) THEN
(SELECT POP.KDS_TEKLIF_TUT_TL)
ELSE 0 END) AS Expr1) AS YKF_CUSTOMER_LIMIT
However,when I add another "when" condition it gives error.
(SELECT (CASE
WHEN (PA.ACTOR_KIND = 5) THEN
(SELECT POP.KDS_TEKLIF_TUT_TL)
WHEN (PA.ACTOR_KIND = 10) THEN
(SELECT POP.KDS_ALICI_LIM_TL)
ELSE 0 END) AS Expr1) AS YKF_CUSTOMER_LIMIT
It seems all logical to me but not for sql
**Edit It was caused by typo error,there is nothing wrong with those statements.
I don't know if this will fix your problem, but you don't need SELECT for the WHEN clauses:
SELECT (CASE WHEN PA.ACTOR_KIND = 5
THEN POP.KDS_TEKLIF_TUT_TL
WHEN PA.ACTOR_KIND = 10
THEN POP.KDS_ALICI_LIM_TL
ELSE 0
END) AS YKF_CUSTOMER_LIMIT
You don't need to give the expression a name in the subquery as well as in the outer query. One name is enough. (Although that doesn't cause your error.)
I am trying to write an sql statement but i am getting syntax error. I know it is do with my select and case statement but cant figure out.As the error is not descriptive. I am using redshift
select school_district_teacher_ind,customer_status,initial_pay_type,(select(
CASE
WHEN total_line_price = 0
THEN 'free'
ELSE 'paid'
END
)
from storiacloud.schl_storia_revenue_fact_a)as a,count(distinct convert(varchar(100),[Otc_Order_Number])+'_'+ convert(varchar(100),[Otc_Order_Line_Number]))
from storiacloud.schl_storia_revenue_fact_a as fact
inner join
storiacloud.schl_storia_school_status as status
on fact.school_ucn = status.ucn
where date = '11/2/2015'
group by school_district_teacher_ind,customer_status,initial_pay_type,a
Below is the error
ERROR: Invalid Query:
Detail:
-----------------------------------------------
error: Invalid Query:
code: 8001
context: single-row subquery returns more than one row
query: 5132289
location: 25.cpp:69
process: padbmaster [pid=29183]
-----------------------------------------------
Execution time: 0.16s
1 statement failed.
The results that i expect are
Note first column customer type is school_district_teacher_ind in the above select statment
Your case was a subquery that is selecting from the same table as your main query. Try this;
SELECT school_district_teacher_ind ,
customer_status ,
initial_pay_type ,
CASE WHEN total_line_price = 0 THEN 'free'
ELSE 'paid'
END AS a ,
COUNT(DISTINCT CONVERT(VARCHAR(100), [Otc_Order_Number]) + '_'
+ CONVERT(VARCHAR(100), [Otc_Order_Line_Number]))
FROM storiacloud.schl_storia_revenue_fact_a AS fact
INNER JOIN storiacloud.schl_storia_school_status AS status ON fact.school_ucn = status.ucn
WHERE date = '11/2/2015'
GROUP BY school_district_teacher_ind ,
customer_status ,
initial_pay_type ,
CASE WHEN total_line_price = 0 THEN 'free'
ELSE 'paid'
END
I think you just want conditional aggregation. The query is something like this:
select school_district_teacher_ind, customer_status, initial_pay_type,
sum(case when total_line_price = 0 then 1 else 0 end) as free,
sum(case when total_line_price = 0 then 0 else 1 end) as paid
from storiacloud.schl_storia_revenue_fact_a fact inner join
storiacloud.schl_storia_school_status status
on fact.school_ucn = status.ucn
where date = '2015-11-02'
group by school_district_teacher_ind,customer_status, initial_pay_type;
I'm getting the error " ORA-00907: missing right parenthesis" at row: 7, column: 6
when I launch this query on ORACLE 11g DB:
SELECT B.ID_COND AS IDCOND,B.ID_PAYMENT AS IDPAYMENT, B.DT_SCAD AS DATASCAD, B.DT_PAYMENT AS DATAPAYMENT,B.DE_CHANNELPAG AS CHANNELPAYMENT, B.DT_STARTVALID AS DATASTART, B.DT_ENDVALID AS DATAEND,
B.IM_TOTAL AS TOTAL, B.ST_PAYMENT AS STATECOND, B.CO_CIP AS codCIP, B.ID_PEND AS IDPEND, B.TI_PAYMENT AS TIPOPAYMENT,
B.cause_PAYMENT AS causaleCOND, B.IM_PAYMENT AS amountPAYMENT, B.DE_MIDDLEPAYMENT AS MIDDLEPAYMENT, B.DE_NOTEPAYMENT as notePAYMENT,
(select st_PAYMENT from QLT.paymnts P
where P.ID_COND=B.ID_COND
AND ROWNUM <= 1
order by (CASE WHEN P.st_PAYMENT='ES' THEN 1 ELSE 0 END) DESC, TS_INSMNT DESC
) AS STATEPAYMENT
FROM QLT.JLTCOPD B
WHERE B.ID_PEND = '269' AND B.TI_PAYMENT = 'S'
ORDER BY B.DT_SCAD ASC, DT_STARTVALID ASC
Trying to delete this row: 'order by (CASE WHEN P.st_PAYMENT='ES' THEN 1 ELSE 0 END) DESC, TS_INSMNT DESC' no errors are shown and a result is returned.
I'm trying to convert a DB2 query to ORACLE, the original DB2 inner query was:
(select st_PAYMENT from QLT.paymnts P where P.ID_COND=B.ID_COND order by (CASE WHEN P.st_PAYMENT='ES' THEN 1 ELSE 0 END) DESC, TS_INSMNT DESC fetch first 1 rows only)
How can I change it in order to do the porting without errors?
try replacing the inner query with:
(
select st_PAYMENT FROM
(
select st_PAYMENT from QLT.paymnts P
where P.ID_COND=B.ID_COND
order by (CASE WHEN P.st_PAYMENT='ES' THEN 1 ELSE 0 END) DESC, TS_INSMNT DESC
)
WHERE ROWNUM <= 1
)
I want to add some calculation inside my case statement to dynamically create the contents of a new column but I get the error:
Column 'Test1.qrank' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
This is the code I'm working on
case
when test1.TotalType = 'Average' then Test2.avgscore
when test1.TotalType = 'PercentOfTot' then (cnt/SUM(test1.qrank))
else cnt
end as displayscore
I did try to group but it didn't work.
Any hints?
The error you posted can happen when you're using a clause in the GROUP BY statement without including it in the select.
Example
This one works!
SELECT t.device,
SUM(case when transits.direction = 1 then 1 else 0 end) ,
SUM(case when transits.direction = 0 then 1 else 0 end) from t1 t
where t.device in ('A','B') group by t.device
This one not (omitted t.device from the select)
SELECT
SUM(case when transits.direction = 1 then 1 else 0 end) ,
SUM(case when transits.direction = 0 then 1 else 0 end) from t1 t
where t.device in ('A','B') group by t.device
This will produce your error complaining that I'm grouping for something that is not included in the select
Please, provide all the query to get more support.
You could use a Common Table Expression to create the SUM first, join it to the table, and then use the WHEN to to get the value from the CTE or the original table as necessary.
WITH PercentageOfTotal (Id, Percentage)
AS
(
SELECT Id, (cnt / SUM(AreaId)) FROM dbo.MyTable GROUP BY Id
)
SELECT
CASE
WHEN o.TotalType = 'Average' THEN r.avgscore
WHEN o.TotalType = 'PercentOfTot' THEN pt.Percentage
ELSE o.cnt
END AS [displayscore]
FROM PercentageOfTotal pt
JOIN dbo.MyTable t ON pt.Id = t.Id
If you're using SQL Server 2005 or above, you can use the windowing function SUM() OVER ().
case
when test1.TotalType = 'Average' then Test2.avgscore
when test1.TotalType = 'PercentOfTot' then (cnt/SUM(test1.qrank) over ())
else cnt
end as displayscore
But it'll be better if you show your full query to get context of what you actually need.