How to make layout from SQL query

I have this select query (view) like this:

SELECT ArticleID, period, SUM(Amount) As Total 
FROM atbv_Accounting_OrdersDeliveries 
WHERE InvoiceNo IS NOT NULL 
GROUP BY ArticleID, period

That returns me this:

  • Unable to connect to localDB in VS2012 – “A network-related or instance-specific error occurred while establishing a connection to SQL Server…”
  • Sql compare TimeStamp with normal date
  • DQL Select every rows having one column's MAX value
  • Update one of 2 duplicates in an sql server database table
  • In an OUTPUT clause in an INSTEAD OF INSERT trigger, is it possible to reference both INSERTED tables?
  • SQL Server - Drop constraint and add default value at once
  • enter image description here

    Is it possible to write a select statement that would return the data like this:

    enter image description here

    2 Solutions collect form web for “How to make layout from SQL query”

    I do not have a sql server here to test this but should be something like:

    DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
    DECLARE @ColumnName AS NVARCHAR(MAX)
    
    --Get distinct values of the PIVOT Column 
    SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
           + QUOTENAME(period)
    FROM (SELECT DISTINCT period FROM atbv_Accounting_OrdersDeliveries WHERE InvoiceNo IS NOT NULL) AS periods
    
    SET @DynamicPivotQuery = 
      N'SELECT , ' + @ColumnName + '
        FROM (SELECT
        ArticleID, period, Amount
        FROM atbv_Accounting_OrdersDeliveries 
        WHERE InvoiceNo IS NOT NULL 
        ) AS T
        PIVOT(SUM(Amount) 
              FOR period IN (' + @ColumnName + ')) AS PVTTable'
    --Execute the Dynamic Pivot Query
    EXEC sp_executesql @DynamicPivotQuery
    

    More information on this for MS Sql Server can be found here

    You can to do this:

    EDIT:

    SELECT ArticleID, [201701], [201702], [201703], [201708]
    FROM
    (
        SELECT *
        FROM atbv_Accounting_OrdersDeliveries 
        WHERE InvoiceNo IS NOT NULL 
    ) T
    PIVOT
    (
      SUM(Amount)
      FOR period IN ([201701], [201702], [201703], [201708])
    ) P;
    
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.