Hi Everyone I am trying to create a query to create two separate columns with comma sepearated values from one row.
Below is the create table statement
CREATE TABLE logs-table
CREATE TABLE [dbo].Accounts
INSERT INTO logs_table(AccountId,UtcActionDate,ActionType,ActionSubType,PrimaryArgs)
VALUES ('7DADAEDB-A0E5-4290-8D94-8D3C8144A662','2022-02-15 16:12:56.000', 'Exchange','AddedToWatchlist', 'Trade-share1'),
('7DADAEDB-A0E5-4290-8D94-8D3C8144A662','2022-02-15 16:55:22.000', 'Exchange','AddedToWatchlist','Trade-share2'),
('7DADAEDB-A0E5-4290-8D94-8D3C8144A662','2022-02-15 16:55:27.000', 'Exchange','AddedToWatchlist','Issuer-SPV'),
('9DADAEDB-A0E5-4290-8D94-8D3C8144A662','2022-02-15 16:55:22.000', 'Exchange','AddedToWatchlist','Trade-share1'),
('9DADAEDB-A0E5-4290-8D94-8D3C8144A662','2022-02-15 16:55:27.000', 'Exchange','AddedToWatchlist','Issuer-SPV'),
('9DADAEDB-A0E5-4290-8D94-8D3C8144A662','2022-02-15 16:55:22.000', 'Exchange','AddedToWatchlist','Trade-share2'),
('9DADAEDB-A0E5-4290-8D94-8D3C8144A662','2022-02-15 16:55:27.000', 'Exchange','AddedToWatchlist','Issuer-SPV2'),
INSERT INTO Accounts
VALUES ('7DADAEDB-A0E5-4290-8D94-8D3C8144A662','tesuser1@Stuff .com'),
('9DADAEDB-A0E5-4290-8D94-8D3C8144A662','tesuser2@Stuff .com'),
The output I want will look like this
Account ID EmailAddress Trade Issuer
7DADAEDB-A0E5-4290-8D94-8D3C8144A662 tesuser1@Stuff .com share1,share2 SPV
9DADAEDB-A0E5-4290-8D94-8D3C8144A662 tesuser2@Stuff .com share1,share2 SPV,SPV2
I have tried to create query using self join but it's returning duplicate records
select A.Id, A.EmailAddress,
STRING_AGG (TRIM(RIGHT(TSPV.PrimaryArgs, LEN(TSPV.PrimaryArgs) - CHARINDEX('-', TSPV.PrimaryArgs))),',') AS 'Trade' ,
STRING_AGG (TRIM(RIGHT(TBT.PrimaryArgs, LEN(TBT.PrimaryArgs) - CHARINDEX('-', TBT.PrimaryArgs))),',') AS 'Issuers'
FROM Accounts A
INNER JOIN logs_table TSPV ON A.Id= TSPV.AccountId AND TSPV.PrimaryArgs LIKE '%Trade%' AND TSPV.ActionSubType IN ('AddedToWatchlist') AND TSPV.ActionType='Exchange'
INNER JOIN logs_table TBT ON A.Id= TBT.AccountId AND TBT.PrimaryArgs LIKE '%Issuer%' AND TBT.ActionSubType IN ('AddedToWatchlist') AND TBT.ActionType='Exchange'
Group by A.Id,A.emailaddress
Any idea how can I acheive the required output? I am doing this in view so no temp tables or curosrs?