2017-02-08 22 views
1

我想要獲取每個事件的每種付款方式銷售的門票數量。而我後續的查詢:分組依據不能正確加入兩張表

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 

我必須做的子查詢?問題在哪裏?

+0

預期結果集是什麼?另外,由於你是由同一個字段分組,因此'count(distinct(a.performance_id))'有什麼意義? –

+0

將所有標識(本例中的表和列名稱)翻譯爲英文將有所幫助,因此我們可以更好地理解表格的語義。 – pintxo

+0

@GiorgosBetsos 我也想知道每種活動每種付款的門票數量。 –

回答

0

你有它在你的問題已經:

我想每個付款方式售出的彩票號碼和我 後續查詢

所以你基本上看在這樣的查詢:

SELECT payment_method, COUNT(*) FROM x GROUP BY payment_method; 

你似乎有不同的做一些事情,結果可能喜歡就好,所以如果你對組payment_method列,以及:

SELECT  
    a.performance_id AS performanceId, 
    b.payment_method AS paymentMethod, 
    COUNT(*) numPayments 
FROM 
    vw_PrecioTipoZona_Paid as a 
INNER JOIN 
    vw_Payment_UserByPerformance AS b ON (a.performance_id = b.performance_id) 
WHERE 
    a.performance_id ='DED63133-A099-4949-AA57-13BBE9462BAF' 
GROUP BY 
    a.performance_id, 
    b.payment_method 
+0

不是@pintxo,我不適合我。我想要每個付款方式的門票銷售數量,它應該是這樣的:Mastercard 3,Visa 1 –

0

首先,這是沒有意義的在一起:

SELECT a.performance_id, 
     count(distinct(a.performance_id)) as EventQuantity, 
... 
GROUP BY a.performance_id 

你有分組的相同 performance_id的東西,然後你問展現您有多少個DISTINCT performance_ids在組中。

猜猜答案始終是什麼?

二,SQL是聲明式語言,所以首先聲明你想要的。

如果你不能充分表達它(這是好的) - 然後用文字表達,並請用它更新你的問題。 猜測缺乏投入很少有生產力。

0

MySQl允許您錯誤地使用group by。你不應該使用你在這個查詢中使用的技術。

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 
    a.performance_id ='DED63133-A099-4949-AA57-13BBE9462BAF' 
    group by a.performance_id, b.payment_method 

當您使用GROUP BY只有這樣,才能保證正確的結果是組由所有非聚集的領域。所有其他數據庫都構成這部分語法,因此不存在此問題。

如果這仍然不能給出正確的結果,那麼您寫的內容的具體內容存在問題。我們需要查看業務需求,表結構,表中的示例數據和示例結果,以幫助您找到正確的查詢。

看我在寫這篇文章時增加了額外的細節,我想你需要使用派生表。

SELECT  a.performance_id, 
      count(a.performance_id) as EventQuantity, 
      a.admission_count as TicketQuantity, 
      b.payment_method as PaymentMethod 
FROM (select performance_id, sum(admission_count) as Admissioncount vw_PrecioTipoZona_Paid 
WHERE a.performance_id ='DED63133-A099-4949-AA57-13BBE9462BAF' 
group by performance_id)as a 
inner join vw_Payment_UserByPerformance as b 
     on a.performance_id = b.performance_id 
    group by a.performance_id, b.payment_method 
+0

此查詢返回與我擁有的結果相同的結果。 –