0

I want to build dynamic sql query based on following

SELECT P.Trackingno,PA.SKUId,SC.SKUName,count(PA.SKUId) as TotalSKUId 
        ,sum(case when PA.IsAvailable = 1 then 1 else 0 end) AS IsAvailable     
        FROM ADMIN.posavailability PA
        LEFT OUTER JOIN Admin.SKUCreation SC ON SC.TCID=PA.Skuid
        LEFT OUTER JOIN Admin.POSVisitDetails PD on PD.VisitId=PA.VisitID
        LEFT OUTER JOIN Admin.POS P ON P.TrackingNo=PD.TrackingNo       
        WHERE PA.VisitId in
        (SELECT visitid FROM Admin.POSVisitDetails PD  WHERE PD.month=2 and PD.year=2017) 
        and PA.IsActive=1
        GROUP BY P.Trackingno,PA.SKUId,SC.SKUName,PD.Month,PD.year
        ORDER BY P.Trackingno

I got out as follows:

enter image description here

My desired output is:

enter image description here

Can any one help for dynamic pivot SQL query.

4
  • Does it need to be dynamic because the SKUName can change? EDIT: Also how do you get the "totalSKUId"? Commented Mar 20, 2017 at 4:58
  • SKUName can change. Commented Mar 20, 2017 at 5:03
  • Do this in your application code. Commented Mar 20, 2017 at 5:35
  • @Hussain try my answer. Commented Mar 20, 2017 at 5:43

2 Answers 2

2

Here maybe help you. Dynamic sql query

        CREATE TABLE TrackingTbl 
        (
            TrackingNo int,
            SKUID int,
            SKUName varchar(50),
            TotalSKUID int,
            IsAvaiable int
        )
        INSERT INTO TrackingTbl VALUES (1234,1,'Red',2,2)
        INSERT INTO TrackingTbl VALUES (1234,2,'White',2,1)
        INSERT INTO TrackingTbl VALUES (1234,3,'Blue',2,0)
        INSERT INTO TrackingTbl VALUES (1234,4,'Yellow',2,2)
        INSERT INTO TrackingTbl VALUES (3456,1,'Red',3,3)
        INSERT INTO TrackingTbl VALUES (3456,2,'White',3,2)
        INSERT INTO TrackingTbl VALUES (3456,3,'Blue',3,1)
        INSERT INTO TrackingTbl VALUES (3456,4,'Yellow',3,0)

        DECLARE @Columns varchar(200) 

        SET @Columns = Stuff((SELECT concat(', [',td.SKUName,']') FROM (select DISTINCT tt.SKUName  FROM TrackingTbl tt ) td FOR XML PATH (''))
                            ,1,1,'')

        DECLARE @Query nvarchar(max) = CONCAT(
        'SELECT TrackingNo,TotalSKUID,',@Columns,
        ' FROM 
        (
            SELECT tt.IsAvaiable, tt.SKUName ,tt.TrackingNo, tt.TotalSKUID
             FROM TrackingTbl tt
        ) sc
        PIVOT
        (
            sum(IsAvaiable) FOR SKuName IN (',@Columns,' )
        ) pvt')

        exec sp_executesql  @Query
        DROP TABLE dbo.TrackingTbl
Sign up to request clarification or add additional context in comments.

Comments

0

Try this using MAX ()

 ;with cte as (
SELECT P.Trackingno,PA.SKUId,SC.SKUName,count(PA.SKUId) as TotalSKUId 
    ,sum(case when PA.IsAvailable = 1 then 1 else 0 end) AS IsAvailable     
    FROM ADMIN.posavailability PA
    LEFT OUTER JOIN Admin.SKUCreation SC ON SC.TCID=PA.Skuid
    LEFT OUTER JOIN Admin.POSVisitDetails PD on PD.VisitId=PA.VisitID
    LEFT OUTER JOIN Admin.POS P ON P.TrackingNo=PD.TrackingNo       
    WHERE PA.VisitId in
    (SELECT visitid FROM Admin.POSVisitDetails PD  WHERE PD.month=2 and PD.year=2017) 
    and PA.IsActive=1
    GROUP BY P.Trackingno,PA.SKUId,SC.SKUName,PD.Month,PD.year
    ORDER BY P.Trackingno
   )

 select trackingno,totalskuid,max(case when skuname='Red' then isavailable else '' end) Red,
                  max(case when skuname='White' then isavailable else '' end) White,
                  max(case when skuname='Blue' then isavailable else '' end) Blue,
                  max(case when skuname='Yellow' then isavailable else '' end) Yellow
from cte
group by trackingno,totalskuid

1 Comment

Thanks for your efforts, Here SKUName cant be static. It should be dynamic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.