This is further progress of question here: how to use "Stuff and 'For Xml Path'" to unite rows in table
I have 3 tables - invoices, positions and accounts which build common table by SELECT as specified in result below. I need to exclude duplicate rows which appear due to different accounts for same invoices. I think it can be solved by “Stuff and 'For Xml Path'” and/or by UNION, but I don't really know how to do that.
Please assist with this issue. Thank You in advance.
Here's dbfiddle with DL and sample data population: https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=6401c2886a24b21239dade27e8c549ec
Using DevExpress dashboard designer based on SQL Server 2016.
Query:
-- DDL and sample data population, start
CREATE TABLE invoices
(
invoice VARCHAR(20) NOT NULL,
id INT NOT NULL,
);
INSERT invoices(invoice, id)
VALUES ('ty20210110', 1);
INSERT invoices(invoice, id)
VALUES ('ty20210111', 2);
INSERT invoices(invoice, id)
VALUES ('ty20210112', 3);
CREATE TABLE positions
(
position VARCHAR(20) NOT NULL,
quantity INT NOT NULL,
price INT NOT NULL,
summ INT NOT NULL,
invoice INT NOT NULL,
);
INSERT positions(position, quantity, price, summ, invoice)
VALUES ('p1000001', 2, 100, 200, 1);
INSERT positions(position, quantity, price, summ, invoice)
VALUES ('p1000002', 3, 100, 300, 2);
INSERT positions(position, quantity, price, summ, invoice)
VALUES ('p1000003', 1, 250, 250, 2);
INSERT positions(position, quantity, price, summ, invoice)
VALUES ('p1000004', 2, 120, 240, 1);
INSERT positions(position, quantity, price, summ, invoice)
VALUES ('p1000005', 4, 100, 400, 1);
INSERT positions(position, quantity, price, summ, invoice)
VALUES ('p1000006', 3, 100, 300, 1001);
INSERT positions(position, quantity, price, summ, invoice)
VALUES ('p1000007', 5, 80, 400, 3);
INSERT positions(position, quantity, price, summ, invoice)
VALUES ('p1000008', 5, 100, 500, 3);
CREATE TABLE accounts
(
account INT NOT NULL,
invoice INT NOT NULL,
);
INSERT accounts(account, invoice)
VALUES (01, 2);
INSERT accounts(account, invoice)
VALUES (02, 2);
INSERT accounts(account, invoice)
VALUES (03, 1);
INSERT accounts(account, invoice)
VALUES (04, 3);
INSERT accounts(account, invoice)
VALUES (05, 1);
INSERT accounts(account, invoice)
VALUES (06, 3);
-- DDL and sample data population, end
SELECT
positions.position, positions.quantity, positions.price, positions.summ,
invoices.invoice,
accounts.account
FROM
positions
INNER JOIN
invoices invoices ON invoices.id = positions.invoice
INNER JOIN
accounts accounts ON invoices.id = accounts.invoice
Result:
position | quantity | price | summ | invoice | account |
---|---|---|---|---|---|
p1000001 | 2 | 100 | 200 | in20210110 | 3 |
p1000001 | 2 | 100 | 200 | in20210110 | 5 |
p1000002 | 3 | 100 | 300 | in20210111 | 1 |
p1000002 | 3 | 100 | 300 | in20210111 | 2 |
p1000003 | 1 | 250 | 250 | in20210111 | 1 |
p1000003 | 1 | 250 | 250 | in20210111 | 2 |
p1000004 | 2 | 120 | 240 | in20210110 | 3 |
p1000004 | 2 | 120 | 240 | in20210110 | 5 |
p1000005 | 4 | 100 | 400 | in20210110 | 3 |
p1000005 | 4 | 100 | 400 | in20210110 | 5 |
p1000006 | 3 | 100 | 300 | in20210110 | 3 |
p1000006 | 3 | 100 | 300 | in20210110 | 5 |
p1000007 | 5 | 80 | 400 | in20210112 | 4 |
p1000007 | 5 | 80 | 400 | in20210112 | 6 |
p1000008 | 5 | 100 | 500 | in20210112 | 4 |
p1000008 | 5 | 100 | 500 | in20210112 | 6 |
Required result 1:
position | quantity | price | summ | invoice | account |
---|---|---|---|---|---|
p1000001 | 2 | 100 | 200 | in20210110 | 3, 5 |
p1000004 | 2 | 120 | 240 | in20210110 | 3, 5 |
p1000005 | 4 | 100 | 400 | in20210110 | 3, 5 |
p1000006 | 3 | 100 | 300 | in20210110 | 3, 5 |
p1000002 | 3 | 100 | 300 | in20210111 | 1, 2 |
p1000003 | 1 | 250 | 250 | in20210111 | 1, 2 |
p1000007 | 5 | 80 | 400 | in20210112 | 4, 6 |
p1000008 | 5 | 100 | 500 | in20210112 | 4, 6 |
Required result 2:
position | quantity | price | summ | invoice | account 1 | account 2 |
---|---|---|---|---|---|---|
p1000001 | 2 | 100 | 200 | in20210110 | 3 | 5 |
p1000004 | 2 | 120 | 240 | in20210110 | 3 | 5 |
p1000005 | 4 | 100 | 400 | in20210110 | 3 | 5 |
p1000006 | 3 | 100 | 300 | in20210110 | 3 | 5 |
p1000002 | 3 | 100 | 300 | in20210111 | 1 | 2 |
p1000003 | 1 | 250 | 250 | in20210111 | 1 | 2 |
p1000007 | 5 | 80 | 400 | in20210112 | 4 | 6 |
p1000008 | 5 | 100 | 500 | in20210112 | 4 | 6 |