需要将一个表的数据与另一个表匹配。

huangapple go评论63阅读模式
英文:

need to match data of one table with another

问题

需要将一个表的数据与另一个表匹配。第一张表只有一行,第二张表根据日历有多行。

表格详情:-

第一张表:-

A         	B		     C	        D 		  E               G		

600	      MT_000		6765	29-04-23	30-04-23	WEEK
 	      MT		    6758	01-05-23	31-05-23	MONTH
601	      MT_0001		6771	01-06-23	30-06-23	WEEK

第二张表:-

A       B            C         D                    
601	    MT			6758	07-05-23			
601		MT			6758	14-05-23				   
601		MT			6758	21-05-23				   
601		MT			6758	28-05-23			   
601		MT			6758	28-05-23				
601		MT			6758	04-06-23			

希望的数据如下:

A    B        C       D          E 
601  MT    6758    07-05-23   MONTH 
601  MT    6758    14-05-23   MONTH 
601  MT    6758    21-05-23   MONTH
601  MT    6758    28-05-23   MONTH
601  MT    6758    28-05-23   MONTH
601  MT    6758    04-06-23   WEEK

A列的依赖关系应该存在,值也可以为空。

我尝试过连接两个表,但数据不正确。每一行都在与其他行进行比较,导致有18行。

请帮我解决这个问题。如果需要更清晰的信息,您可以提问。

英文:

Need to match data of one table with another. As Table one as only one row and table second has many row as per the calendar.

Table details:-

First table:-

A         	B		     C	        D 		  E               G		

600	      MT_000		6765	29-04-23	30-04-23	WEEK
 	      MT		    6758	01-05-23	31-05-23	MONTH
601	      MT_0001		6771	01-06-23	30-06-23	WEEK

Second table:-

A       B            C         D                    
601	    MT			6758	07-05-23			
601		MT			6758	14-05-23				   
601		MT			6758	21-05-23				   
601		MT			6758	28-05-23			   
601		MT			6758	28-05-23				
601		MT			6758	04-06-23			

Want data like this :-

A    B        C       D          E 
601  MT    6758    07-05-23   MONTH 
601  MT    6758    14-05-23   MONTH 
601  MT    6758    21-05-23   MONTH
601  MT    6758    28-05-23   MONTH
601  MT    6758    28-05-23   MONTH
601  MT    6758    04-06-23   WEEK

Dependency on Column A should be there value can null also.

I tried join both the table but data is not correct. Giving 18 row as each row is comparing with each other.

Please help me with this issue.
For more clearity you may ask q.

答案1

得分: 1

以下是翻译好的部分:

看起来你正在查找table2中的行是否为WEEKMONTH。如果这是你想要的,请使用如下所示的inner join

SELECT t2.*, t1.G
from table2 t2
inner join table1 t1 on t2.D between t1.D and t1.E
order by t2.A, t1.D

结果:

A	B	C	    D	        G
601	MT	6758	07-MAY-23	MONTH
601	MT	6758	14-MAY-23	MONTH
601	MT	6758	21-MAY-23	MONTH
601	MT	6758	28-MAY-23	MONTH
601	MT	6758	28-MAY-23	MONTH
601	MT	6758	04-JUN-23	WEEK

演示链接

英文:

Its appear that you are looking for whether the rows in table2 are WEEK or MONTH. If that's what you want, use a inner join as shown below:

SELECT t2.*, t1.G
from table2 t2
inner join table1 t1 on t2.D between t1.D and t1.E
order by t2.A, t1.D

Result :

A	B	C	    D	        G
601	MT	6758	07-MAY-23	MONTH
601	MT	6758	14-MAY-23	MONTH
601	MT	6758	21-MAY-23	MONTH
601	MT	6758	28-MAY-23	MONTH
601	MT	6758	28-MAY-23	MONTH
601	MT	6758	04-JUN-23	WEEK

Demo here

答案2

得分: 1

将两个表根据日期范围(可能还包括b列的起始部分)进行连接:

