为什么不是小数,而是整数sum(cast( us_存款 as decimal)    ) 如何选出小数啊
select isnull( sum(cast( us_存款 as decimal)    ),0) as 总存储金额 ,isnull( sum(cast(us_消费 as decimal)) ,0) as 总消费金额 from pt_Use where 卡号=@卡号 group by 卡号pt_Use表的内容:
us_Id us_存款 us_消费 us_PublishTime 卡号
19 .56 0 2009-9-4 19:32:29 2
20 .56 .5 2009-9-4 19:32:39 2
21 .56 .1 2009-9-4 19:32:44 2
22 .56 .1 2009-9-4 19:32:48 2
23 .56 .1 2009-9-4 19:32:50 2
24 .56 .4 2009-9-4 19:32:54 2
25 .56 .4 2009-9-4 19:32:57 2
26 .56 .4 2009-9-4 19:34:10 2
27 .56 .4 2009-9-4 19:34:13 2
28 .56 .4 2009-9-4 19:34:15 2
29 .56 .4 2009-9-4 19:34:19 2
30 1 0 2009-9-5 1:28:04 1
31 5 0 2009-9-5 1:28:09 1
32 2 0 2009-9-5 9:40:51 1
33 12 0 2009-9-5 9:43:02 1
34 12 2 2009-9-5 9:43:07 1
35 3 4 2009-9-5 10:20:40 1
36 3 4 2009-9-5 10:20:45 1
37 5.1 3.5 2009-9-5 11:50:47 1

解决方案 »

  1.   

    select isnull( sum(cast( us_存款 as decimal)    ),0) as 总存储金额 ,isnull( sum(cast(us_消费 as decimal)) ,0) as 总消费金额 from pt_Use where 卡号=@卡号 group by 卡号
      

  2.   

    sum(cast( us_存款 as decimal(18,2)   )
      

  3.   


    select isnull( sum(cast( us_存款 as decimal(10,2))    ),0) as 总存储金额 ,isnull( sum(cast(us_消费 as decimal(10,2))) ,0) as 总消费金额 from pt_Use where 卡号=@卡号 group by 卡号
    --精度根据实际情况设置
      

  4.   

    select 
      isnull( sum(cast( us_存款 as decimal(18,2))    ),0) as 总存储金额 ,
      isnull( sum(cast(us_消费 as decimal(18,2))) ,0) as 总消费金额 
    from 
      pt_Use 
    where 
      卡号=@卡号 
    group by 
     卡号
      

  5.   

    --> 测试数据:#pt_Use
    if object_id('tempdb.dbo.#pt_Use') is not null drop table #pt_Use
    create table #pt_Use([us_Id] int,[us_存款] numeric(4,2),[us_消费] numeric(2,1),[us_PublishTime] datetime,[卡号] int)
    insert #pt_Use
    select 19,.56,0,'2009-9-4 19:32:29',2 union all
    select 20,.56,.5,'2009-9-4 19:32:39',2 union all
    select 21,.56,.1,'2009-9-4 19:32:44',2 union all
    select 22,.56,.1,'2009-9-4 19:32:48',2 union all
    select 23,.56,.1,'2009-9-4 19:32:50',2 union all
    select 24,.56,.4,'2009-9-4 19:32:54',2 union all
    select 25,.56,.4,'2009-9-4 19:32:57',2 union all
    select 26,.56,.4,'2009-9-4 19:34:10',2 union all
    select 27,.56,.4,'2009-9-4 19:34:13',2 union all
    select 28,.56,.4,'2009-9-4 19:34:15',2 union all
    select 29,.56,.4,'2009-9-4 19:34:19',2 union all
    select 30,1,0,'2009-9-5 1:28:04',1 union all
    select 31,5,0,'2009-9-5 1:28:09',1 union all
    select 32,2,0,'2009-9-5 9:40:51',1 union all
    select 33,12,0,'2009-9-5 9:43:02',1 union all
    select 34,12,2,'2009-9-5 9:43:07',1 union all
    select 35,3,4,'2009-9-5 10:20:40',1 union all
    select 36,3,4,'2009-9-5 10:20:45',1 union all
    select 37,5.1,3.5,'2009-9-5 11:50:47',1--> 查询
    select 
      卡号,
      isnull( sum(cast( us_存款 as decimal(18,2))    ),0) as 总存储金额 ,
      isnull( sum(cast(us_消费 as decimal(18,2))) ,0) as 总消费金额 
    from 
      #pt_Use 
    group by 
     卡号/**
    卡号          总存储金额                                   总消费金额
    ----------- --------------------------------------- ---------------------------------------
    1           43.10                                   13.50
    2           6.16                                    3.20(2 行受影响)
    **/
      

  6.   

    decimal(P ,s)
    s是小数点后的位数
      

  7.   

    decimal 和 numeric
    带定点精度和小数位数的 numeric 数据类型。decimal[(p[, s])] 和 numeric[(p[, s])]定点精度和小数位数。使用最大精度时,有效值从 - 10^38 +1 到 10^38 - 1。decimal 的 SQL-92 同义词是 dec 和 dec(p, s)。p(精度)指定小数点左边和右边可以存储的十进制数字的最大个数。精度必须是从 1 到最大精度之间的值。最大精度为 38。s(小数位数)指定小数点右边可以存储的十进制数字的最大个数。小数位数必须是从 0 到 p 之间的值。默认小数位数是 0,因而 0 <= s <= p。最大存储大小基于精度而变化。接点分