sql group by和join基于一个奇怪的客户机表

zour9fqk  于 2021-07-26  发布在  Java
关注(0)|答案(4)|浏览(280)

我有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
+-------+-----------+-----------+-------------------------------------+
table : client_information
+-------+-----------+-----------+-------------------+
|   other_id    | client_Type       |   item  
+-------+-----------+-----------+----------+
|   112341        |   new        | hola        |
|   112341        |   old        | mango       |
|   145634       |   old         | pine        | 
|   879547       |   old         | vip         |
|   745688       |   new         | unio        |  
|   745688       |   old         | dog         |
|   147899       |   new         | cat         |
|   124589        |   new        | amigo         |
+-------+-----------+-----------+-----------+
table : connector
+-------+-----------+-----------+-------------------+
|   transaction_ID | other_id    |   item  
+-------+-----------+-----------+----------+
|   12345        |   112341       | hola      |
|   82456        |   145634       | pine      | 
|   08157        |   879547       | unio      |
|   08546        |   745688       | dog       |  
|   65898        |   147899       | cat       |
|   06587        |   124589       | amigo     |
+-------+-----------+-----------+-----------+

我希望输出像这样:

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将我的基表连接到连接器表,以获取与客户端类型匹配的其他\u 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)

gmxoilav

gmxoilav1#

我认为这是连接和聚合。为了提高效率,您可以在子查询中预聚合,然后加入:

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

请注意,我重写了上的过滤器 order_date 使用半开间隔而不是 between . 当你的约会有一段时间的时候,这会很好的处理这个问题。

3zwtqj6y

3zwtqj6y2#

据我所知,你的代码工作,虽然不是你想使用 INNER JOIN 它没有添加一个 LEFT JOIN . 我认为所发生的事情是由于 NULL 元素。添加 NULL 元素,而不是得到一个错误,您必须使用一些函数来更改 NULL 价值 0 .
其中一个功能就是 ISNULL(yourColumn, 0) t-sql的函数。文档。

bis0qfac

bis0qfac3#

我可以看到,在结果表中,您只需要使用mastercard的客户机,所以您应该在那里使用内部连接,这样只有使用mastercard的客户机才会被考虑。我想剩下的查询还可以,但主要问题是客户机信息的连接。

iszxjhcz

iszxjhcz4#

我认为在使用gmb的答案上,您还需要加入到item列中,否则您将得到多行输出。

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

只需修改你的过滤器,你应该被排序。

相关问题