多列上的SQL IIF

时间:2014-08-05 13:34:41

标签: sql ms-access iif

新周新问题..

我需要找出本月度假的人。 我有这张桌子:

worker - id,name,forename

假期 - idworker,from,to,idstatus

现在我需要一个查询,它给我这样的东西:

n | fn | idstat | 1 | 2 | 3 | 4 | 5 | 6 .. max | muster| 1 | t | t | t | f | f | f .. max | muster| 2 | f | f | f | f | t | t .. peter | pan | 1 | f | t | f | f | f | t ..

这些数字是这个月的天数 t / f是真假,所以如果thex正在休假......

现在我有了这段代码:

SELECT worker.forename + " " + worker.name AS Name, holiday.IdStatus,
IIF(#08/01/2014# BETWEEN holiday.from AND holiday.to, 'True', 'FALSE') AS 1,
IIF(#08/02/2014# BETWEEN holiday.from AND holiday.to, 'True', 'FALSE') AS 2,
IIF(#08/03/2014# BETWEEN holiday.from AND holiday.to, 'True', 'FALSE') AS 3,
IIF(#08/04/2014# BETWEEN holiday.from AND holiday.to, 'True', 'FALSE') AS 4,
IIF(#08/05/2014# BETWEEN holiday.from AND holiday.to, 'True', 'FALSE') AS 5,
IIF(#08/06/2014# BETWEEN holiday.from AND holiday.to, 'True', 'FALSE') AS 6,
FROM worker LEFT JOIN holiday ON worker.Id = holiday.idworker

但这只是在假期中使用了一列,因此一个人有多列......

有人有个主意吗? > _>'

编辑:

示例数据: 工人: Id - Name - Forname 1 - Pan - Peter 2 - Max - Muster

节日: IdWorker - from - to - ids - comment 1 - 8/1/2014 - 8/2/2014 - 1 - has holiday from 1.8.2014 to 2.8.2014 1 - 8/5/2014 - 8/6/2014 - 2 - wants holiday from 5.8.2014 to 6.8.2014 1 - 8/4/2014 - 8/4/2014 - 1 - has holiday on 4.08.2014

IDSTATUS(IDS): 1是假期 2是想度假

现在我的结果应该是这样的:

n | fn | idstat | 1 | 2 | 3 | 4 | 5 | 6 .. max | muster| 1 | f | f | f | f | f | f .. peter | pan | 2 | f | f | f | f | t | t .. peter | pan | 1 | t | t | f | t | f | f ..

1 个答案:

答案 0 :(得分:0)

你可以使用交叉表,但是如果你想在其他地方使用结果,它真的很笨拙。如果你迫切需要这种解决方案,你可以选择所有日子,加入假期表,为每个日期(IIf(date >= startdate and date <= end date))输入IIf,然后转动它。