Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Please help me for this qusetion

Please help me for this qusetion

Scheduled Pinned Locked Moved Database
helpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Robymon
    wrote on last edited by
    #1

    I have two tables "CM_Master" and "CM_Details" and the sample records are below, i want to make a report like under the result. How can i join two tables and create table heading like the shift names in CM_Master table. CM_Master ShiftID ShiftName 1 Day 2 Night CM_Details CM_ID Site_ID Level_ID ShiftID Number 1 1 1 1 5 2 1 2 1 4 3 1 1 2 2 4 2 2 2 8 Result Site Level Day Night 1 1 5 2 1 2 4 0 2 2 0 8

    _ 1 Reply Last reply
    0
    • R Robymon

      I have two tables "CM_Master" and "CM_Details" and the sample records are below, i want to make a report like under the result. How can i join two tables and create table heading like the shift names in CM_Master table. CM_Master ShiftID ShiftName 1 Day 2 Night CM_Details CM_ID Site_ID Level_ID ShiftID Number 1 1 1 1 5 2 1 2 1 4 3 1 1 2 2 4 2 2 2 8 Result Site Level Day Night 1 1 5 2 1 2 4 0 2 2 0 8

      _ Offline
      _ Offline
      __TR__
      wrote on last edited by
      #2

      Try the below approach

      CREATE TABLE #CM_Master
      (
      ShiftID INT, ShiftName VARCHAR(50)
      )

      INSERT INTO #CM_Master
      SELECT 1, 'Day' UNION
      SELECT 2, 'Night'

      CREATE TABLE #CM_Detail
      (
      CM_ID INT, Site_ID INT, Level_ID INT, ShiftID INT, Number INT
      )

      INSERT INTO #CM_Detail
      SELECT 1, 1, 1, 1, 5 UNION
      SELECT 2, 1, 2, 1, 4 UNION
      SELECT 3, 1, 1, 2, 2 UNION
      SELECT 4, 2, 2, 2, 8

      --SELECT * FROM #CM_Detail

      SELECT DISTINCT CD.Site_ID, CD.Level_ID, ISNULL(A.Day,0) AS [Day], ISNULL(B.Night,0) AS Night
      FROM #CM_Detail CD
      LEFT JOIN
      (
      SELECT Site_ID, Level_ID,
      SUM(Number) [Day]
      FROM #CM_Detail
      WHERE ShiftID = 1
      GROUP BY Site_ID, Level_ID
      ) A ON CD.Site_ID = A.Site_ID AND CD.Level_ID = A.Level_ID
      LEFT JOIN
      (
      SELECT Site_ID, Level_ID,
      SUM(Number) AS [Night]
      FROM #CM_Detail
      WHERE ShiftID = 2
      GROUP BY Site_ID, Level_ID
      ) B ON CD.Site_ID = B.Site_ID AND CD.Level_ID = B.Level_ID

      and here is a generic approach where shift names are not hard coded.

      DECLARE @PivotColumnHeader VARCHAR(MAX)
      SELECT @PivotColumnHeader = COALESCE(@PivotColumnHeader + ', [' + ShiftName + ']', '[' + ShiftName + ']')
      FROM #CM_Master

      DECLARE @SQL VARCHAR(MAX)
      SET @SQL = N'SELECT * FROM
      (
      SELECT Site_ID, Level_ID, ShiftName, ISNULL(Number,0) AS Number FROM #CM_Detail CD
      INNER JOIN #CM_Master CM ON CD.ShiftID = CM.ShiftID
      ) P
      PIVOT
      (
      SUM(Number) FOR ShiftName IN (' + @PivotColumnHeader + ')
      ) AS PivotTable'

      EXECUTE (@SQL)

      R 1 Reply Last reply
      0
      • _ __TR__

        Try the below approach

        CREATE TABLE #CM_Master
        (
        ShiftID INT, ShiftName VARCHAR(50)
        )

        INSERT INTO #CM_Master
        SELECT 1, 'Day' UNION
        SELECT 2, 'Night'

        CREATE TABLE #CM_Detail
        (
        CM_ID INT, Site_ID INT, Level_ID INT, ShiftID INT, Number INT
        )

        INSERT INTO #CM_Detail
        SELECT 1, 1, 1, 1, 5 UNION
        SELECT 2, 1, 2, 1, 4 UNION
        SELECT 3, 1, 1, 2, 2 UNION
        SELECT 4, 2, 2, 2, 8

        --SELECT * FROM #CM_Detail

        SELECT DISTINCT CD.Site_ID, CD.Level_ID, ISNULL(A.Day,0) AS [Day], ISNULL(B.Night,0) AS Night
        FROM #CM_Detail CD
        LEFT JOIN
        (
        SELECT Site_ID, Level_ID,
        SUM(Number) [Day]
        FROM #CM_Detail
        WHERE ShiftID = 1
        GROUP BY Site_ID, Level_ID
        ) A ON CD.Site_ID = A.Site_ID AND CD.Level_ID = A.Level_ID
        LEFT JOIN
        (
        SELECT Site_ID, Level_ID,
        SUM(Number) AS [Night]
        FROM #CM_Detail
        WHERE ShiftID = 2
        GROUP BY Site_ID, Level_ID
        ) B ON CD.Site_ID = B.Site_ID AND CD.Level_ID = B.Level_ID

        and here is a generic approach where shift names are not hard coded.

        DECLARE @PivotColumnHeader VARCHAR(MAX)
        SELECT @PivotColumnHeader = COALESCE(@PivotColumnHeader + ', [' + ShiftName + ']', '[' + ShiftName + ']')
        FROM #CM_Master

        DECLARE @SQL VARCHAR(MAX)
        SET @SQL = N'SELECT * FROM
        (
        SELECT Site_ID, Level_ID, ShiftName, ISNULL(Number,0) AS Number FROM #CM_Detail CD
        INNER JOIN #CM_Master CM ON CD.ShiftID = CM.ShiftID
        ) P
        PIVOT
        (
        SUM(Number) FOR ShiftName IN (' + @PivotColumnHeader + ')
        ) AS PivotTable'

        EXECUTE (@SQL)

        R Offline
        R Offline
        Robymon
        wrote on last edited by
        #3

        Is it possible to make the sql dynamic, instead of hard code. eg: Day, Night some time the master table have more records.

        L _ 2 Replies Last reply
        0
        • R Robymon

          Is it possible to make the sql dynamic, instead of hard code. eg: Day, Night some time the master table have more records.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Robymon wrote:

          Is it possible to

          Yes, that's possible. The correct response to the answer would have been "thank you".

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          1 Reply Last reply
          0
          • R Robymon

            Is it possible to make the sql dynamic, instead of hard code. eg: Day, Night some time the master table have more records.

            _ Offline
            _ Offline
            __TR__
            wrote on last edited by
            #5

            I have modified the solution to include the dynamic query.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups