现有费用预算表(各部门按月做部门费用预算)结构如下:
部门编码  部门名称  科目编码   科目名称  1月预算  2月预算..12月预算
01        电脑部    01         薪金      100      200      1200
01        电脑部    02         加班费    200      300      300
02        财务部    01         薪金      100      300      1100 表中没有日期字段,怎么能转换出一个日期提供到查询,使查询能按日期段进行呢?
如我要查询出电脑部一月的预算,条件是:部门编码:01;时间:2011-01-01到2011-01-31 结果如下:部门编码  部门名称  科目编码   科目名称  1月预算  
01        电脑部    01         薪金      100      
01        电脑部    02         加班费    200      
      

解决方案 »

  1.   

    select * from(
    select 部门编码,部门名称,科目编码,科目名称,1月预算 as 本月预算,'2011-01-01' as dt from 费用预算表
    union all
    select 部门编码,部门名称,科目编码,科目名称,2月预算,'2011-02-01' as dt from 费用预算表
    union all
    select 部门编码,部门名称,科目编码,科目名称,3月预算,'2011-03-01' as dt from 费用预算表
    union all
    ......
    select 部门编码,部门名称,科目编码,科目名称,12月预算,'2011-12-01' as dt from 费用预算表
    )t
      

  2.   

    select * from
    (
    select  部门编码,部门名称,科目编码,科目名称,1月预算,cast('2011-01-01' as datetime) as date
    union all
    select  部门编码,部门名称,科目编码,科目名称,2月预算,'2011-02-01'
    union all
    select  部门编码,部门名称,科目编码,科目名称,3月预算,'2011-03-01'
    union all
    select  部门编码,部门名称,科目编码,科目名称,4月预算,'2011-04-01'
    union all
    select  部门编码,部门名称,科目编码,科目名称,5月预算,'2011-05-01'
    union all
    select  部门编码,部门名称,科目编码,科目名称,6月预算,'2011-06-01'
    union all
    select  部门编码,部门名称,科目编码,科目名称,7月预算,'2011-07-01'
    union all
    select  部门编码,部门名称,科目编码,科目名称,8月预算,'2011-08-01'
    union all
    select  部门编码,部门名称,科目编码,科目名称,9月预算,'2011-09-01'
    union all
    select  部门编码,部门名称,科目编码,科目名称,10月预算,'2011-10-01'
    union all
    select  部门编码,部门名称,科目编码,科目名称,11月预算,'2011-11-01'
    union all
    select  部门编码,部门名称,科目编码,科目名称,12月预算,'2011-12-01'
    ) tb
    where date between '2011-01-01' and '2011-01-11'
      

  3.   

    select * from
    (
    select  部门编码,部门名称,科目编码,科目名称,1月预算,cast('2011-01-01' as datetime) as date from 费用预算表
    union all
    select  部门编码,部门名称,科目编码,科目名称,2月预算,'2011-02-01' from 费用预算表
    union all
    select  部门编码,部门名称,科目编码,科目名称,3月预算,'2011-03-01' from 费用预算表
    union all
    select  部门编码,部门名称,科目编码,科目名称,4月预算,'2011-04-01' from 费用预算表
    union all
    select  部门编码,部门名称,科目编码,科目名称,5月预算,'2011-05-01' from 费用预算表
    union all
    select  部门编码,部门名称,科目编码,科目名称,6月预算,'2011-06-01' from 费用预算表
    union all
    select  部门编码,部门名称,科目编码,科目名称,7月预算,'2011-07-01' from 费用预算表
    union all
    select  部门编码,部门名称,科目编码,科目名称,8月预算,'2011-08-01' from 费用预算表
    union all
    select  部门编码,部门名称,科目编码,科目名称,9月预算,'2011-09-01' from 费用预算表
    union all
    select  部门编码,部门名称,科目编码,科目名称,10月预算,'2011-10-01' from 费用预算表
    union all
    select  部门编码,部门名称,科目编码,科目名称,11月预算,'2011-11-01' from 费用预算表
    union all
    select  部门编码,部门名称,科目编码,科目名称,12月预算,'2011-12-01' from 费用预算表
    ) tb
    where date between '2011-01-01' and '2011-01-11'
      

  4.   

    只有在查询的时候自己添加一列时间字段, 不过根据你的描述,个人觉得没有必要添加时间字段啊! 表中数据已经分的很清楚了啊! 你要查询那一列,直接select 不行嘛?
      

  5.   

    http://topic.csdn.net/u/20110704/11/0005f420-5d9e-4636-b848-1a9275638c5b.html?66926楼主你自己发的帖子怎么又重发了
    declare @start datetime
    declare @end datetime
    set @start = '2011-01-01'
    set @end = '2011-03-01';with cte as
    (
        select code,[name],[1月] as qty,1 as mon from tb
        union all
        select code,[name],[2月] as qty,2 as mon from tb
        union all
        select code,[name],[3月] as qty,3 as mon from tb
        ....
    )select *
    from cte
    where mon in (
        select distinct month(convert(datetime,dateadd(mm,number,@start)))
        from master..spt_values
        where [type] = 'p' and number between 0 and datediff(mm,@start,@end)
    )with cte as
    (
      select 部门,部门编码,科室,科室名称,[1月] as qty,1 as mon from 表
      union all
      select 部门,部门编码,科室,科室名称,[2月] as qty,2 as mon from 表
      union all
      select 部门,部门编码,科室,科室名称,[3月] as qty,3 as mon from 表
      ...
    )