id 部门 日期        领用金额 客人使用
1 餐厅部    2009-08-08 50 50
2 餐厅部    2009-08-10 20 0
3        外签      2009-08-11 0 5000
4 外签      2009-08-11 0 500
5 外签      2009-08-11 0 600要生成的结果为: 部门 日期        领用金额 客人使用   余额         外签      2009-08-11 500 5000      -4500
外签      2009-08-11 0 500       -5000
外签      2009-08-11 0 600       -5600

解决方案 »

  1.   

    在group by 列
    --后面加
    having bm='外签'
      

  2.   

    select * from 部门='外签'
      

  3.   

    如果对应上了 直接这样
    select * from tb where 部门='外签'
      

  4.   

    select 
       * ,
       余额=领用金额-客人使用
    from tb where 部门='外签'
      

  5.   

    SELECT * ,余额=客人使用-领用金 from tb where 部门='外签'
      

  6.   


    select *,领取金额-客人使用 as 金额 from table where 部门='外迁' 
      

  7.   


    Create table tb1(id int,部门 nvarchar(20),日期 datetime,领用金额 int,客人使用 int)
    insert into tb1
    select 1, '餐厅部',    '2009-08-08' ,50 ,50 union all
    select 2, '餐厅部',    '2009-08-10', 20, 0  union all
    select 3, '外签'   ,   '2009-08-11', 0, 5000  union all
    select 4, '外签'     , '2009-08-11', 0, 500  union all
    select 5, '外签'   ,   '2009-08-11' ,0 ,600 
    select *,(select Sum(-客人使用) from tb1 where tb1.ID<a.ID+1 and tb1.部门=a.部门) '余额' from tb1 as a
    where 部门='外签'---------结果------------ID      部门     日期                       领用金额  客人使用  余额
    3 外签 2009-08-11 00:00:00.000 0 5000 -5000
    4 外签 2009-08-11 00:00:00.000 0 500 -5500
    5 外签 2009-08-11 00:00:00.000 0 600 -6100
      

  8.   

    if OBJECT_ID('tttt') is not null drop table tttt
    create table tttt(id int,bm varchar(20),rq varchar(10),nyje decimal(14,2),khsy decimal(14,2))
    insert tttt
    select 1, '餐厅部',    '2009-08-08', 50, 50 
    union all select 2, '餐厅部',    '2009-08-10', 20, 0 
    union all select 3, '外签',      '2009-08-11', 0, 5000 
    union all select 4, '外签',      '2009-08-11', 0, 500 
    union all select 5, '外签',      '2009-08-11', 0, 600 select a.bm,a.rq,nyje,khsy,khsy-nyje as 余额
      from tttt a where bm='外签'
    bm rq nyje khsy 余额
    外签 2009-08-11 0.00 5000.00 5000.00
    外签 2009-08-11 0.00 500.00 500.00
    外签 2009-08-11 0.00 600.00 600.00
      

  9.   

    SELECT *,(SELECT 客人使用-领用金 FROM TB WHERE T.部门=部门 AND ID<=T.ID)AS 余额 FROM TB T?????可能是这个意思
      

  10.   


    Create table tb1(id int,部门 nvarchar(20),日期 datetime,领用金额 int,客人使用 int)
    insert into tb1
    select 1, '餐厅部',    '2009-08-08' ,50 ,50 union all
    select 2, '餐厅部',    '2009-08-10', 20, 0  union all
    select 3, '外签'   ,   '2009-08-11', 0, 5000  union all
    select 4, '外签'     , '2009-08-11', 0, 500  union all
    select 5, '外签'   ,   '2009-08-11' ,0 ,600 
    select *,(select Sum(领用金额-客人使用) from tb1 where tb1.ID<a.ID+1 and tb1.部门=a.部门) '余额' from tb1 as a
    where 部门='外签'---------结果------------ID      部门     日期                       领用金额  客人使用  余额
    3    外签    2009-08-11 00:00:00.000    0    5000    -5000
    4    外签    2009-08-11 00:00:00.000    0    500    -5500
    5    外签    2009-08-11 00:00:00.000    0    600    -6100
      

  11.   

    -- =========================================
    -- -----------t_mac 小编-------------------
       --------------------希望有天成为大虾---- 
    -- =========================================IF OBJECT_ID('tb') IS NOT NULL
    DROP TABLE tb
    GO
    CREATE TABLE tb(id int,部门 varchar(10), 日期 varchar(10) ,领用金额 int,客人使用 int)
    go
    insert tb SELECT 
    1, '餐厅部',    '2009-08-08', 50 ,50 UNION ALL SELECT 
    2, '餐厅部',    '2009-08-10', 20, 0 UNION ALL SELECT 
    3, '外签',      '2009-08-11', 500, 5000 UNION ALL SELECT 
    4, '外签',      '2009-08-11', 0, 500 UNION ALL SELECT 
    5, '外签',      '2009-08-11', 0, 600 
    go
    select 
    *,
    余额=(select Sum(-客人使用+领用金额) from tb where tb.ID<t.ID+1 and tb.部门=T.部门)
    from tb t
    where 部门='外签'
    go
    /*
    (5 行受影响)
    id          部门         日期         领用金额        客人使用        余额
    ----------- ---------- ---------- ----------- ----------- -----------
    3           外签         2009-08-11 500         5000        -4500
    4           外签         2009-08-11 0           500         -5000
    5           外签         2009-08-11 0           600         -5600*/
      

  12.   

    if object_id('t') is not null
    drop table t
    go
    create table t(id int identity(1,1), 部门 varchar(10), 日期 datetime , 领用金额 int, 客人使用 int)
    go
    insert into t select '餐厅部',    '2009-08-08', 50, 50 
    union all select  '餐厅部',    '2009-08-10', 20, 0 
    union all select  '外签',      '2009-08-11', 0, 5000 
    union all select  '外签',      '2009-08-11' ,0, 500 
    union all select  '外签',      '2009-08-11', 0, 600 
    select 部门, 日期, 领用金额, 客人使用,  余额= (领用金额-客人使用) 
    from t where 部门='外签'
    /*
    部门         日期                      领用金额        客人使用        余额
    ---------- ----------------------- ----------- ----------- -----------
    外签         2009-08-11 00:00:00.000 0           5000        -5000
    外签         2009-08-11 00:00:00.000 0           500         -500
    外签         2009-08-11 00:00:00.000 0           600         -600(3 行受影响)*/
      

  13.   


    SELECT ID=IDENTITY(INT,1,1),* INTO #TT FROM TB WHERE 部门 ='外签'
    SELECT *,
    (SELECT SUM(客人使用-领用金) FROM #TT WHERE T.部门=部门 AND ID<=T.ID)AS 余额 FROM #TT T
      

  14.   

    declare @T table(id int,部门 varchar(50),日期 datetime,领用金额 int, 客人使用 int)insert into @t 
    select 1, '餐厅部' ,   '2009-08-08', 50 ,50 union all
    select 2, '餐厅部' ,   '2009-08-10', 20 ,0 union all
    select 3, '外签' ,   '2009-08-11', 500 ,5000 union all
    select 4, '外签' ,   '2009-08-11', 0 ,500 union all
    select 5, '外签' ,   '2009-08-11', 0 ,600  select 部门,日期,领用金额,客人使用, dif=(select sum(领用金额-客人使用) from @T where id<=t.id and 部门='外签') 
    from @t t where 部门='外签'/*(5 行受影响)
    部门                                                 日期                      领用金额        客人使用        dif
    -------------------------------------------------- ----------------------- ----------- ----------- -----------
    外签                                                 2009-08-11 00:00:00.000 500         5000        -4500
    外签                                                 2009-08-11 00:00:00.000 0           500         -5000
    外签                                                 2009-08-11 00:00:00.000 0           600         -5600(3 行受影响)*/
      

  15.   


    IF OBJECT_ID('ta') IS NOT NULL
    DROP TABLE ta
    GO
    IF OBJECT_ID('tb') IS NOT NULL
    DROP TABLE tb
    GO
    CREATE TABLE tb( 部门 varchar(14), 日期 datetime,领用金额 int, 客人使用 int,余额 int)
    go
    CREATE TABLE ta( id int,部门 varchar(14), 日期 datetime,领用金额 int, 客人使用 int)
    go
    insert ta SELECT 
    1, '餐厅部',    '2009-08-08', 50 , 50 union all select
    2, '餐厅部',    '2009-08-10', 20,  0 union all select
    3, '外签'   ,   '2009-08-11', 500, 5000 union all select
    4, '外签'    ,  '2009-08-11', 0,   500 union all select
    5, '外签'     , '2009-08-11', 0,   600declare @n int
    set @n=0insert tb select 部门,日期,领用金额, 客人使用,0 from ta where 部门='外签'
    update tb set @n=领用金额-客人使用+@n,余额=@nselect * from tb
    部门             日期                      领用金额        客人使用        余额
    -------------- ----------------------- ----------- ----------- -----------
    外签             2009-08-11 00:00:00.000 500         5000        -4500
    外签             2009-08-11 00:00:00.000 0           500         -5000
    外签             2009-08-11 00:00:00.000 0           600         -5600(3 行受影响)drop table ta 
    drop table tb
      

  16.   

    select * from tb where 部门='外签'
      

  17.   

    没看清楚需求  再写个  select 部门,日期,领用金额,客人使用,(领用金额-使用金额) as 余额 from tb where 部门='外签'