SQL:将表A与表B[(A.Col1=B.Col1&&A.Col2=B.Col2)和max(transacrionTime)]
我需要帮助,为下面的场景构建SQL查询或CTE 问题:我需要将主表A与明细表B连接起来,以找出表B中具有相同itemType、具有maxTransactionTime和&TransactionTimeSQL:将表A与表B[(A.Col1=B.Col1&&A.Col2=B.Col2)和max(transacrionTime)],sql,kinetica,Sql,Kinetica,我需要帮助,为下面的场景构建SQL查询或CTE 问题:我需要将主表A与明细表B连接起来,以找出表B中具有相同itemType、具有maxTransactionTime和&TransactionTime
id itemType country EntryTime
1. Item1 IND 12:01:20:291
2. Item2 USA 14:11:22:299
3. Item4 LON 18:01:17:112
4. Item1 SIN 20:05:30:020
5. Item3 HKG 22:02:23:442
股票价格表
id itemType country TransactionTime Price
1. Item1 IND 12:01:20:291 10.12
2. Item2 USA 14:11:22:299 50.12
3. Item4 LON 18:01:17:112 02.12
4. Item1 SIN 20:05:30:020 10.67
5. Item3 HKG 22:02:23:442 11.22
6. Item1 IND 12:01:20:291 10.14
7. Item2 USA 14:11:22:299 50.11
8. Item4 LON 18:01:17:112 02.10
9. Item1 SIN 20:05:30:020 10.90
10. Item3 HKG 22:02:23:442 11.37
11. Item1 IND 12:01:20:291 10.10
12. Item2 USA 14:11:22:299 50.01
13. Item4 LON 18:01:17:112 02.11
14. Item1 SIN 20:05:30:020 10.89
15. Item3 HKG 22:02:23:442 11.90
请帮助提供建议,如果需要更多详细信息,请在评论中告知我
试液
Select o.id, o.itemType, o.country, o.EntryTime, o.sp.price
from OrderTable o join
StockPrice sp
ON o.country = sp.country and o.itemType = sp.itemType and
o.EntryTime = (select top 1 TransactionTime from StockPrice spIN where o.country = spIN.country and o.itemType = spIN.itemType and
and spIN.spIN.TransactionTime < o.EntryTime order by spIN.TransactionTime)
不知何故,结果集的行数多于预期的行数
与上述查询有关的问题
从上面的查询中,我只得到表A中第一行的结果。
若表B中有多行的确切时间为max TransactionTime,则返回的结果将对应于表B中的行数。
看看这是否有效:
select o.id, o.itemType, o.country, o.EntryTime, sp.price
from OrderTable o cross apply (
select top 1 * from StockPrice spIN
where o.country = spIN.country and o.itemType = spIN.itemType
and spIN.TransactionTime <= o.EntryTime
order by TransactionTime desc
) sp
order by id;
我需要帮助不是问题。你在问什么?你所做的尝试有什么不起作用?这些尝试是什么?你为什么贴上23号标签?完全不同的RDBMS?我从来没有听说过Kinetica,所以我猜这就是你真正想要的标签。不要在问题上添加不适当的标签。@GordonLinoff,Kinetica是数据库,请参见下面的@Larnu,谢谢突出显示,我会更正它。这可能是因为您没有在子查询中包含order by:从StockPrice spIN中选择top 1 TransactionTime,其中o.country=spIN.country和o.itemType=spIN.itemType,并且spIN.TransactionTime