I’m currently facing an issue that my Oracle knowledge cannot solve, I’m definitely not DB expert and that’s why I ask you if you have any idea how to solve my SQL query issue.
Here’s my problem, I have two tables, let’s call them DEVICE_TABLE and COUNT_TABLE
COUNT_TABLE looks like :
DEVICE (Int) PK | QUANTITY (Int)
- - - - - - - - - - - - - - - - - - - - - - - - - - -
1001 | 4
- - - - - - - - - - - - - - - - - - - - - - - - - - -
1002 | 20
- - - - - - - - - - - - - - - - - - - - - - - - - - -
1003 | 1
…
DEVICE_TABLE looks like :
ID (Int) PK | WiFi (String) | Email (String) | Bluetooth(String) | …
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1001 | Yes | No | No | …
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1002 | Yes | Yes | No | …
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1003 | Unknown | Unknown | Yes | …
…
Constraints are :
DEVICE_TABLE.ID = COUNT_TABLE.DEVICE
WiFi, Email, Bluetooth… are Strings that can only be : “Yes”, “No” or “Unknown”
Finally, my SQL request result expected is (based on my example):
Feature | Yes | No | Unknown
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
WiFi | 24 | 0 | 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Email | 20 | 4 | 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Bluetooth | 1 | 24 | 0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
…
In few words, aim of this request is to sum all devices count that are compatible with a particular feature.
Thank you in advance if you have any clue on how to achieve this ! (Maybe it is not possible…)
In Oracle 11, you can use the
pivotclause together with theunpivotclause:Alternatively, you might want to join the two existing tables to a third table that containts the values for the three desired rows. It’s probably a bit easier to read, too: