This question already has answers here:
How to concatenate text from multiple rows into a single text string in SQL Server
(47 answers)
Closed 7 years ago.
So I've got this table
Email Username
------------------------- -------------------------
a#a.com a1
a#a.com a2
a#a.com a3
a#a.com a4
b#b.com b1
b#b.com b2
b#b.com b3
b#b.com b4
c#c.com c1
c#c.com c2
c#c.com c3
c#c.com c4
(12 row(s) affected)
But I want each e-mail address to appear once, then all associated Usernames to be listed after it, if that's possible..
Any help whatsoever is appreciated,
Cheers.
Edit
Ok, so apparently, all the usernames will come under one column, all concatenated together, sounds kinda dumb, but that's what I've been asked for.
The output I want would be
Email Username
------------------------- -------------------------
a#a.com a1, a2, a3, a4
b#b.com b1, b2, b3, b4
c#c.com c1, c2, c3, c4
d#d.com d1, d2, d3, d4
Try this using FOR XML PATH
select
Email,
Username =
stuff((
select
', ' + t2.Username
from #table1 t2
where
t2.Email = t1.Email
group by t2.Username
for xml path(''), type).value('.', 'varchar(max)'
), 1, 2, '')
from #table1 t1
declare #t table (Id Varchar(10),username varchar(10))
insert into #t(Id,username)values ('a#a.com','a1'), ('a#a.com','a2'), ('a#a.com','a3'),
select DISTINCT Id,substring(
(
Select ','+t.username AS [text()]
From #t t
Where t.Id = t.Id
ORDER BY tt.Id
For XML PATH ('')
), 2, 1000)Username from #t tt
Related
I have an in-memory table as follows:
A1 B1 C1
A2 B2 C2
A3 B3 C3
How can I turn it into the table below?
A3 B3 C3
A2 B2 C2
A1 B1 C1
I have tried by adding an auto-increment field. Use keyword order by and desc to sort the records based on the new column on descending order. Then, I get the expected result after deleting this column. I wonder if there is a more convenient way to get a reversed table.
You can use the rowNo function to sort columns without adding a new auto-increment field.
t=table(`A1`A2`A3 as col1,`B1`B2`B3 as col2,`C1`C2`C3 as col3)
select * from t order by rowNo(col1) desc
Output:
col1 col2 col3
---- ---- ----
A3 B3 C3
A2 B2 C2
A1 B1 C1
I have a table with sample values as below
In this table, all the values in Col1 will have its supporting values in Col2. The values A1 and A2 are like master values and they will never appear in Col2. I need to make an output displaying this master values in a new column like below
What would be the best way to achieve this in Oracle SQL?
Looks like a hierarchical query:
SQL> select connect_by_root t.col1 as main,
2 t.col1,
3 t.col2
4 from test t
5 start with t.col1 in ('A1', 'A2')
6 connect by t.col1 = prior t.col2
7 order by main, t.col1, t.col2;
MAIN COL1 COL2
----- ----- -----
A1 A1 B1
A1 A1 B2
A1 A1 B3
A1 B1 C1
A1 B2 C2
A1 C1 D1
A2 A2 E1
A2 A2 E2
A2 E1 F1
A2 E1 F2
10 rows selected.
SQL>
Could you please help me in deleting duplicates (partial) from table? I have a table containing 5 columns. And in this table I have duplicates -- but only 4 columns are the same and one of the columns (field5) is different. That is:
F1 F2 F3 F4 F5
A1 A2 A3 A4 103
A1 A2 A3 A4 3
So, for a duplicate, 4 columns/fields are the same, except the 5th one. And I want to delete the row containing number "103", that's, a higher number. How can I achieve this?
If this was a normal duplicate, I would just use max(rowid) and remove that row. But now this could delete the row containing lower number instead of the higher number.
One method that I can think of is creating a new table containing rows which are duplicate and Field5 has a higher number from this table. Then deleting rows from original table by comparing it to this new table. But that seems not so good solution to me -- especially if the original table is big, this might take long time.
Any help would be much appreciated. Thank you.
Idea is to keep a record for each combinations of F1,F2,F3,F4 and delete the rest.
Try this:
DELETE FROM TABLE_NAME WHERE ROWID IN
(SELECT ROWID FROM
(SELECT ROWID, row_number() OVER(PARTITION BY F1,F2,F3,F4 ORDER BY F5) RN
FROM TABLE_NAME)
WHERE RN<>1);
How about this?
SQL> select * from test order by f1, f5;
F1 F2 F3 F4 F5
-- -- -- -- ----------
a1 a2 a3 a4 3
a1 a2 a3 a4 50 --> delete
a1 a2 a3 a4 103 --> delete
b1 b2 b3 b4 2
b1 b2 b3 b4 200 --> delete
c1 c2 c3 c4 1
6 rows selected.
SQL> delete from test t
2 where rowid not in (select rowid
3 from test t1
4 where t1.f1 = t.f1
5 and t1.f2 = t.f2
6 and t1.f3 = t.f3
7 and t1.f4 = t.f4
8 and t1.f5 =
9 (select min (t2.f5)
10 from test t2
11 where t2.f1 = t.f1
12 and t2.f2 = t.f2
13 and t2.f3 = t.f3
14 and t2.f4 = t.f4));
3 rows deleted.
SQL> select * from test order by f1, f5;
F1 F2 F3 F4 F5
-- -- -- -- ----------
a1 a2 a3 a4 3
b1 b2 b3 b4 2
c1 c2 c3 c4 1
SQL>
I normally just do this:
delete demo
where rowid in
( select lead(rowid) over (partition by f1, f2, f3, f4 order by f5) as next_rowid
from demo );
That is, delete every "next" row in order of f5 within its (f1, f2, f3, f4) group.
I have following primary table.
ID Email TM UR EA1 TM1 UR1 TM2 UR2 TM3 UR3
1 abc#b.com a1 b1 a#a.com a2 b2 a3 b3 a4 b4
And need output as follows.
ID Email TM UR
1 abc#b.com a1 b1
1 a#a.com a2 b2
1 a3 b3
1 a4 b4
Edit: I have already solved this by using UNION. I need optimize way to do that as I have many such columns for one record, and Union is not the solution that I'm looking for.
SELECT ID, Email, TM, UR
FROM TABLE
UNION
SELECT ID, EA1 Email, TM1 TM, UR1 UR
FROM TABLE
UNION
SELECT ID, '' Email, TM2 TM, UR2 UR
FROM TABLE
UNION
SELECT ID, '' Email, TM3 TM, UR3 UR
FROM TABLE
This question already has answers here:
join comma delimited data column
(7 answers)
Closed 8 years ago.
my table1 is :
T1
col1 col2
A1 C1,C2
A2 C3,C5,C6
A3 C4
A4 C2,C5
and so table 2:
T2
col1 col2 col3
A1 C1 reaction
A1 C2 accept
A2 C5 reaction
A2 C6 manager
A4 C2 manager
how to result this?:
query result
col1 col2
A1 reaction,accept
A2 NULL,reaction,manager
A3 NULL
A4 manager,NULL
please help me?
Never, never, never store multiple values in one column.
Like you see now this will only give you headaches. Normalize your table T1. Then you can join normally.
It should look like this
col1 col2
A1 C1
A1 C2
A2 C3
A2 C5
A2 C6
A3 C4
A4 C2
A4 C5