Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用函数时在sql server中透视_Sql_Sql Server - Fatal编程技术网

使用函数时在sql server中透视

使用函数时在sql server中透视,sql,sql-server,Sql,Sql Server,我有这样一个存储过程: ALTER procedure [dbo].[performance] @startdate nvarchar(100), @enddate nvarchar(100) as begin declare @date1 nvarchar(100)=convert(varchar, @startdate+'00:00:00.000',120) declare @date2 nvarchar(100)= convert(varchar, @enddate+'23:59:5

我有这样一个存储过程:

ALTER procedure [dbo].[performance] 
@startdate nvarchar(100), 
@enddate nvarchar(100) as begin declare @date1 nvarchar(100)=convert(varchar, @startdate+'00:00:00.000',120)  declare @date2 nvarchar(100)= convert(varchar, @enddate+'23:59:59.000',120)   set NOCOUNT on;
select l.LocName,v.Vtype,
 SUM(convert(numeric(18, 2), DATEDIFF(MI,t.DelDate,t.Paydate))) as TotalDiff,[dbo].[testfunctionstacknew](
   CONVERT(decimal(10,1), AVG( CONVERT(NUMERIC(18,2), DATEDIFF(SS,t.Paydate,t.DelDate) ) )))  as Average
from Transaction_tbl t left join VType_tbl v on t.vtid=v.vtid left join Location_tbl l on t.Locid=l.Locid 
where t.Locid in(select t1.Locid  from Transaction_tbl t1) and dtime between @date1 and @date2 and Status =5 group by v.Vtype,l.LocName,l.Locid order by l.Locid
end
LocName   Vtype    TotalDiff   Average
Address   Normal     15         00:10:01
Adress    vip        18         00:08:01
Address   VVIP        9         00:04:00
Address   Pass       20         00:15:00
Goldsouk  normal     45         00:18:08
Goldsouk   vip       17         00:11:36
Fashion    vip       78         00:35:25
Fashion    VVip      2          00:01:00
我是这样出来的:

ALTER procedure [dbo].[performance] 
@startdate nvarchar(100), 
@enddate nvarchar(100) as begin declare @date1 nvarchar(100)=convert(varchar, @startdate+'00:00:00.000',120)  declare @date2 nvarchar(100)= convert(varchar, @enddate+'23:59:59.000',120)   set NOCOUNT on;
select l.LocName,v.Vtype,
 SUM(convert(numeric(18, 2), DATEDIFF(MI,t.DelDate,t.Paydate))) as TotalDiff,[dbo].[testfunctionstacknew](
   CONVERT(decimal(10,1), AVG( CONVERT(NUMERIC(18,2), DATEDIFF(SS,t.Paydate,t.DelDate) ) )))  as Average
from Transaction_tbl t left join VType_tbl v on t.vtid=v.vtid left join Location_tbl l on t.Locid=l.Locid 
where t.Locid in(select t1.Locid  from Transaction_tbl t1) and dtime between @date1 and @date2 and Status =5 group by v.Vtype,l.LocName,l.Locid order by l.Locid
end
LocName   Vtype    TotalDiff   Average
Address   Normal     15         00:10:01
Adress    vip        18         00:08:01
Address   VVIP        9         00:04:00
Address   Pass       20         00:15:00
Goldsouk  normal     45         00:18:08
Goldsouk   vip       17         00:11:36
Fashion    vip       78         00:35:25
Fashion    VVip      2          00:01:00
但我需要不同型号的输出

LocName     Normal      Vip      VVip        Pass        Staff
Address     00:10:01    00:08:01  00:04:00    0           0
GoldSouck    00:18:08   00:11:36   0          0           0
Fashion      0          00:35:25   00:01:00   0           0 

因为我知道我必须用支点,但是我不知道怎么用这个?如果有人知道,请帮助我找出..我无法硬记录我的vtype..实际vtype是动态的。这来自vtype表..如果有人知道如何解决此问题..请帮助我找出..您可以手动透视:

ALTER procedure [dbo].[performance]
(
    @startdate nvarchar(100), 
    @enddate nvarchar(100)
)
as
begin
    set nocount on

    declare
        @date1 nvarchar(100)=convert(varchar, @startdate+'00:00:00.000',120)
        @date2 nvarchar(100)= convert(varchar, @enddate+'23:59:59.000',120)

    ;with cte as (
        select
            l.LocName, v.Vtype,
            sum(datediff(mi, t.DelDate, t.Paydate)) as TotalDiff,
            dbo.testfunctionstacknew(
                convert(decimal(10,1),
                    avg(convert(numeric(18,2), datediff(ss, t.Paydate, t.DelDate))
                )
            ) as Average
        from Transaction_tbl as t
            left join VType_tbl as v on t.vtid = v.vtid
            left join Location_tbl as l on t.Locid = l.Locid 
        where
            t.Locid in(select t1.Locid from Transaction_tbl as t1) and
            t.dtime between @date1 and @date2 and
            t.Status = 5
        group by v.Vtype, l.LocName, l.Locid
    )
    select
        c.LocName,
        max(case when c.Vtype = 'Normal' then Average end) as Normal,
        max(case when c.Vtype = 'Vip' then Average end) as Vip,
        max(case when c.Vtype = 'VVip' then Average end) as VVip,
        max(case when c.Vtype = 'Pass' then Average end) as Pass,
        max(case when c.Vtype = 'Staff' then Average end) as Staff
    from cte as c
    group by c.LocName
    order by c.LocName
end

执行时,am GET错误如下:从字符串转换日期和/或时间时,转换失败。am传递日期如下:2013-01-01和2013-08-01