简体   繁体   English

SQL 跨表查询

[英]SQL Query across tables

I am trying to extract data from a system comprised of various tables where I am using one table as a primary list and then trying to select multiple events from another table which all relate to the single line in the first table.我正在尝试从由多个表组成的系统中提取数据,其中我使用一个表作为主列表,然后尝试从另一个表中选择多个事件,这些事件都与第一个表中的单行相关。

The Orders table and Processing tables are constructed;构建了 Orders 表和 Processing 表;

Orders
Ref         | Fruit     | Shapes        | Colours   | OrderDate
-----------------------------------------------------------------
22222       | Apple     | Round         | Red       | 2020/02/23
33333       | Pear      | Round         | Green     | 2020/02/23
44444       | Banana    | Long          | Yellow    | 2020/02/23

Processing
Fruit       | ProcessID     | ProcessDateTime
-----------------------------------------+----------------------------
Apple       | Receipt       | 2020/02/23 09:34:03
Apple       | Repack        | 2020/02/23 10:15:23
Apple       | Dispatch      | 2020/02/23 10:32:11
Pear        | Receipt       | 2020/03/23 08:34:55
Pear        | Repack        | 2020/02/23 09:08:43
Pear        | Dispatch      | 2020/02/23 10:43:21
Banana      | Receipt       | 2020/02/23 07:23:32
Banana      | Repack        | 2020/02/23 09:33:28
Banana      | Dispatch      | 2020/02/23 11:38:01

And the data extracted should be similar to the format;并且提取出来的数据应该和格式差不多;

Ref     | Fruit | DateOrdered   | DateReceipt           | DateRepack            | DateDispatch
-----------------------------------------------------------------------------------------------------
22222   | Apple | 2020/02/23    | 2020/02/23 09:34:03   | 2020/02/23 10:15:23   | 2020/02/23 10:32:11
33333   | Pear  | 2020/02/23    | 2020/02/23 08:34:55   | 2020/02/23 09:08:43   | 2020/02/23 10:43:21
44444   | Banana| 2020/02/23    | 2020/02/23 07:23:32   | 2020/02/23 09:33:28   | 2020/02/23 11:38:01

The code I have created will begin similar to;我创建的代码将类似于;

SELECT
Orders.Ref,
Orders.Fruit,
Orders.Shapes,
Orders.Colours,
.Now I want to select each ProcessID for each Ref and ProcessDateTime date for each of the processes in Processing. 
There are 3 processes in the example here, but it will not necesarily always
be 3, it will vary in number of processes.

FROM Orders
LEFT INNER JOIN Processing ON Processing.Fruit = Orders.Fruit

WHERE
Orders.OrderDate BETWEEN date1 AND date2;  -- These dates will be identified when running...

I have looked online around conditional joins, but am struggling a bit here and any help would be most welcome, thanks!我在网上查看了条件连接,但在这里有点挣扎,非常欢迎任何帮助,谢谢!

Thank you Gordon for your response to this issue, it is very nearly working correctly for me except for the fact that each of the entries in table processing is being placed on a separate line, rather than combining on the single line as I had hoped.感谢 Gordon 对这个问题的回答,除了表处理中的每个条目都放在单独的行上,而不是像我希望的那样组合在一行上之外,它对我来说几乎可以正常工作。 Using your solution, I am getting;使用您的解决方案,我得到了;

Ref     | Fruit | DateOrdered   | DateReceipt           | DateRepack            | DateDispatch
-----------------------------------------------------------------------------------------------------
22222   | Apple | 2020/02/23    | 2020/02/23 09:34:03   |                       |                   
22222   | Apple | 2020/02/23    |                       | 2020/02/23 10:15:23   |                    
22222   | Apple | 2020/02/23    |                       |                       | 2020/02/23 10:32:11
33333   | Pear  | 2020/02/23    | 2020/02/23 08:34:55   |                       | 
33333   | Pear  | 2020/02/23    |                       | 2020/02/23 09:08:43   |
33333   | Pear  | 2020/02/23    |                       |                       | 2020/02/23 10:43:21
44444   | Banana| 2020/02/23    | 2020/02/23 07:23:32   |                       | 
44444   | Banana| 2020/02/23    |                       | 2020/02/23 09:33:28   | 
44444   | Banana| 2020/02/23    |                       |                       | 2020/02/23 11:38:01

Rather than the result which i wanted to achieve below;而不是我想在下面实现的结果;

Ref     | Fruit | DateOrdered   | DateReceipt           | DateRepack            | DateDispatch
-----------------------------------------------------------------------------------------------------
22222   | Apple | 2020/02/23    | 2020/02/23 09:34:03   | 2020/02/23 10:15:23   | 2020/02/23 10:32:11
33333   | Pear  | 2020/02/23    | 2020/02/23 08:34:55   | 2020/02/23 09:08:43   | 2020/02/23 10:43:21
44444   | Banana| 2020/02/23    | 2020/02/23 07:23:32   | 2020/02/23 09:33:28   | 2020/02/23 11:38:01

You can use conditional aggregation:您可以使用条件聚合:

SELECT o.Ref, o.Fruit, 
       MAX(CASE WHEN p.ProcessID = 'RECEIPT' THEN ProcessDateTime END) as receipt,
       MAX(CASE WHEN p.ProcessID = 'REPACK' THEN ProcessDateTime END) as repack,
       MAX(CASE WHEN p.ProcessID = 'DISPATCH' THEN ProcessDateTime END) as dispatch
FROM Orders o LEFT JOIN
     Processing p
     ON p.Fruit = o.Fruit
WHERE o.OrderDate BETWEEN date1 AND date2
GROUP BY o.Ref, o.Fruit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM