如何将所有字段选择语句加总为一行?

时间:2012-07-29 09:39:52

标签: sql sql-server sql-server-2005

我的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'))
)

1 个答案:

答案 0 :(得分:2)

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

试试这个:

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列。