我想要獲取每個事件的每種付款方式銷售的門票數量。而我後續的查詢:分組依據不能正確加入兩張表
SELECT count(distinct(a.performance_id)) as EventQuantity,
sum(a.admission_count) as TicketQuantity
FROM vw_PrecioTipoZona_Paid as a
WHERE 1 = 1
AND a.performance_id ='DED63133-A099-4949-AA57-13BBE9462BAF'
GROUP BY a.performance_id
,我得到這樣的結果,這是OK:
EventQuantity TicketQuantity
1 203
但與其他參加表時,結果是至極之和,在這種情況下a.admission_count
乘以另一個表中的記錄數。
誰有問題的查詢是這樣的:
SELECT a.performance_id,
count(distinct(a.performance_id)) as EventQuantity,
sum(a.admission_count) as TicketQuantity,
b.payment_method as PaymentMethod
FROM vw_PrecioTipoZona_Paid as a inner join vw_Payment_UserByPerformance as b
on a.performance_id = b.performance_id
WHERE
1 = 1
and a.performance_id ='DED63133-A099-4949-AA57-13BBE9462BAF'
group by a.performance_id, b.payment_method
與此查詢我得到這個結果:
EventQuantity TicketQuantity PaymentMethod
1 10353 Cash
1 5887 Card
1 1624 MasterCardECommerce
1 812 VisaEcommece
而這個結果是wron,結果應該是:
EventQuantity TicketQuantity PaymentMethod
1 111 Cash
1 63 Card
1 17 MasterCardECommerce
1 8 VisaEcommece
vw_Payment_UserByPerformance view structure i S中的如下:
performance_id user_role_id userrole_name userrole_group date_transaction user_id user_name owner_user_id owner_user_name amount_adm_net amount_req_net amount_charge_charge amount_total amount_net chargeTransaction tax payment_method
而且vw_PrecioTipoZona_Paid視圖結構是如下:
performance_id performance_name performance_start_date date_transaction user_role_id userrole_name userrole_group user_id user_name price_type price_zone price_zone_priority admission_count NET charge1 charge2 charge3 charge4 charge5 GROSS
我必須做的子查詢?問題在哪裏?
預期結果集是什麼?另外,由於你是由同一個字段分組,因此'count(distinct(a.performance_id))'有什麼意義? –
將所有標識(本例中的表和列名稱)翻譯爲英文將有所幫助,因此我們可以更好地理解表格的語義。 – pintxo
@GiorgosBetsos 我也想知道每種活動每種付款的門票數量。 –