SQL查询超过两列

时间:2012-02-20 15:46:07

标签: sql sql-server-2008 sql-server-2005 tsql

我有两张表如下,

表名为Payment_transaction,列名为

payment_ref_id, booking_ref_id, amount, due_date

第二个表名为booking,其名称为booking_ref_id, date_created booking_status

我想写一个查询,它将返回每年的支付交易数量。

2 个答案:

答案 0 :(得分:1)

如果所有交易都保存在Payment_transaction中,那么如果您想计算payment_transactions而不是每笔交易的预订,则无需预订表。

查询应如下所示:

SELECT Year(due_date), COUNT(*) 
FROM Payment_transaction
GROUP BY Year(due_date)

答案 1 :(得分:1)

SELECT
    COUNT(p.booking_ref_id) AS payment_count,
    YEAR(b.date_created) AS payment_year
FROM booking b
INNER JOIN Payment_transaction p
    ON p.booking_ref_id = b.booking_ref_id
GROUP BY YEAR(b.date_created)