Grouping Columns or Making rows into columns

pdsqsql 431 Reputation points
2022-02-25T03:41:42.467+00:00

Hello,
I am trying to build the query based on following report data I would like to publish.

TREELEVEL   TempID  TempPID Label   Name  
0   33430   34774   Price Folder    Price Folder  
1   33433   33430   Program 1.0 Program  
1   33435   33430   Records 2.0 Records  
1   33437   33430   ForwardPrices   3.0 dForwardPrices  
1   33439   33430   CashPrices  4.0 CashPrices  
1   34368   33430   Rates   5.0 Rates  
1   34490   33430   Email   6.0 Email   

If you see above table data, TreeLevel = 0 is Parent records and underneath all the different sub folder so TempID = 33430 is parent TempID and underneath all the TempPID is same 33430, I need all the records including parent.

Query

 SELECT   TREELEVEL, TempID, TempPID  ,  Label, Name FROM Instances  
 WHERE [TempPID] =  33430  
GROUP BY  TREELEVEL, TempID, TempPID  ,  Label, Name  
ORDER BY Label  

Blockquote

![177717-image.png]2

So my result will like:

177649-image.png

Note: I need for each TempPID and corresponding TempID in group, There are multiple Labels (Folders) so each in own separate group

Thanks

Developer technologies Transact-SQL
{count} votes

7 answers

Sort by: Most helpful
  1. pdsqsql 431 Reputation points
    2022-03-05T00:19:08.563+00:00

    Thanks Erland.
    I was trying to explain that within my data i am getting overflow error not accusing as I should be grateful you guys for helping and really appreciate it.
    I provided only few inserts and it's a big table so I when I was running the query, I was getting following result as a example

    TREELEVEL    TempID    TempPID      Label                Name
     0                   33430        34774    Price Folder      Price Folder
     0                   33430         34774    Price Folder     1.0 Price Folder
     0                   33430         34774    Price Folder      2.0 Price Folder
     0                   33430          34774    Price Folder     3.0 Price Folder
     0                   33430          34774    Price Folder     4.0 Price Folder
     0                   33430          34774    Price Folder      5.0 Price Folder
     0                   33430           34774    Price Folder     6.0 Price Folder
    

    So it's repeating Label and Name value, only adding line numbers or record numbers like for 2nd record, for Name column, it's concatenating record number and Name vlaue
    I am looking following:

    TREELEVEL    TempID    TempPID    Label    Name
     0    33430    34774    Price Folder    Price Folder
     1    33433    33430    Program    1.0 Program
     1    33435    33430    Records    2.0 Records
     1    33437    33430    ForwardPrices    3.0 dForwardPrices
     1    33439    33430    CashPrices    4.0 CashPrices
     1    34368    33430    Rates    5.0 Rates
     1    34490    33430    Email     6.0 Email 
    

  2. pdsqsql 431 Reputation points
    2022-03-09T22:03:43.79+00:00

    Thanks Erland.
    Completely agreed with your views and concerns.
    I have created script for Table and Insert based on what's the report I am looking and that might confusing as when I am running query in actual table it has 100K+ Records so it's pulling different results.
    Basically, I am looking the report look like if you compare with with Folders and each Folder has Sub-Folder then Folder has another Folder and underneath it has Files but sometimes different folders has different sub-folder and files so i need to grab that kind of hierarchy report.
    Ex. In Windows File Explorer if you see following structure:

    C:\ => Users ==>
    Admin
    Default
    Public

    Then Admin folder has different files and may be sub-folders

    Default

    It has following folders
    AppData
    Desktop
    Document
    Downloads
    Favorites
    Pictures

    Then AppData Folder has following Sub-Folders
    Local
    Roaming

    Then Local folder has another sub-folder like
    Micorsoft
    Ms help
    Temp

    I hope I have explained clearly now?

    Thanks for your help!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.