SQLite:根据计算或名称选择列

时间:2014-05-14 02:09:21

标签: sqlite

我是SQL初学者。我有两个SQLite表:一个包含有关工作时间的信息,另一个包含每个工作日的特定时间范围的时间预算。我想知道如何为任何工作时间的企业选择正确的预算。

Work_Times
+---------------------+---------------------+--------+
|        begin        |         end         | Note   |
+---------------------+---------------------+--------+
| 2011-08-02 00:00:00 | 2013-08-02 00:00:00 | Free   |
| 2011-10-15 06:00:00 | 2011-10-15 09:45:00 | Tour A |
| 2011-12-05 05:30:00 | 2011-03-05 10:00:00 | Tour B |
+---------------------+---------------------+--------+

Week_Budget
+------------+------------+------+------+------+------+------+------+------+-------+
| from       | to         | Mon  | Tue  | Wed  | Thu  | Fri  | Sat  | Sun  | sum   |
+------------+------------+------+------+------+------+------+------+------+-------+
| 01.08.2011 | 30.09.2011 | 3.80 | 3.80 | 3.85 | 3.80 | 0.00 | 0.00 | 0.00 | 15.25 |
| 01.10.2011 | 27.11.2011 | 4.18 | 4.18 | 4.25 | 4.18 | 0.00 | 0.00 | 0.00 | 16.79 |
| 28.11.2011 | 31.03.2012 | 3.25 | 3.25 | 3.25 | 3.25 | 3.00 | 0.00 | 0.00 | 16.00 |
+------------+------------+------+------+------+------+------+------+------+-------+

我想我可以加入行......

WHERE Week_Buget.from => Work_Times.begin< = Week_Buget.to

...但我无法找到一种方法来选择可能是daynumber(%w)的正确列。

1 个答案:

答案 0 :(得分:0)

好的,在biziclops提示的帮助下,我找到了CASE语法,现在得到了解决方案:

SELECT wt.begin, wt.end, CASE
    WHEN strftime('%w', wt.begin)='1' THEN Mon
    WHEN strftime('%w', wt.begin)='2' THEN Tue
    WHEN strftime('%w', wt.begin)='3' THEN Wed
    WHEN strftime('%w', wt.begin)='4' THEN Thu
    WHEN strftime('%w', wt.begin)='5' THEN Fri
    WHEN strftime('%w', wt.begin)='6' THEN Sat
    WHEN strftime('%w', wt.begin)='0' THEN Sun END
    AS daybudget
FROM Week_Budget AS wb, Work_Times AS wt 
WHERE t.begin BETWEEN wb.from AND wb.to;

我还有两个问题:

  1. 有更聪明的方法吗?
  2. 完成这项工作的更好方法是什么?上面的解决方案或带有INNER JOIN的查询,如下所示:
  3. 第2版:

    SELECT wt.begin, wt.end, CASE
        WHEN strftime('%w', wt.begin)='1' THEN Mon
        WHEN strftime('%w', wt.begin)='2' THEN Tue
        WHEN strftime('%w', wt.begin)='3' THEN Wed
        WHEN strftime('%w', wt.begin)='4' THEN Thu
        WHEN strftime('%w', wt.begin)='5' THEN Fri
        WHEN strftime('%w', wt.begin)='6' THEN Sat
        WHEN strftime('%w', wt.begin)='0' THEN Sun END
        AS daybudget
    FROM Week_Budget AS wb
    INNER JOIN Work_Times AS wt 
        ON t.begin BETWEEN wb.from AND wb.to;