1

I have store procedure like this:

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

I am getting out put like this:

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

but i need output in different model as

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 

so I try to write the same stored procedure by using pivot

ALTER procedure [dbo].[ParkingSummary1]
 @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)
  DECLARE @cols AS NVARCHAR(MAX),@query  AS NVARCHAR(MAX)
  select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Vtype) from VType_tbl
  FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)')  ,1,1,'')
  set @query = 'SELECT LocName, ' + @cols + '  from (select l.LocName,Vtype from Transaction_tbl t join VType_tbl v on t.vtid = v.vtid join dbo.Location_tbl l on t.locid=l.Locid  where dtime between '''+ @date1 +''' and '''+ @date2 +'''  
  and Status = 5) d  pivot  ( count(Vtype) for Vtype in (' + @cols + ')) p '
  exec sys.sp_executesql @query
end

but I while executing this am getting error like this: Column 'Location_tbl.LocName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

  • possible duplicate of [SQL Server PIVOT Column Data](http://stackoverflow.com/questions/1984306/sql-server-pivot-column-data) – Bogdan Sahlean Sep 15 '13 at 08:51
  • sir..am new in pivot concept..can you show me once please – user2747546 Sep 15 '13 at 08:54
  • See also: [question #1](http://stackoverflow.com/questions/18317732/sql-server-pivot-table-with-joins-and-dynamic-columns) or [question #2](http://stackoverflow.com/questions/16759531/dynamic-pivot-in-sql-server) – Bogdan Sahlean Sep 15 '13 at 09:02

0 Answers0