I have a problem with a subquery in my query.
In this query :
SELECT *
FROM Statistic_RecordedConversations A
JOIN (SELECT DID, max(DateTime) MaDate
FROM Statistic_RecordedConversations
where DID IN (
Select OpenData as DID, QuestionID, InterviewID
from Surveys.dbo.Askia2363Data
where QuestionID = 895
and InterviewID in (
select Surveys.dbo.Askia2363Data.InterviewID
from Surveys.dbo.Askia2363Interview,
Surveys.dbo.Askia2363Data
where Surveys.dbo.askia2363Interview.InterviewID =
Surveys.dbo.Askia2363Data.InterviewID
and completed = 1
and QuestionID = 891
and ClosedData = 4685
and CAST(EndInterview as Date) =
CAST (Current_TIMESTAMP as Date)))
GROUP BY DID ) B
ON A.DID = B.DID
AND A.DateTime = B.MaDate
I got an error with msg 116. I think its due to the ligne 6 where I have not only 1 column. I don't know how to get this query working. I tried EXISTS insteand of IN but got too many results and not the results that are interesting to me.
If someone got another solution, many thanks! :)
You can only return one field to correlate inside of an IN statement. You need to only return DID.
where DID IN ( Select OpenData as DID from Surveys.dbo.Askia2363Data...
Related
I am trying to use COUNT(DISTINC column) OVER(PARTITION BY column) when I am using COUNT + window function(OVER).
I get an error like the one in the title and can't get it to work.
I have looked into how to deal with this error, but I have not found an example of how to deal with such a complex query as the one below.
I cannot find an example of how to deal with such a complex query as shown below, and I am not sure how to handle it.
The COUNT part of the problem exists on line 65.
How can such a complex query be resolved without slowing down?
WITH RECURSIVE "cte" AS((
SELECT
"videos_productvideocomment"."id",
"videos_productvideocomment"."user_id",
"videos_productvideocomment"."video_id",
"videos_productvideocomment"."parent_id",
"videos_productvideocomment"."text",
"videos_productvideocomment"."commented_at",
"videos_productvideocomment"."edited_at",
"videos_productvideocomment"."created_at",
"videos_productvideocomment"."updated_at",
"videos_productvideocomment"."id" AS "root_id"
FROM
"videos_productvideocomment"
WHERE
(
"videos_productvideocomment"."parent_id" IS NULL
AND "videos_productvideocomment"."video_id" = 'f264433c-c0af-49cc-8b40-84453da71b2d'
)
) UNION(
SELECT
"videos_productvideocomment"."id",
"videos_productvideocomment"."user_id",
"videos_productvideocomment"."video_id",
"videos_productvideocomment"."parent_id",
"videos_productvideocomment"."text",
"videos_productvideocomment"."commented_at",
"videos_productvideocomment"."edited_at",
"videos_productvideocomment"."created_at",
"videos_productvideocomment"."updated_at",
"cte"."root_id" AS "root_id"
FROM
"videos_productvideocomment"
INNER JOIN
"cte"
ON "videos_productvideocomment"."parent_id" = "cte"."id"
))
SELECT
*,
EXISTS(
SELECT
(1) AS "a"
FROM
"videos_productvideolikecomment" U0
WHERE
(
U0."comment_id" = t."id"
AND U0."user_id" = '3bd3bc86-0335-481e-9fd2-eb2fb1168f48'
)
LIMIT 1
) AS "liked"
FROM
(
SELECT DISTINCT
"cte"."id",
"cte"."created_at",
"cte"."updated_at",
"cte"."user_id",
"cte"."text",
"cte"."commented_at",
"cte"."edited_at",
"cte"."parent_id",
"cte"."video_id",
"cte"."root_id" AS "root_id",
COUNT(DISTINCT "cte"."root_id") OVER(PARTITION BY "cte"."root_id") AS "reply_count", <--- here
COUNT("videos_productvideolikecomment"."id") OVER(PARTITION BY "cte"."id") AS "liked_count"
FROM
"cte"
LEFT OUTER JOIN
"videos_productvideolikecomment"
ON (
"cte"."id" = "videos_productvideolikecomment"."comment_id"
)
) t
WHERE
t."id" = t."root_id"
ORDER BY
CASE
WHEN t."user_id" = '3bd3bc86-0335-481e-9fd2-eb2fb1168f48' THEN 0
ELSE 1
END ASC,
"liked_count" DESC
DISTINCT will look for duplicates and remove it, but in big data it will take a lot of time to process this query, you should process the middle of the record in the programming part I think it will be fast than. Thank
I am trying to execute the below query and getting Invalid SQL Syntax error
. [IBM][CLI Driver] CLI0118E Invalid SQL syntax. SQLSTATE=37000(37000,-99999). Is it anything to do with driver upgrade? It was working fine a while ago. Please advice. Thanks in advance.
select a.name_task as nameTask, a.cd_sts as cdSts, a.desc_err_msg as
statusDesc
from task_log a,(
select id_bus_procss, name_task , id_run,
max(dt_lst_updt) as dt_lst_updt from task_log
where id_run = '1'
and id_bus_procss = '14'
and name_task in ({0})
and dt_lst_updt >= (
select dt_evnt_sts from
sf_evntflow_sts
where id_run = '1' and
id_evntflow ='15'
and cd_evnt_sts in (''CLN'',''RTY'' )
)
group by id_bus_procss, name_task, id_run)
X
where a.dt_lst_updt = X.dt_lst_updt
if you are executing this on stored procedure , Try this may help, Remove the new line character and separate the parameters by
spaces .
Or otherwise try upgrade.......
if you try this query its better?
select a.name_task as nameTask, a.cd_sts as cdSts, a.desc_err_msg as statusDesc
from task_log a,
( select b.id_bus_procss, b.name_task , b.id_run, max(b.dt_lst_updt) as dt_lst_updt
from task_log b inner join sf_evntflow_sts c on b.id_run=c.id_run and c.id_evntflow ='15'
and c.cd_evnt_sts in (''CLN'',''RTY'' ) and b.dt_lst_updt >=c.dt_evnt_sts
where b.id_run = '1' and b.id_bus_procss = '14' and b.name_task in ({0})
group by b.id_bus_procss, b.name_task, b.id_run
) as X
where a.dt_lst_updt = X.dt_lst_updt
I'm creating a view in MariaDB and i'm having trouble making it work for a couple of fields. Currently this is working:
( SELECT DISTINCT IFNULL(grades.`grade`,'No Grade')
FROM `table` grades
WHERE userinfo.`id` = grades.`id`
AND grades.`Item Name` = 'SOMEINFO'
) 'SOMENAME',
But i need to add a select where the 'No grade' is, in the following form
( SELECT DISTINCT IFNULL( grades.`grade`,
SELECT IF( EXISTS
( SELECT *
FROM `another_table`
WHERE userid = 365
AND courseid = 2
), 'Enrolled', 'Not enrolled'
)
)
FROM `table` grades
WHERE userinfo.`id` = grades.`id`
AND grades.`Item Name` = 'SOMEINFO'
) 'SOMENAME',
i know that
SELECT IF( EXISTS( SELECT *
FROM `another_table`
WHERE userid = 365
AND courseid = 2
),
'Enrolled', 'Not enrolled'
)
is working too, but now the whole thing it's giving me an error, so any suggestions would be greatly appreciated
Thanks
This looks like a subquery:
(SELECT DISTINCT IFNULL(grades.`grade`,
SELECT IF( EXISTS (SELECT *
FROM `another_table`
WHERE userid = 365 AND courseid = 2
), 'Enrolled', 'Not enrolled'
)
)
FROM `table` grades
WHERE userinfo.`id` = grades.`id` AND
grades.`Item Name` = 'SOMEINFO'
) as SOMENAME,
You are using a subquery that returns two columns in a position where a scalar subquery is expected. A scalar subquery returns one column in at most one row.
Unfortunately, there is no easy way to do what you want in MySQL, because of the restrictions on views. I would advise you to rewrite the logic so the exists is handled using a left join in the from clause.
I'm struggling here trying to write a script that finds where an order was returned multiple times by the same associate (count greater than 1). I'm guessing my syntax with the subquery is incorrect. When I run the script, I get a message back that the "SELECT failed.. [3669] More than one value was returned by the subquery."
I'm not tied to the subquery, and have tried using just the group by and having statements, but I get an error regarding a non-aggregate value. What's the best way to proceed here and how do I fix this?
Thank you in advance - code below:
SEL s.saletran
, s.saletran_dt SALE_DATE
, r.saletran_id RET_TRAN
, r.saletran_dt RET_DATE
, ra.user_id RET_ASSOC
FROM salestrans s
JOIN salestrans_refund r
ON r.orig_saletran_id = s.saletran_id
AND r.orig_saletran_dt = s.saletran_dt
AND r.orig_loc_id = s.loc_id
AND r.saletran_dt between s.saletran_dt and s.saletran_dt + 30
JOIN saletran rt
ON rt.saletran_id = r.saletran_id
AND rt.saletran_dt = r.saletran_dt
AND rt.loc_id = r.loc_id
JOIN assoc ra --Return Associate
ON ra.assoc_prty_id = rt.sls_assoc_prty_id
WHERE
(SELECT count(*)
FROM saletran_refund
GROUP BY ORIG_SLTRN_ID
) > 1
AND s.saletran_dt between '2015-01-01' and current_date - 1
Based on what you've got so far, I think you want to use this instead:
where r.ORIG_SLTRN_ID in
(select
ORIG_SLTRN_ID
from
saletran_refund
group by ORIG_SLTRN_ID
having count (*) > 1)
That will give you the ORIG_SLTRN_IDs that have more than one row.
you don't give enough for a full answer but this is a start
group by s.saletran
, s.saletran_dt SALE_DATE
, r.saletran_id RET_TRAN
, r.saletran_dt RET_DATE
, ra.user_id RET_ASSOC
having count(distinct(ORIG_SLTRN_ID)) > 0
this does return more the an one row
run it
SELECT count(*)
FROM saletran_refund
GROUP BY ORIG_SLTRN_ID
I have a problem with the following HQL query:
select sum(MYTABLE.COUNTER) from (
select count(DISTINCT bi.products.id) as COUNTER
from BusinessInformation as bi
where bi.informationOwners.id in (100)
and bi.products.id in (10)
and bi.valueAmount not in ('NA')
and ((bi.valueType = 'ACHIEVED' and bi.referenceYears.id = 1) or (bi.valueType = 'FINAL_BALANCE' and bi.referenceYears.id = 2))
group by bi.informationOwners.id
) MYTABLE
The compiler reports:
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 34
Do you have any idea what's wrong with the query? I tested the inner query and it works fine.
Thanks,
C
HQL subqueries can occur only in the select or where clauses, not FROM.