I got this PostgreSQL query:
SELECT Sum(CASE
WHEN p LIKE '%add%'
AND p NOT LIKE '%add post%' THEN counter
ELSE 0
end) AS "AGREGAR",
Sum(IF p LIKE '%add%' AND p NOT LIKE '%add post%' THEN 1 ELSE 0 END IF ) AS P
FROM (SELECT l.action AS p,
Count(l.userid) AS counter
--r.name
FROM mdl_log AS l
JOIN mdl_role_assignments AS ra
ON l.userid = ra.userid
JOIN mdl_role AS r
ON ra.roleid = r.id
JOIN mdl_course AS c
ON c.id = l.course
AND course = 12033
AND roleid = 3
GROUP BY roleid,
l.action,
r.name
ORDER BY l.action) AS "P"
But when i execute it, i get this error:
ERROR: error de sintaxis en o cerca de «p»
LINE 6: Sum(IF p LIKE '%add%' AND p NOT LIKE '%add pos...
^
********** Error **********
ERROR: error de sintaxis en o cerca de «p»
SQL state: 42601
Character: 216
It's like if the engine don't recognise the "p" alias in the sub-query, but when I put "p" without anymore in the select, it works fine. What am I doing wrong?
Change the line with the if conditional to a normal case expression:
Sum(CASE WHEN p LIKE '%add%' AND p NOT LIKE '%add post%' THEN 1 ELSE 0 END) AS P
and you should be fine. The if statement is used in the PL/pgSQL language used in functions and triggers etc.
Related
SELECT A.SALE_ID,COUNT(A.SALE_ID) AS TOTAL_PENDING,C.ALLOTTE,C.FORM_NO,E.CUS_NAME
FROM B_UNIT_BOOKING_DETAIL A
JOIN COD_BOOKING_MASTER C ON A.SALE_ID=C.SALE_ID
JOIN COD_FORM_REG D ON C.FORM_NO=D.FORM_NO
JOIN COD_CUSTOMER_MASTER E ON E.CUS_ID=C.ALLOTTE
WHERE A.DUE_DATE < '20-AUG-21' AND A.RECEIVED='N' AND
(SELECT COUNT(B.SALE_ID) FROM B_UNIT_BOOKING_DETAIL B
WHERE B.DUE_DATE < '20-AUG-21' AND B.RECEIVED='N'
AND B.SALE_ID=A.SALE_ID GROUP BY B.SALE_ID ) > '2'
AND E.PROJECT_ID='4'
AND E.COMPANY_ID='2' AND C.FORM_NO NOT IN (SELECT REG_NO FROM COD_RECOVERY_RECORD) AND ROWNUM <= 50
GROUP BY A.SALE_ID ,C.ALLOTTE,C.FORM_NO,D.CUS_ID,E.CUS_NAME
This query shows the error invalid number but I check all the field every datatype is correct by still its showing error can anyone please tell me how I can do this.
SELECT A.SALE_ID,COUNT(A.SALE_ID) AS TOTAL_PENDING,C.ALLOTTE,C.FORM_NO,E.CUS_NAME
FROM B_UNIT_BOOKING_DETAIL A
JOIN COD_BOOKING_MASTER C ON A.SALE_ID=C.SALE_ID
JOIN COD_FORM_REG D ON C.FORM_NO=D.FORM_NO
JOIN COD_CUSTOMER_MASTER E ON E.CUS_ID=C.ALLOTTE
WHERE A.DUE_DATE < '20-AUG-21' AND A.RECEIVED='N' AND
(SELECT COUNT(B.SALE_ID) FROM B_UNIT_BOOKING_DETAIL B
WHERE B.DUE_DATE < '20-AUG-21' AND B.RECEIVED='N'
AND B.SALE_ID=A.SALE_ID GROUP BY B.SALE_ID ) > '2'
AND E.PROJECT_ID='4'
AND E.COMPANY_ID='2' AND D.FORM_NO NOT IN (SELECT REG_NO FROM COD_RECOVERY_RECORD) AND ROWNUM <= 50
GROUP BY A.SALE_ID ,C.ALLOTTE,C.FORM_NO,D.CUS_ID,E.CUS_NAME
I just change ALLASES OF FORM_NO TO GET BTHE RESULT.
I hope I can explain myself enough.
I'm working with JPA #Query. I need to do a Case-When.
If the id is from a certain company, return all employees, otherwise return only employees from that company
In PostgreSQL is working the query
SELECT * FROM VIEW_EMPLOYEE v
WHERE (v.name LIKE '%%' OR v.lastname LIKE '%%' OR v.sso LIKE '%%')
AND (CASE WHEN (SELECT c.acronym FROM company c WHERE id = 1) <> 'TH' THEN v.company_id = 1 ELSE TRUE END)
But when I code it in JPA #Query
#Query("SELECT u FROM ViewEmployee u WHERE (u.name LIKE %?1% OR u.lastname LIKE %?1% OR u.sso LIKE %?1%) " +
"AND (CASE WHEN (SELECT c.acronym FROM Company c WHERE c.id = ?2) <> 'TH' THEN u.viewEmployeeId.companyId = ?2 ELSE TRUE END)")
fun findEmployee(search: String, companyId: Long, pageable: Pageable): Page<ViewEmployee>
Give me this error message
unexpected token: = near line 1, column 251 [SELECT u FROM com.solucioneskuali.models.ViewEmployee u WHERE (u.name LIKE ?1 OR u.lastname LIKE ?1 OR u.sso LIKE ?1) AND (CASE WHEN (SELECT c.acronym FROM com.solucioneskuali.models.Company c WHERE c.id = ?2) <> 'TH' THEN u.viewEmployeeId.companyId = ?2 ELSE TRUE END)]
Can someone help to find what I'm doing wrong?
I am not good at SQL, I wanted to take MAX using partition by in the following query, but when I use the same query without where clause of that max drive column it says that column does not exist but if I remove the column from where I can see in select the same column is present.
select
MAX(case when total_split_count = 0 or total_split_count is null then total_split_count else 1 end) OVER (PARTITION BY ia.col1,ia.col2,ia.col3,ia.col4,ia.col5,ia.col6) as bb
from audits.tbl_name ia
where bb = 1
ERROR: column "bb" does not exist Position: 304
where bb = 1
^ 1 statement failed.
but the query runs with where clause:
select
MAX(case when total_split_count = 0 or total_split_count is null then total_split_count else 1 end) OVER (PARTITION BY ia.col1,ia.col2,ia.col3,ia.col4,ia.col5,ia.col6) as bb
from audits.tbl_name ia
Note: I created that column at run time through "as".
The alias defined in select clause in not visible in where clause.
Use
select * from (select ... as bb from audits.tbl_name ia) x where bb = 1
or CTE:
with x as (select ... as bb from audits.tbl_name ia) select * from x where bb = 1
I want to delete records from F table which has only one H record and that record has thrown 76 errorkode.
I am getting syntax error as i am joining and doing the count(*) check . Is there any correct way to do it???
delete from F fb where id in(select h.id from H h
join MI m on h.m_i_id=m.id
join ERROR e on e.m_i_id=m.id
join ERRORKODE ek on e.errorkode_id=ek.id where errorkode=76) and
select count(*) from H h where h.f_id = fb.id) =1
The where clause of delete has two conditions separated by and, the second one is query which should be in parenthesis. There is missing left parenthesis (before select keyword).
SELECT
cu.user_id,
cu.gender,
CASE WHEN cu.looking_for_gender = cu.gender THEN 1 ELSE 0 END AS
sexual_orientation,
os_name,
ROUND((DATE(NOW()) - cu.birthdate)/365.25) AS user_age,
SUM(dsb.likes) AS likes,
SUM(dsb.dislikes) AS dislikes,
SUM(dsb.blocks) AS blocks,
SUM(dsb.matches) AS matches,
SUM(dsb.received_likes) AS received_likes,
SUM(dsb.received_dislikes) AS received_dislikes,
SUM(dsb.received_blocks) AS received_blocks,
CASE WHEN cu.status = 'default' THEN 1 ELSE 0 END AS recall_case,
CASE WHEN cu.status = 'default' THEN extract(epoch from
cu.last_activity - cu.updated_time)/86400 ELSE 0 END AS
recall_retention
FROM ( SELECT stats.core_users cu
LEFT JOIN yay.daily_swipes_by_users dsb ON (dsb.user_id = cu.user_id)
WHERE cu.user_id = '1' GROUP BY 1) e1
LEFT JOIN LATERAL (SELECT cd.os_name FROM stats.core_devices cd WHERE
e1.user_id = cd.user_id ORDER BY cd.updated_time DESC LIMIT 1) e2
ON TRUE;
Current Error Code:
ERROR: syntax error at or near "LEFT"
LINE 18: LEFT JOIN yay.daily_swipes_by_users dsb ON (dsb.user_id = cu...
^
The supplied query would fail in many ways, the following might work I hope, but as you can see this drops a great deal of other columns in the process
SELECT
e1.user_id
, e1.cu
, e2.os_name
FROM (
SELECT stats.core_users cu, cu.user_id
LEFT JOIN yay.daily_swipes_by_users dsb ON (dsb.user_id = cu.user_id)
WHERE cu.user_id = '1'
GROUP BY stats.core_users cu, cu.user_id
) e1
LEFT JOIN LATERAL(
SELECT cd.os_name
FROM stats.core_devices cd
WHERE e1.user_id = cd.user_id
ORDER BY cd.updated_time DESC
LIMIT 1) e2 ON TRUE
;
SELECT
cu.user_id,
cu.gender,
CASE WHEN cu.looking_for_gender = cu.gender THEN 1 ELSE 0 END AS sexual_orientation,
e2.os_name,
ROUND((DATE(NOW()) - cu.birthdate)/365.25) AS user_age,
CASE WHEN cu.status = 'default' THEN 1 ELSE 0 END AS recall_case,
CASE WHEN cu.status = 'default' THEN extract(epoch from cu.last_activity - cu.updated_time)/86400 ELSE 0 END AS recall_retention,
SUM(dsb.likes) AS likes,
SUM(dsb.dislikes) AS dislikes,
SUM(dsb.blocks) AS blocks,
SUM(dsb.matches) AS matches,
SUM(dsb.received_likes) AS received_likes,
SUM(dsb.received_dislikes) AS received_dislikes,
SUM(dsb.received_blocks) AS received_blocks
FROM
stats.core_users cu
LEFT JOIN yay.daily_swipes_by_users dsb ON (dsb.user_id = cu.user_id)
LEFT JOIN LATERAL (SELECT cd.os_name FROM stats.core_devices cd WHERE cu.user_id = cd.user_id ORDER BY cd.updated_time DESC LIMIT 1) e2
ON TRUE
WHERE cu.user_id = '1'
GROUP BY 1,2,3,4,5,6,7
;
This works.