I have table look like below. I have two type of package type for a single product. But some product has only UPC
| Product No | Barcode | Package Type | Length | Width |
| ------------ | ------------ | ------------ | ------------ | ------------ |
| 19 | 123456 | UPC | 6 | 2 |
| 19 | 789101 | UCC | 3 | 4 |
| 20 | 987568 | UPC | 2 | 5 |
| 20 | 869584 | UCC | 7 | 8 |
| 21 | 869584 | UPC | 8 | 3 |
But I need a loke like this table. Basically I want to keep a row only where package type = UCC when the product have two package type. But when there is only one package type ( e.g. product no 21) then I need to keep Package type= UPS too. Then I need to keep length and width for both package type.
| Product No | Barcode | Package Type | UCC Length | UCC Width | UPC Length | UPC Width |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| 19 | 789101 | UCC | 3 | 4 | 6 | 2 |
| 20 | 869584 | UCC | 7 | 8 | 2 | 5 |
| 21 | 869584 | UPC | NA | NA | 8 | 3 |
I've been struggling how to do it in a SQL. If any help please it would be appreciated
2条答案
按热度按时间ix0qys7i1#
使用窗口函数根据“Package Type”按字母顺序排列结果:
**注:**这不会排除只有UCC的相同产品编号。
xuo3flqw2#
这会给你你想要的输出。值得注意的是,这可能不是一个很好的解决方案,因为它不容易扩展-如果你在某个时候需要处理第三,第四,第五个包类型,它会很快变得混乱。你可能会想看看使用PIVOT。但如果你很高兴它只是两个包类型,这是简单的,它应该是明确的代码在做什么。