-1

I have the following SQL Server table:

+-------+----------+----------+----------+
| group | subgroup | position |  value   |
+-------+----------+----------+----------+
| D924  | A        | 50       | 9144142  |
| D924  | A        | 52       | 9268118  |
| D924  | A        | 60       | 9144588  |
| D924  | A        | 70       | 10116006 |
| D924  | A        | 110      | 9074177  |
| D924  | A        | 171      | 7367052  |
| D924  | A        | 180      | 10118595 |
| D924  | A        | 190      | 9074522  |
| D924  | B        | 150      | 12423396 |
| D955  | ...      | ...      | ...      |
+-------+----------+----------+----------+

I need to list all the position for every subgroup within the same group

Like so:

+-------+----------+----------+----------+
| group | subgroup | position |  value   |
+-------+----------+----------+----------+
| D924  | A        | 50       | 9144142  |
| D924  | A        | 52       | 9268118  |
| D924  | A        | 60       | 9144588  |
| D924  | A        | 70       | 10116006 |
| D924  | A        | 110      | 9074177  |
| D924  | A        | 171      | 7367052  |
| D924  | A        | 180      | 10118595 |
| D924  | A        | 190      | 9074522  |
| D924  | A        | 150      |          |
| D924  | B        | 50       |          |
| D924  | B        | 52       |          |
| D924  | B        | 60       |          |
| D924  | B        | 70       |          |
| D924  | B        | 110      |          |
| D924  | B        | 171      |          |
| D924  | B        | 180      |          |
| D924  | B        | 190      |          |
| D924  | B        | 150      | 12423396 |
| D955  | ...      | ...      | ...      |
+-------+----------+----------+----------+

I would like to achieve the result table in a single SQL query. Can you advise?

2

3 Answers 3

2

This is simply a DISTINCT list of the position and [Group] & subgroup values with a LEFT JOIN back to the table.

Doing a 2 DISTINCT queries will be expensive, so if you have a table of your groups and positions, I would suggest using those, rather than the CTEs:

WITH Groups AS
    (SELECT DISTINCT
            [group],
            subgroup
     FROM dbo.YourTable),
Positions AS
    (SELECT DISTINCT
            position
     FROM dbo.YourTable)
SELECT G.[Group],
       G.subgroup,
       P.Position,
       YT.[value]
FROM Groups G
     CROSS JOIN Positions P
     LEFT JOIN dbo.YourTable YT ON G.[Group] = YT.[Group]
                               AND G.subgroup = YT.subgroup
                               AND P.Position = YT.Position;
2
  • This is very close to the result. The only problem is that your query give me the positions of other Groups, i only want the positions 50,52,60,70,110,150,171,180 and 190. If there is the position 390 for the group D955, I don't want it in the D924 rows
    – JohanB
    Mar 4, 2020 at 13:09
  • Seems like you haven't given enough sample data here @JohanB . This a DISTINCT on the groups and subgroups though, so not entirely sure of the problem here.
    – Thom A
    Mar 4, 2020 at 13:11
0

You seem to only want the value for the first "position" in each group. That suggests row_number():

select group, subgroup, position,
       (case when row_number() over (partition by group, position order by subgroup) = 1
             then value
        end) as value
from t;

Here is a db<>fiddle.

2
  • This is giving me the exact same result at the source table
    – JohanB
    Mar 4, 2020 at 13:10
  • @JohanB . . . I added a db<>fiddle to illustrate that it works. Mar 4, 2020 at 13:16
0

Select distinct subgroups and positions first, then join them and outer join your table.

with sub as (select distinct group, subgroup from mytable)
   , pos as (select distinct group, position from mytable)
select
  sub.group. sub.subgroup, pos.position, t.value
from sub
join pos on pos.group = sub.group
left join mytable t on t.group = sub.group
                   and t.subgroup = sub.subgroup
                   and t.position = pos.position
order by t.group, t.subgroup, t.position;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.