-2

我的 sql 有问题。我尝试在我的声明中将最大数量总和设为一行,但它不起作用。警告消息在 ')' 附近显示不正确的语法。这是我的代码:-

select sum(A) from (select t.ticketid, lt.description ticketsource,
c.description tickedcreted,
max_score = isnull(
(select max(si.marks) as max_score
from survey_items si
left join survey_questions sq on (si.question_id = sq.question_id)
left join survey s on (sq.survey_id = s.survey_id
and s.code_interaction= 1)),0
),
sum(si.marks) totalAnswer,

count(t.ticketid) totalticketcreated,
count(t.feedback) totalfeedbackreceived
from  survey_items si
left join lookup_questions lq on (si.item_id = lq.item_id)
left join feedback fb on (fb.feedback_id = lq.feedback_id)
left join ticket t on (t.ticketid = fb.ticketid)
left join survey_questions sq on (si.question_id = sq.question_id)
left join survey s on (sq.survey_id = s.survey_id)
left join lookup_assignedOfficer c on (c.code = t.enteredby )
left join lookup_department ld on t.department_code =ld.code
left join lookup_ticketsource lt on lt.code = s.code_interaction
where t.trashed_date is null and lt.enabled =1 and ld.description is not null
and
((t.assigned_date BETWEEN '7/16/2012' AND '7/16/2012'))
)
4

1 回答 1

2

当使用从 select 语句中检索的结果集作为表时,您必须将别名传递给表。

尝试这个:

select sum(A) from 
(
    select t.ticketid, lt.description ticketsource,
    c.description tickedcreted,
    max_score = isnull(
    (select max(si.marks) as max_score
    from survey_items si
    left join survey_questions sq on (si.question_id = sq.question_id)
    left join survey s on (sq.survey_id = s.survey_id
    and s.code_interaction= 1)),0
    ),
    sum(si.marks) totalAnswer,

    count(t.ticketid) totalticketcreated,
    count(t.feedback) totalfeedbackreceived
    from  survey_items si
    left join lookup_questions lq on (si.item_id = lq.item_id)
    left join feedback fb on (fb.feedback_id = lq.feedback_id)
    left join ticket t on (t.ticketid = fb.ticketid)
    left join survey_questions sq on (si.question_id = sq.question_id)
    left join survey s on (sq.survey_id = s.survey_id)
    left join lookup_assignedOfficer c on (c.code = t.enteredby )
    left join lookup_department ld on t.department_code =ld.code
    left join lookup_ticketsource lt on lt.code = s.code_interaction
    where t.trashed_date is null and lt.enabled =1 and ld.description is not null
    and
    ((t.assigned_date BETWEEN '7/16/2012' AND '7/16/2012'))
) AS result

其次,您试图找到 A 列的总和,但结果集中不存在 A 列。

于 2012-07-29T10:26:03.467 回答