group by无法正常连接两个表

时间:2017-02-08 18:31:46

标签: mysql sql sql-server join group-by

我想获得每个活动每种付款方式售出的门票数量。我跟随查询:

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

我得到了这个结果,这没关系:

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 视图结构如下:

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

我必须制作子查询吗?这里的问题在哪里?

3 个答案:

答案 0 :(得分:1)

MySQl允许您错误地使用分组依据。您永远不应该使用您在此查询中使用的技术。

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

当您以唯一的方式使用组来保证正确的结果时,将按所有非聚合字段进行分组。所有其他数据库都将此部分作为语法,因此没有此问题。

如果仍然没有给出正确的结果,那么你所写的内容的具体细节就会出现问题。我们需要查看业务需求,表结构,表中的示例数据以及样本结果,以帮助您找到正确的查询。

查看我在撰写本文时添加的其他详细信息,我认为您需要使用派生表。

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

答案 1 :(得分: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

答案 2 :(得分:0)

首先,这在一起没有意义:

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

您正在对具有相同 performance_id的内容进行分组,然后您要求向您显示该组中有多少 DISTINCT performance_ids。

猜猜答案永远是什么?

其次,SQL是一种声明性语言,因此请先声明您想要的内容。

如果你不能在SQL中充分表达它(这没关系) - 那么用文字表达,请用它来更新你的问题。 猜测缺乏投入很少有效。