SQL格式 - 需要帮助

时间:2014-04-15 10:13:33

标签: sql sql-server sql-server-2008

早上好,

提前道歉但我仍然相对较新的SQL。

我正在运行下面的脚本时遇到此错误,但我不确定原因。它在where和group by子句中指定。你能给我一些关于我哪里出错的指示吗?

8120,Level 16,State 1,Line 4 列'contact_legacies.contact_number'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

SELECT
cl.contact_number as contact_number,
cl.legacy_number as legacy_number,
c.label_name as label_name,
cl.source_date as source_date,
cl.legacy_status as legacy_status,
lb.expected_value as total_expected,
sum(lbr.amount) as total_received,
sum(lb.expected_value)- sum(lbr.amount) as amount_outstanding, 
cl.agency_notification_date as date_notified_by_agency,
cl.death_notification_date as executor_notification_date,
cl.date_of_death as date_of_death
--lb.bequest_status as bequest_status,
--lb.bequest_type as bequest_type,
--lb.bequest_sub_type as bequest_sub_type,
--a.address_line1 as address_line1,
--a.address_line2 as address_line2,
--a.town as town,
--a.county as county,
--a.postcode as postcode,
--a.country as country

FROM
contact_legacies cl

INNER JOIN contacts c ON cl.contact_number = c.contact_number
INNER JOIN legacy_bequests lb ON cl.legacy_number = lb.legacy_number
INNER JOIN addresses a on a.address_number = c.address_number
INNER JOIN legacy_bequest_receipts lbr on cl.legacy_number = lbr.legacy_number

WHERE
cl.legacy_status = 'IP' --AND
--cl.source_date between %1 AND %2

ORDER BY
cl.contact_number,cl.legacy_number,c.label_name,cl.source_date,cl.legacy_status,lb.expected_value,sum(lbr.amount),
sum(lb.expected_value)- sum(lbr.amount),cl.agency_notification_date,cl.death_notification_date,cl.date_of_death

我还想添加一个WHERE子句,它只考虑SELECT语句中的amount_outstanding,其值大于零。我该怎么做?

提前致谢。

2 个答案:

答案 0 :(得分:2)

在您的查询中,ORDER BY不是GROUP BY子句:)。

关于您的WHERE问题,您应该使用HAVING条款

HAVING sum(lb.expected_value)- sum(lbr.amount) > 0 

所以你的查询应该是

SELECT
cl.contact_number as contact_number,
cl.legacy_number as legacy_number,
c.label_name as label_name,
cl.source_date as source_date,
cl.legacy_status as legacy_status,
lb.expected_value as total_expected,
sum(lbr.amount) as total_received,
sum(lb.expected_value)- sum(lbr.amount) as amount_outstanding, 
cl.agency_notification_date as date_notified_by_agency,
cl.death_notification_date as executor_notification_date,
cl.date_of_death as date_of_death
--lb.bequest_status as bequest_status,
--lb.bequest_type as bequest_type,
--lb.bequest_sub_type as bequest_sub_type,
--a.address_line1 as address_line1,
--a.address_line2 as address_line2,
--a.town as town,
--a.county as county,
--a.postcode as postcode,
--a.country as country

FROM
contact_legacies cl

INNER JOIN contacts c ON cl.contact_number = c.contact_number
INNER JOIN legacy_bequests lb ON cl.legacy_number = lb.legacy_number
INNER JOIN addresses a on a.address_number = c.address_number
INNER JOIN legacy_bequest_receipts lbr on cl.legacy_number = lbr.legacy_number

WHERE
cl.legacy_status = 'IP'

GROUP BY
cl.contact_number,cl.legacy_number,c.label_name,cl.source_date,cl.legacy_status,lb.expected_value,cl.agency_notification_date,cl.death_notification_date,cl.date_of_death

HAVING sum(lb.expected_value)- sum(lbr.amount) > 0 

ORDER BY
cl.contact_number,cl.legacy_number,c.label_name,cl.source_date,cl.legacy_status,lb.expected_value,sum(lbr.amount),
sum(lb.expected_value)- sum(lbr.amount),cl.agency_notification_date,cl.death_notification_date,cl.date_of_death

GROUP BY子句中,您需要添加所有不在聚合函数中的字段(sum,count,max,min等)

您可以在此处详细了解聚合:http://www.w3schools.com/sql/sql_functions.asp
关于HAVING条款,您可以在此处阅读:http://www.w3schools.com/sql/sql_having.asp

答案 1 :(得分:0)

每当你的选择列表中有一个聚合函数 - SUM()AVG()等 - 你必须在查询中有一个GROUP BY子句。它必须列出不在聚合函数中的所有列。例外情况是您的选择列表只有 聚合函数;然后你可以省略GROUP BY

有人会认为编译器应该能够自己解决这个问题,但事实并非如此。你必须写它。