SELECT per.ID,Emails = JSON_VALUE(REPLACE((SELECT _ = em.Email FROM Email em WHERE em.Person = per.ID FOR JSON PATH),'"},{"_":"',', '),'$[0]._')FROM Person per
结果将成为
Id Emails1 abc@gmail.com2 NULL3 def@gmail.com, xyz@gmail.com
with lines as(selectrow_number() over(order by id) id, -- id is a line idline -- line of text.fromsource -- line source),result_lines as(selectid,cast(line as nvarchar(max)) linefromlineswhereid = 1union allselectl.id,cast(r.line + N', ' + l.line as nvarchar(max))fromlines linner joinresult_lines ronl.id = r.id + 1)select top 1linefromresult_linesorder byid desc
DECLARE @t table(Id int,Name varchar(10))INSERT INTO @tSELECT 1,'a' UNION ALLSELECT 1,'b' UNION ALLSELECT 2,'c' UNION ALLSELECT 2,'d'
SELECT ID,stuff((SELECT ','+ [Name] FROM @t WHERE Id = t.Id FOR XML PATH('')),1,1,'')FROM (SELECT DISTINCT ID FROM @t ) t
COLUMN employees FORMAT A50
SELECT deptno, LISTAGG(ename, ',') WITHIN GROUP (ORDER BY ename) AS employeesFROM empGROUP BY deptno;
DEPTNO EMPLOYEES---------- --------------------------------------------------10 CLARK,KING,MILLER20 ADAMS,FORD,JONES,SCOTT,SMITH30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
3 rows selected.
postgres=# \c testYou are now connected to database "test" as user "hgimenez".test=# create table names (name text);CREATE TABLEtest=# insert into names (name) values ('Peter'), ('Paul'), ('Mary');INSERT 0 3test=# select * from names;name-------PeterPaulMary(3 rows)
将它们聚集在一个数组中:
test=# select array_agg(name) from names;array_agg-------------------{Peter,Paul,Mary}(1 row)
将数组转换为逗号分隔的字符串:
test=# select array_to_string(array_agg(name), ', ') from names;array_to_string-------------------Peter, Paul, Mary(1 row)
create table name(first_name varchar2(30));
insert into name values ('Peter');insert into name values ('Paul');insert into name values ('Mary');
解决方案是1:
select substr(max(sys_connect_by_path (first_name, ',')),2) from (select rownum r, first_name from name ) n start with r=1 connect by prior r+1=ro/p=> Peter,Paul,Mary
SubjectID StudentName---------- -------------1 Mary1 John1 Sam2 Alaina2 Edward
那么对于NPATH,它只是一个SELECT:
SELECT * FROM npath(ON StudentsPARTITION BY SubjectIDORDER BY StudentNameMODE(nonoverlapping)PATTERN('A*')SYMBOLS('true' as A)RESULT(FIRST(SubjectID of A) as SubjectID,ACCUMULATE(StudentName of A) as StudentName));
declare @rowsCount INTdeclare @i INT = 1declare @names varchar(max) = ''
DECLARE @MyTable TABLE(Id int identity,Name varchar(500))insert into @MyTable select name from Studentsset @rowsCount = (select COUNT(Id) from @MyTable)
while @i < @rowsCountbeginset @names = @names + ', ' + (select name from @MyTable where Id = @i)set @i = @i + 1endselect @names
Declare @Numbers AS Nvarchar(MAX) -- It must not be MAX if you have few numbersSELECT @Numbers = COALESCE(@Numbers + ',', '') + NumberFROM TableName where Number IS NOT NULL
SELECT @Numbers
SELECT STUFF((SELECT ', ' + name FROM [table] FOR XML PATH('')), 1, 2, '')
以下是一个示例:
DECLARE @t TABLE (name VARCHAR(10))INSERT INTO @t VALUES ('Peter'), ('Paul'), ('Mary')SELECT STUFF((SELECT ', ' + name FROM @t FOR XML PATH('')), 1, 2, '')--Peter, Paul, Mary
INSERT INTO NAMES VALUES('PETER');INSERT INTO NAMES VALUES('PAUL');INSERT INTO NAMES VALUES('MARY');
程序从这里开始
DECLARE
MAXNUM INTEGER;CNTR INTEGER := 1;C_NAME NAMES.NAME%TYPE;NSTR VARCHAR2(50);
BEGIN
SELECT MAX(ROWNUM) INTO MAXNUM FROM NAMES;
LOOP
SELECT NAME INTO C_NAME FROM(SELECT ROWNUM RW, NAME FROM NAMES ) P WHERE P.RW = CNTR;
NSTR := NSTR ||','||C_NAME;CNTR := CNTR + 1;EXIT WHEN CNTR > MAXNUM;
END LOOP;
dbms_output.put_line(SUBSTR(NSTR,2));
END;
-- Create example tableCREATE TABLE tmptable (NAME VARCHAR(30)) ;
-- Insert example dataINSERT INTO tmptable VALUES('PETER');INSERT INTO tmptable VALUES('PAUL');INSERT INTO tmptable VALUES('MARY');
-- Recurse querywith tblwithrank as (select * , row_number() over(order by name) rang , count(*) over() NbRowfrom tmptable),tmpRecursive as (select *, cast(name as varchar(2000)) as AllName from tblwithrank where rang=1union allselect f0.*, cast(f0.name + ',' + f1.AllName as varchar(2000)) as AllNamefrom tblwithrank f0 inner join tmpRecursive f1 on f0.rang=f1.rang +1)select AllName from tmpRecursivewhere rang=NbRow
declare @mytable as table(id int identity(1,1), str nvarchar(100))insert into @mytable values('Peter'),('Paul'),('Mary')
declare @myresult as table(id int,str nvarchar(max),ind int, R# int)
;with cte as(select id,cast(str as nvarchar(100)) as str, cast(0 as int) ind from @mytableunion allselect t2.id,cast(t1.str+',' +t2.str as nvarchar(100)) ,t1.ind+1 from cte t1 inner join @mytable t2 on t2.id=t1.id+1)insert into @myresult select *,row_number() over(order by ind) R# from cte
select top 1 str from @myresult order by R# desc