T
T
TemaKam2021-07-10 16:33:10
SQL Server
TemaKam, 2021-07-10 16:33:10

How to group by 2 columns separately?

table
IQdiJex.jpg

you need to create a view where there will be cena sums for each id_pos_p and for each id_orugie_p
I try to do it through UNION, but it displays 2 columns, not 4
request
CREATE VIEW bux_filter AS
SELECT id_orugie_p AS 'id orugie', SUM(cena) AS 'sum cena orugie' FROM postavki_filter
GROUP BY id_orugie_p
UNION ALL
SELECT id_pos_p AS 'id post', SUM(cena) AS 'sum cena post' FROM postavki_filter
GROUP BY id_pos_p


it turns out
qojZeoF.jpg

need
60e9cdcf5954d684210206.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2021-07-11
@TemaKam

Agree that you want the strange, but even strange things can be solved. For example row_number and full join

SELECT 
  orugie, sum_cena_orugie, pos, sum_cena_pos
FROM (
  	SELECT row_number() over (order by id_orugie_p) orugie_rn,  id_orugie_p orugie, SUM(cena) sum_cena_orugie
  FROM test
  GROUP BY id_orugie_p
) t_orugie
FULL JOIN (
  SELECT row_number() over (order by id_pos_p) pos_rn,  id_pos_p pos, SUM(cena) sum_cena_pos
  FROM test
  GROUP BY id_pos_p
) t_pos on orugie_rn = pos_rn;

Test MS SQL queries online

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question