sql server - SQL Query help using Pivot -


I have a low stored procedure that is working perfectly, but at the end of the row the horizontal total is needed , For example the following. . It should be done on every line.

[1] [2]. [3] ....................... ........................ [2 9]. [30] .. [31] ... total

.. help please

  create process [dbo]. [VTR_Report_DaysOfMonthWise] @Month Integer, @year integer, declared @Branch_ID as integer @startDate varchar (20) Start @EndDate varchar declaration (20) Select @startDate = Convert (varchar (10), DATEADD (m, @ Month - 1, DATEADY (yyyy, @Year - 1900, 0)), 105 SELECT @endDate = Convert (varchar (10), DATEADD (d, -1), date (m, @month, DATEADD (yyyy) , @Year - 1900, 0), 105) Select * from (select CLLName, DATEPART (DD, CD.VRRPSPET) as 'DAY', ISNULL (Zodiac (cast) (Cd.vtrvalue as integer)), 0) dbo.VTRCheckList c c.CLid = c.CLid on the left as 'VTRValue' from the external Get the VTRCheckListDetails CD and convert (date, cd.vtrRespDate, 105) & gt; = changed (date, @startdown, 105) A nd convert (date, cd.vtrRespDate, 105) & lt; = convert (date, @ ENDDATE) , 105) and cd.branchid = @Branch_ID Group c.CLName, DatePart (day, cd.vtrRespDate)) for an axis (sum (VTRValue) day ([1], [2], [3], [4] [15], [16], [5], [6], [7], [8], [9], [10], [11] [12], [13], [ 17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [2] 9], [30], [31]) B ED    

In your internal query to draw as a separate column Add a subquery, something like this, a subquery call called Monthqual. (I assume that vtrRespDate is a date field.)

  Select c.CL name, DATEPART (dd, cd.vtrRespDate) as 'day', ISNULL (sum (Cast (cd.vtrvalue as ()), 0 as 'VTRValue', (select ISNULL (zodiac (x.vtrvalue as int (cast)), 0) VTRCheckListDetails x WHERE YEAR (x.vtrRespDate) ) = YEAR (cd.vtrRespDate) and month (x .vtrRespDate) = month (cd.vtrRespDate) and convert (date, cd.vtrRespDate, 105) & gt; = changed (date, @ STARTDATE, 105) and convert (date) , Cd.vtrRespDate, 105) & lt; = changed (date @ ENDDATE, 105)) dbo.VTRCheckList from MonthTotal as c c.CLid = c.CLid VTRCheckListDetails in the CD Join and convert (date, cd.vtrRespDate, 105) & gt; = changed (date, @ STARTDATE, 105) and convert (date, cd.vtrRespDate, 105) & lt; = changed (date, @ ENDDATE, 105 ) C.CLName, DatePart (day, cd.vtrRespDate) and cd.branchid = @Branch_ID Group    

Comments