How can I loop through values from a table in a case statement

后端 未结 1 1140
名媛妹妹
名媛妹妹 2021-01-21 11:07

(Sorry, new to SQL). I have the following table which users can update and/or add to:

Project Type:
ID   Name  
1    Documents
2    DVD
3    Poster


        
相关标签:
1条回答
  • 2021-01-21 11:20

    You can accomplish this with conditional aggregation, like you had listed.

    SELECT
    ProductName,
    SUM(CASE WHEN Name = 'Documents' THEN 1 ELSE 0 END) as NumOfDocuments,
    SUM(CASE WHEN Name = 'DVD' THEN 1 ELSE 0 END) as NumOfDVD,
    SUM(CASE WHEN Name = 'Poster' THEN 1 ELSE 0 END) as NumOfPoster
    FROM
    YourTable
    GROUP BY ProductName
    

    DYNAMIC SQL

    IF OBJECT_ID('tempdb..#Clients') IS NOT NULL DROP TABLE #Clients
    IF OBJECT_ID('tempdb..#Products') IS NOT NULL DROP TABLE #Products
    IF OBJECT_ID('tempdb..#Projects') IS NOT NULL DROP TABLE #Projects
    IF OBJECT_ID('tempdb..#ProjectTypes') IS NOT NULL DROP TABLE #ProjectTypes
    
    CREATE TABLE #Clients (ID int, Name varchar(64))
    CREATE TABLE #Products (ID int, Name varchar(64), ClientID int)
    CREATE TABLE #Projects (ID int, Name varchar(64), ProductID int, ProjectTypeID int)
    CREATE TABLE #ProjectTypes (ID int, Name varchar(64))
    
    
    INSERT INTO #Clients (ID, Name) VALUES
    (1,'Client1'),
    (2,'Client2'),
    (3,'Client3'),
    (4,'Client4')
    
    INSERT INTO #Products (ID, Name, ClientID) VALUES
    (1,'Prod1',1),
    (2,'Prod2',1),
    (3,'Prod3',1),
    (2,'Prod2',2),
    (2,'Prod2',3),
    (3,'Prod3',3),
    (4,'Prod3',3)
    
    INSERT INTO #Projects (ID, Name, ProductID, ProjectTypeID) VALUES
    (1,'Proj1',1,1),
    (1,'Proj1',2,1),
    (1,'Proj1',3,1),
    (2,'Proj2',2,2),
    (2,'Proj2',3,2),
    (2,'Proj2',4,2),
    (3,'Proj3',4,3)
    
    INSERT INTO #ProjectTypes (ID, Name) VALUES
    (1,'ProjType1'),
    (2,'ProjType2'),
    (3,'ProjType3')
    
    
    IF OBJECT_ID('tempdb..#Summary') IS NOT NULL DROP TABLE #Summary
    
    SELECT
        c.ID,
        prod.Name as ProductName,
        proj.Name as ProjectName,
        projT.Name as ProjType
    INTO #Summary
    FROM #Clients c
        INNER JOIN
        #Products prod on prod.ClientID = c.ID
        INNER JOIN
        #Projects proj on proj.ProductID = prod.ID
        INNER JOIN
        #ProjectTypes projT on projT.ID = proj.ProjectTypeID
    
    SELECT * FROM #Summary
    
    DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
    DECLARE @ColumnName AS NVARCHAR(MAX)
    
    --Get distinct values of the PIVOT Column 
    SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
           + QUOTENAME(ProjType)
    FROM (SELECT DISTINCT ProjType FROM #Summary) AS Names
    
    --Prepare the PIVOT query using the dynamic 
    SET @DynamicPivotQuery = 
      N'SELECT ProductName, ' + @ColumnName + '
        FROM #Summary
        PIVOT(COUNT(ProjType)
              FOR ProjType IN (' + @ColumnName + ')) AS PVTTable'
    --Execute the Dynamic Pivot Query
    EXEC sp_executesql @DynamicPivotQuery
    

    So using your tables, you should be able to just run this... it may need slight tweaking.

    IF OBJECT_ID('tempdb..#Summary') IS NOT NULL DROP TABLE #Summary
    
    SELECT
        c.ID,
        prod.Name as ProductName,
        proj.Name as ProjectName,
        projT.Name as ProjType
    INTO #Summary
    FROM Clients c
        INNER JOIN
        Products prod on prod.ClientID = c.ID
        INNER JOIN
        Projects proj on proj.ProductID = prod.ID
        INNER JOIN
        ProjectTypes projT on projT.ID = proj.ProjectTypeID
    
    
    DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
    DECLARE @ColumnName AS NVARCHAR(MAX)
    
    --Get distinct values of the PIVOT Column 
    SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
           + QUOTENAME(ProjType)
    FROM (SELECT DISTINCT ProjType FROM #Summary) AS Names
    
    --Prepare the PIVOT query using the dynamic 
    SET @DynamicPivotQuery = 
      N'SELECT ProductName, ' + @ColumnName + '
        FROM #Summary
        PIVOT(COUNT(ProjType)
              FOR ProjType IN (' + @ColumnName + ')) AS PVTTable'
    --Execute the Dynamic Pivot Query
    EXEC sp_executesql @DynamicPivotQuery
    

    Tutorial

    0 讨论(0)
提交回复
热议问题