我有以下架构:
CREATE TABLE table1(colA DATE, colB INT, colC TEXT);
INSERT INTO table1(colA, colB, colC) VALUES ("2016-01-01", "1", "C1");
INSERT INTO table1(colA, colB, colC) VALUES ("2016-01-02", "2", "C2");
CREATE TABLE table2(colD TEXT, colE TEXT, colF TEXT);
INSERT INTO table2(colD, colE, colF) VALUES ("2016-01-03", "3", "F1");
INSERT INTO table2(colD, colE, colF) VALUES ("2016-01-04", "4", "F2");
我要做的是将一个表附加到另一个表,然后添加另一个列(colG)。根据colA中的日期范围,colG中的每个单元格中应显示不同的消息。我还将添加另一个列,它将根据整数的值来显示消息。所以我正在寻找一种通用的方法。任何想法如何实现?非常感激。这就是我的尝试:
SELECT colA,
colB,
CASE When date <2016-01-01 "daterange A"
When date <2016-01-01<2016-01-02 "daterangeB"
When date <2016-01-02<2016-01-03 "daterangeC"
When date <2016-01-03<2016-01-04 "daterangeD"
AS colG
FROM (
SELECT colA, colB FROM table1
UNION ALL
SELECT colD, colE FROM table2
) union_tbl;
答案 0 :(得分:0)
如果你的语法正确,你的SQL应该做你想要的。像这样:
SELECT colA,
colB,
(CASE When date < '2016-01-01' then 'daterange A'
When date < '2016-01-02' then 'daterangeB'
When date < '2016-01-03' then 'daterangeC'
When date < '2016-01-04' then 'daterangeD'
END) AS colG
FROM (SELECT colA, colB FROM table1
UNION ALL
SELECT colD, colE FROM table2
) t;
注意:
case
语句按顺序评估每个子句,因此您可以简化逻辑。答案 1 :(得分:0)
您确定桌面上的数据类型吗?
在大多数数据库引擎中,如果上述数据类型正确,则union语句将失败。大多数关系数据库引擎都要求数据类型匹配union语句的成员。
SELECT colA,
colB,
(CASE When date < '2016-01-01' then 'daterange A'
When date < '2016-01-02' then 'daterangeB'
When date < '2016-01-03' then 'daterangeC'
When date < '2016-01-04' then 'daterangeD'
END) AS colG
FROM (SELECT
CAST(colA AS TEXT) AS colA
, CAST(colB AS TEXT) AS colB
FROM table1
UNION ALL
SELECT colD, colE FROM table2
) tba;
答案 2 :(得分:0)
您还应获取列日期并将日期中的字符串日期转换为
PlannerBenchmarkFactory plannerBenchmarkFactory = PlannerBenchmarkFactory.createFromXmlResource(
"prova/nurseRosteringBenchmarkConfig.xml");