我試圖從一個表中匹配2nd
表中的另一列的列的記錄數。然後我需要它所選的另一列的總值。從2個表中選擇並獲得總計數
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」。