数据根据时间

时间:2016-10-19 07:26:04

标签: sql sql-server-2008 datetime

我的数据中的table1是

    ALTER procedure [dbo].[spdata]
@fromdate datetime,
@todate datetime,
@Region varchar(50)
as
Select (Select Sum(Convert(int,SF)) from RVU inner dbo.VI vh on RVU.FID = vh.FID WHERE vh.No = Q.No and ID in (
Select ID from RU WHERE CAST(StartDate as date)>=  CAST(@fromdate as date) and  CAST(EndDate as date)<= CAST(@todate as date)
))  as SF 
 from (
Select 
S.Name, 
S.No,
SUM(Case when s.Vme='Car' then total else 0 end) as CAR,
SUM(Case when s.Vme='Tin' then total else 0 end) as Tin,
SUM(Case when s.Vme='Cake' then total else 0 end) as Cake,
SUM(Case when s.Vme='Flow' then total else 0 end) as  Flow,
SUM(Case when s.Vme='Unit' then total else 0 end) as Unit,
SUM(total) total ,
MAX(S.Speed) Speed
 from (
Select vh.Name as Name,vh.No as No,VV.Vame,count(VV.Vme) as total, RV.SF as MA,
RV.Speed from  VVU VV inner join RVU RV on VV.MID=RV.ID inner join RU RU on RV.ID=RU.ID
left join dbo.VI vh  on RV.FID = vh.FID WHERE CAST(RU.StartDate as date)>=  CAST(@fromdate as date) and CAST(RU.EndDate as date)<= CAST(@todate as date)  and
RU.Name_C= @Name_C AND Vme <> '' Group By vh.Name, vh.No, VV.Vme, RV.SF,
RV.Speed ) S GROUP BY s.RegNo, s.Name) Q

和sp是

[spdata]  '2016-07-01 00:00:00.000', '2016-07-31 23:59:59.000', 'pARIS'

Name   No   CAR Tin Cake Flow Unit    total Speed   SF
John   412  0    0  12     0    5     17       82    60
Mike   48   2    1   5     1    3      9      160    464
ACNme  438  0    1   5     2    3     11      10     264
XYZ    248  0    1   5     3    3     12      60     244
当我输入参数DATA IS

时,从那个sp

'2016-07-01 02:02:00.000', '2016-07-31 12:59:59.000',

现在我想在我改变时间的时候&#39; 2016-07-01 00:00:00.000&#39;,&#39; 2016-07-31 23:59:59.000&#39;,

像这样

var obj = {
        name : "Jenny",
        roll : "1000"
}

然后记录也反映在这个时间意味着根据日期加时间数据将显示

1 个答案:

答案 0 :(得分:0)

不要将你的StartDate,EndDate,@ fromdate,@ date作为日期。

 `Alter procedure [dbo].[spdata]
        @fromdate datetime,
        @todate datetime,
        @Region varchar(50)
        as
        Select (Select Sum(Convert(int,SF)) from RVU inner dbo.VI vh on RVU.FID = vh.FID WHERE vh.No = Q.No and ID in (
        Select ID from RU WHERE StartDate >=  @fromdate  and  EndDate <=@todate 
        ))  as SF 
         from (
        Select 
        S.Name, 
        S.No,
        SUM(Case when s.Vme='Car' then total else 0 end) as CAR,
        SUM(Case when s.Vme='Tin' then total else 0 end) as Tin,
        SUM(Case when s.Vme='Cake' then total else 0 end) as Cake,
        SUM(Case when s.Vme='Flow' then total else 0 end) as  Flow,
        SUM(Case when s.Vme='Unit' then total else 0 end) as Unit,
        SUM(total) total ,
        MAX(S.Speed) Speed
         from (
        Select vh.Name as Name,vh.No as No,VV.Vame,count(VV.Vme) as total, RV.SF as MA,
        RV.Speed from  VVU VV inner join RVU RV on VV.MID=RV.ID inner join RU RU on RV.ID=RU.ID
        left join dbo.VI vh  on RV.FID = vh.FID WHERE RU.StartDate >=  @fromdate  and RU.EndDate <= @todate   and
        RU.Name_C= @Name_C AND Vme <> '' Group By vh.Name, vh.No, VV.Vme, RV.SF,
        RV.Speed 
        ) S GROUP BY s.RegNo, s.Name) Q`