简体   繁体   English

在DB for SQL中以日期格式联接3个表

[英]Joining 3 Tables in DB for SQL with Date Format

I am attempting to join 3 tables together in SQL, to no avail. 我试图在SQL中将3个表连接在一起,但无济于事。 My code is below: 我的代码如下:

SELECT "Trade Details 2".Portfolio, 
"Trade Details 2".CONTRACT_ID, 
DATE("Trade Details 2".START_DATE) as START_DATE, 
DATE(substr("Trade Details 2".MATURITY_DATE, 0, 5) || '-' || substr("Trade 
Details 2".MATURITY_DATE, 5,2) || '-' ||    substr("Trade Details 
2".MATURITY_DATE, 7, 9)) as MATURITY_DATE, 
"Trade Details 2".NOTIONAL1,
"Trade Details 2".CONTRACT_NPV,
"Trade Details".TERM
"FX SPOT".Mid
JOIN "Trade Details" ON "Trade Details 2".CONTRACT_ID="Trade Details".FCC_ID
INNER JOIN "FX SPOT" ON "FX SPOT".MID
WHERE "Trade Details 2".CONTRACT_ID="Trade Details".FCC_ID and
("Trade Details 2".NOTIONAL1 > "0.0") and
("Trade Details 2".MATURITY_DATE > DATE(substr('20180622', 0, 5) || '-' || 
substr('20180622', 5,2) || '-' ||    substr('20180622', 7, 9)) )
ORDER BY CONTRACT_ID asc

If I just try to adjoin two tables, taking out all the things relating to "FX Spot".Mid, I get this 如果我只是尝试将两个表连接起来,则将与“ FX Spot”有关的所有内容都删除掉了。

SELECT "Trade Details 2".Portfolio, 
"Trade Details 2".CONTRACT_ID, 
DATE("Trade Details 2".START_DATE) as START_DATE, 
DATE(substr("Trade Details 2".MATURITY_DATE, 0, 5) || '-' || substr("Trade Details 2".MATURITY_DATE, 5,2) || '-' ||    substr("Trade Details 2".MATURITY_DATE, 7, 9)) as MATURITY_DATE, 
"Trade Details 2".NOTIONAL1,
"Trade Details 2".CONTRACT_NPV,
"Trade Details".TERM
FROM "Trade Details 2" 
JOIN "Trade Details" 
WHERE "Trade Details 2".CONTRACT_ID="Trade Details".FCC_ID and
("Trade Details 2".NOTIONAL1 > "0.0") and
("Trade Details 2".MATURITY_DATE > DATE(substr('20180622', 0, 5) || '-' || substr('20180622', 5,2) || '-' ||    substr('20180622', 7, 9)) )
ORDER BY CONTRACT_ID asc

which works. 哪个有效。

However, as soon as I attempt to add the third column, I get a syntax error near "." 但是,当我尝试添加第三列时,在“”附近出现语法错误。 I followed how to join 3 tables to a tee, so I don't see what I'm doing wrong here. 我遵循了如何将3张桌子连接到T恤的方法,因此在这里我看不出自己在做什么。

Since you only want one value from FX SPOT, just append that value to every row in your SELECT, probably would be much more efficient yet to just set the value from your FX SPOT select to a variable and append the variable to your table join SELECT, Since I just found out that you can't declare a variable like below in SQLLite just do the Subquery inside your Table Join Select statment: 由于您只需要FX SPOT中的一个值,因此只需将该值附加到SELECT中的每一行,可能会更有效,而只是将FX SPOT select中的值设置为一个变量,然后将该变量附加到表联接SELECT中即可。 ,由于我刚刚发现您无法像下面在SQLLite中那样声明变量,因此只需在Table Join Select语句中执行子查询:

--Can't do this in SQLLite
--DECLARE @mId INT = (SELECT TOP 1 "FX SPOT".Mid FROM "FX SPOT");

SELECT "Trade Details 2".Portfolio, 
"Trade Details 2".CONTRACT_ID, 
DATE("Trade Details 2".START_DATE) as START_DATE, 
DATE(substr("Trade Details 2".MATURITY_DATE, 0, 5) || '-' || substr("Trade 
Details 2".MATURITY_DATE, 5,2) || '-' ||    substr("Trade Details 
2".MATURITY_DATE, 7, 9)) as MATURITY_DATE, 
"Trade Details 2".NOTIONAL1,
"Trade Details 2".CONTRACT_NPV,
"Trade Details".TERM,
--"FX SPOT".Mid REPLACE THIS WITH THE NEXT LINE
--@mId AS MID
(SELECT TOP 1 "FX SPOT".Mid FROM "FX SPOT") AS MID  --Reverted back to this
JOIN "Trade Details" ON "Trade Details 2".CONTRACT_ID="Trade Details".FCC_ID
--INNER JOIN "FX SPOT" ON "FX SPOT".MID DONT NEED THIS Since you have no relationship between this value and other tables
WHERE "Trade Details 2".CONTRACT_ID="Trade Details".FCC_ID and
("Trade Details 2".NOTIONAL1 > "0.0") and
("Trade Details 2".MATURITY_DATE > DATE(substr('20180622', 0, 5) || '-' || 
substr('20180622', 5,2) || '-' ||    substr('20180622', 7, 9)) )
ORDER BY CONTRACT_ID asc

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

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