ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 251.8k

How to represent rows as column from details table?

Jan 6 2021 3:47 AM

I work on sql server 2012 I face issue I can't represent rows as column.

I need to represent code type from details table to display as column with

values of countparts

join between master zplid and details zplid is zplid

every zplid have group of code type so instead of represent code type as rows

I will represent it as columns

columns will have header as code type and content column will be countparts

so How to do that please?
  1. create table #zplidmaster  
  2. (  
  3. zplid int,  
  4. zplidname nvarchar(50)  
  5. )  
  6. insert into #zplidmaster(zplid,zplidname)  
  7. values  
  8. (4124,'tetanium'),  
  9. (4125,'FilmCapacitor'),  
  10. (4145,'CeramicCapacitor'),  
  11. (4170,'Holetransistor'),  
  12. (4190,'resistor')  
  13.   
  14.  create table #zpliddetails  
  15.  (  
  16.  zplid int,  
  17.  CodeType  int,  
  18.  CountParts int  
  19.  )  
  20.  insert into #zpliddetails(zplid,CodeType,CountParts)   
  21.  values  
  22.  (4124,9089,9011),  
  23.  (4124,7498,7000),  
  24.  (4125,9089,2000),  
  25.  (4125,7498,1000),  
  26.  (4145,9089,3000),  
  27.  (4145,7498,8500),  
  28.  (4170,9089,7600),  
  29.  (4170,7498,6600),  
  30.  (4190,9089,9001),  
  31.  (4190,7498,9003)  
expected result :
 
image 

Answers (1)