SELECT
"Trade Details 2".Portfolio,
"Trade Details 2".CONTRACT_ID,
"Trade Details 2".START_DATE,
"Trade Details 2".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 > "20180621")
ORDER BY
CONTRACT_ID asc
我已经能够确定每个Maturity_Date都在2018年6月21日之后,但是我在START_DATE时遇到了麻烦。我的目标是在START_DATE列中将2018年6月22日之前的每个日期替换为2018年6月22日。
我试图将“交易详细信息2”替换为START_DATE <2018-06-22,并以2018-06-22代替),但无济于事。有什么办法可以在SQL中做到这一点?
答案 0 :(得分:1)
假设使用SQLite,您可以这样编写查询:
SELECT td2.Portfolio, td2.CONTRACT_ID,
MAX(td2.START_DATE, '2018-06-22') as START_DATE
td2.MATURITY_DATE, td2.NOTIONAL1, td2.CONTRACT_NPV,
td.TERM
FROM "Trade Details 2" td2 JOIN
"Trade Details" td
ON td2.CONTRACT_ID = td.FCC_ID AND
td2.NOTIONAL1 > 0.0 AND
td2.MATURITY_DATE > '20180621'
ORDER BY CONTRACT_ID asc;
注意:
NOTIONAL1
是一个数字,因此与数字而不是字符串进行比较。JOIN
是很好的语法。应该始终有一个ON
子句。MIN()
和MAX()
采用多个参数。具有多个参数的它们不是聚合函数,而是在其他数据库中的行为像LEAST()
和GREATEST()
。答案 1 :(得分:0)
请参阅start_date_bounded。由于您没有标记dbms,因此可能需要调整语法(例如DATE'2018-06-22'。
SELECT "Trade Details 2".Portfolio, "Trade Details 2".CONTRACT_ID,
CASE WHEN "Trade Details 2".START_DATE < 2018-06-22 THEN 2018-06-22
ELSE "Trade Details 2".START_DATE
END AS start_date_bounded, "Trade Details 2".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 > "20180621")
ORDER BY CONTRACT_ID asc