SELECT t2.*, t1.g
FROM   table2 t2
       INNER JOIN table1 t1
       ON (t1.b LIKE t2.b || '%' AND t2.d BETWEEN t1.d AND t1.e)
ORDER BY t2.b, t2.d

对于示例数据:

CREATE TABLE table1 (A, B, C, D, E, G) AS
SELECT 600, 'MT_000',  6765, DATE '2023-04-29', DATE '2023-04-30', 'WEEK'  FROM DUAL UNION ALL
SELECT NULL, 'MT',      6758, DATE '2023-05-01', DATE '2023-05-31', 'MONTH' FROM DUAL UNION ALL
SELECT 601, 'MT_0001', 6771, DATE '2023-06-01', DATE '2023-06-30', 'WEEK'  FROM DUAL;

CREATE TABLE table2 (A, B, C, D) AS
SELECT 601, 'MT', 6758, DATE '2023-05-07' FROM DUAL UNION ALL
SELECT 601, 'MT', 6758, DATE '2023-05-14' FROM DUAL UNION ALL
SELECT 601, 'MT', 6758, DATE '2023-05-21' FROM DUAL UNION ALL
SELECT 601, 'MT', 6758, DATE '2023-05-28' FROM DUAL UNION ALL
SELECT 601, 'MT', 6758, DATE '2023-05-28' FROM DUAL UNION ALL
SELECT 601, 'MT', 6758, DATE '2023-06-04' FROM DUAL;

输出:

A B C D G
601 MT 6758 2023-05-07 00:00:00 MONTH
601 MT 6758 2023-05-14 00:00:00 MONTH
601 MT 6758 2023-05-21 00:00:00 MONTH
601 MT 6758 2023-05-28 00:00:00 MONTH
601 MT 6758 2023-05-28 00:00:00 MONTH
601 MT 6758 2023-06-04 00:00:00 WEEK

fiddle

英文:

JOIN the two tables on the date range (and, maybe, the start of the b columns):

SELECT t2.*, t1.g
FROM   table2 t2
       INNER JOIN table1 t1
       ON (t1.b LIKE t2.b || '%' AND t2.d BETWEEN t1.d AND t1.e)
ORDER BY t2.b, t2.d

Which, for the sample data:

CREATE TABLE table1 (A, B, C, D, E, G) AS
SELECT 600,  'MT_000',  6765, DATE '2023-04-29', DATE '2023-04-30', 'WEEK'  FROM DUAL UNION ALL
SELECT NULL, 'MT',      6758, DATE '2023-05-01', DATE '2023-05-31', 'MONTH' FROM DUAL UNION ALL
SELECT 601,  'MT_0001', 6771, DATE '2023-06-01', DATE '2023-06-30', 'WEEK'  FROM DUAL;

CREATE TABLE table2 (A, B, C, D) AS
SELECT 601, 'MT', 6758, DATE '2023-05-07' FROM DUAL UNION ALL
SELECT 601, 'MT', 6758, DATE '2023-05-14' FROM DUAL UNION ALL
SELECT 601, 'MT', 6758, DATE '2023-05-21' FROM DUAL UNION ALL
SELECT 601, 'MT', 6758, DATE '2023-05-28' FROM DUAL UNION ALL
SELECT 601, 'MT', 6758, DATE '2023-05-28' FROM DUAL UNION ALL
SELECT 601, 'MT', 6758, DATE '2023-06-04' FROM DUAL;

Outputs:

A B C D G
601 MT 6758 2023-05-07 00:00:00 MONTH
601 MT 6758 2023-05-14 00:00:00 MONTH
601 MT 6758 2023-05-21 00:00:00 MONTH
601 MT 6758 2023-05-28 00:00:00 MONTH
601 MT 6758 2023-05-28 00:00:00 MONTH
601 MT 6758 2023-06-04 00:00:00 WEEK

fiddle

huangapple
  • 本文由 发表于 2023年5月11日 16:09:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定