SQL Group By和基于怪异客户端表的Join

SQL Group By和基于怪异客户端表的Join,sql,join,sum,Sql,Join,Sum,我有3个表,我想连接在一起,并将其分组以获取客户成员信息。我的代码用于将基表分组在一起,但它在连接部分中断,我不知道为什么 BASE TABLE : sales_detail +-------+-----------+-----------+-----------------------------------------+ | order_date | transaction_id| product_cost | payment_type | country +------

我有3个表,我想连接在一起,并将其分组以获取客户成员信息。我的代码用于将基表分组在一起,但它在连接部分中断,我不知道为什么

BASE TABLE : sales_detail 
+-------+-----------+-----------+-----------------------------------------+
|   order_date | transaction_id|   product_cost |  payment_type  |    country
+-------+-----------+-----------+------------------------------------------+
|   10/1     |   12345         |      20       |      mastercard |    usa
|   10/1     |   12345         |      50       |      mastercard |    usa
|   10/5     |  82456          |      50       |      mastercard |    usa
|   10/9     |  64789          |      30       |      visa       |    canada
|   10/15    |  08546          |      20       |      mastercard |    usa
|   10/15    |  08546          |      90       |      mastercard |    usa
|   10/17    |  65898          |       50      |      mastercard |   usa
+-------+-----------+-----------+-------------------------------------+
**我希望输出像这样:**

IDEAL OUTPUT
+-------+-----------+-----------+--------------------------------+
|   order_date | transaction_ID |   product_cost |  client_Type|   
+-------+-----------+-----------+--------------------------------+
|   10/1     |   12345         |      70       |      new        |  
|   10/5     |   82456         |      70       |      old        |
|   10/15    |   08546         |      110      |      old        |
|   10/17    |   65898         |      50       |      new        |
+-------+-----------+-----------+----------------------------------+
**我正试图通过事务ID将我的基表连接到连接器表,以获取其他_ID和项目,以与客户端_类型匹配**

这是我使用的代码,但在添加左连接后未能编译:

select t1.transaction_id, sum(t1.product_cost), t1.order_date, t3.client_type
from sales_detail t1
left join (select DISTINCT  transaction_ID, other_id, fruits from connector) t2
ON t1.transaction_ID=t2.transaction_ID
left join (select DISTINCT order_id, client_type, fruits from client information) t3 
ON t2.other_id=t3.other_id and t2.item=t3.item
where t1.payment_type='mastercard' and t1.order_Date between '2020-10-01' and'2020-10-31'
and country != 'canada'
GROUP BY t1.transaction_id, t1.order_date, t3.client_type;

提前谢谢!我是一个初学者,所以仍然在学习sql的细节!我正在使用hive,我认为这就是连接和聚合。为了提高效率,您可以在子查询中预聚合,然后加入:

select sd.*, ci.client_type
from (
    select order_date, transaction_id, sum(product_cost) product_cost
    from sales_detail
    where 
        payment_type   =  'mastercard' 
        and order_date >= '2020-10-01' 
        and order_date <  '2020-11-01'
        and country    <>  'canada'
    group by order_date, transaction_id
) sd
inner join connector c on c.transaction_id = sd.transaction_id
inner join client_information ci on ci.other_id = c.other_id

请注意,我在订单日期重新编写了过滤器,以使用半开间隔,而不是中间间隔。这可以很好地处理日期有时间段的情况。

据我所知,您的代码可以正常工作,但使用内部联接时可能会出现问题,并且无法添加左联接。我认为发生的是由于空元素导致的失败。要添加NULL元素而不出现错误,必须使用将NULL值更改为0的函数


其中一个函数是T-SQL的ISNULLyourColumn,0函数。

我可以看到,在结果表中,您只需要使用mastercard的客户机,因此您应该在那里使用内部联接,以便只考虑使用mastercard的客户机。虽然我想剩下的查询还可以,但主要的问题是连接客户机信息。

我认为在GMB的答案中,您还需要连接项目列,否则您将获得多行输出

select sd.*, ci.client_type
from (
    select order_date, transaction_id, sum(product_cost) product_cost
    from sales_detail
    group by order_date, transaction_id
) sd
inner join connector c on c.transaction_id = sd.transaction_id
inner join client_information ci on ci.other_id = c.other_id and ci.item = c.item

只需使用过滤器进行修改,您就可以进行排序。

我删除了不一致的数据库标记。请仅使用您真正使用的数据库进行标记。谢谢您的帮助!问题是,这在sql fiddle上非常有效,但对于实际数据,运行时非常慢。有没有什么好方法可以加快查询速度?@eazyz:这看起来是另一个问题,您需要提供更多信息表结构、现有索引、执行计划等。。。。我只能推荐,并为此。谢谢你的作品!问题是,这在sql fiddle上非常有效,但对于实际数据,运行时非常慢。有没有一个好的方法可以加快查询速度?因为我不知道你的模式是什么样子,有多少数据和其他细节,所以我不能提供任何关于如何做的猜测。请在适当的地方标出答案,然后再问另一个问题。
select sd.*, ci.client_type
from (
    select order_date, transaction_id, sum(product_cost) product_cost
    from sales_detail
    where 
        payment_type   =  'mastercard' 
        and order_date >= '2020-10-01' 
        and order_date <  '2020-11-01'
        and country    <>  'canada'
    group by order_date, transaction_id
) sd
inner join connector c on c.transaction_id = sd.transaction_id
inner join client_information ci on ci.other_id = c.other_id
select sd.*, ci.client_type
from (
    select order_date, transaction_id, sum(product_cost) product_cost
    from sales_detail
    group by order_date, transaction_id
) sd
inner join connector c on c.transaction_id = sd.transaction_id
inner join client_information ci on ci.other_id = c.other_id and ci.item = c.item