I have a table defined like this
CREATE TABLE OrderItemRoomings(
OrderItemRoomingId int IDENTITY(1,1) NOT NULL,
OrderItemId int NOT NULL,
PaxId int NOT NULL,
GroupNumber tinyint NOT NULL)
I’m trying to get all OrderItemId which, when grouped, have differents group numbers
Assuming this table
+-----------------------------------+
| OrderItemId | PaxId | GroupNumber |
+-----------------------------------+
| 101 | 501 | 1 |
+-----------------------------------+
| 101 | 502 | 1 |
+-----------------------------------+
| 102 | 503 | 2 |
+-----------------------------------+
| 102 | 504 | 2 |
+-----------------------------------+
| 103 | 505 | 1 |
+-----------------------------------+
| 103 | 506 | 2 |
+-----------------------------------+
I want the query to returns 103 because there are two different group number for id 103.
I can’t figure out the GROUP BY query which would check all sub result detail.
Use
HAVINGto do filtering on grouped sets. Here we show only orderitemid records with more than one unique groupnumber value.