SQL Query for generating matrix like output querying related table in SQL Server

后端 未结 3 1788
臣服心动
臣服心动 2020-12-29 13:22

I have three tables:
Product

ProductID   ProductName  
1           Cycle  
2           Scooter  
3           Car  

<

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 14:15

    We can create matrix using pivot, this can easily done with data frames

    product|Key|Value
    A      |P  |10|
    A      |Q  |40|
    B      |R  |50|
    B      |S  |50|
    
    val newdf=df.groupBy("product").pivot("key").sum("value")
    
    |product|P   |Q   |R   |S   |
    |B      |null|null|  50|  50|
    |A      |  10|  40|null|null|
    

    We can replace null and we can do calculations as well

提交回复
热议问题