我正在尝试从一个表中获取列与表中另一列匹配的记录数2nd
。然后我需要它选择的另一列的总值。
SELECT
h.holiday_id,
h.holiday_name,
CONVERT(Money,b.total_balance) AS total_balance,
b.booking_status_id,
Sum(CONVERT(Money,b.total_balance)) AS total_balance,
Count(*) AS record_count
FROM
[arend].[aren1002].[HOLIDAY_REF] AS h,
[arend].[aren1002].[BOOKING] AS b
LEFT JOIN
[arend].[aren1002].[BOOKING]
ON
h.holiday_id=booking.holiday_id
WHERE
b.booking_status_id = '330' AND h.holiday_id = b.holiday_id
ORDER BY h.holiday_id
Table 1 HOLIDAY_REF
holiday_id | holiday_name
1 | Italy
2 | Russia
3 | Spain
Table 2 BOOKING
holiday_id | booking_status_id | total_balance
1 | 330 | 2500
3 | 330 | 1500
1 | 330 | 1750
2 | 330 | 1240
2 | 330 | 5600
Results:
Holiday_id | holiday_name | total_balance | record_count
1 | Italy | 4250 | 2
2 | Russia | 6840 | 2
3 | Spain | 1500 | 1
不确定我是否以正确的方式去做。
更新:我更新了 sql 命令以反映我必须到达的位置,我现在收到一个错误:无法绑定多部分标识符“h.holiday_id”。