SQL-将一个表中的所有用户合并,不包含不规则的空值

时间:2019-03-06 10:30:12

标签: sql sql-server

我希望将空值放在列表的末尾,但是将它们放在名称之间。

KullaniciAdi      KisiAdi
------------      ------------
AHMET GÜNDÜZ      " Ned Hsoleb(Guzel Alli) " --+
AHMET GÜNDÜZ      Raghuvar Masala              |
.                 .                            |
.                 .                            | AHMET GÜNDÜZ Has 10 Friends
.                 .                            |
.                 .                            |
AHMET GÜNDÜZ      Mint Agro -------------------+
ALİ AKKUM         Mustafa Saidmurodov -----+
ALİ AKKUM         Tarimkoop Ihsangazi      |
.                 .                        |
.                 .                        | ALİ AKKUM Has 9 Friends
.                 .                        |
.                 .                        |
ALİ AKKUM         Ali Uçar  ---------------+
YASEMİN AYSEN     Pepinier El Fertas --+
.                 .                    |
.                 .                    | YASEMİN AYSEN Has 6 Friends
.                 .                    |
YASEMİN AYSEN     Sarker Tredres ------+

这是我的查询联合-所有用户,但具有不规则的null值;

DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX); 
SET @cols = STUFF((SELECT distinct ',MAX(CASE WHEN KullaniciAdi = '''  + p.KullaniciAdi + ''' THEN KisiAdi END) AS '  + QUOTENAME(p.KullaniciAdi) FROM Populer p FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)')  ,1,1,'') 
set @query = 'SELECT ' + @cols + ' FROM  (   SELECT KullaniciAdi,KisiAdi,ROW_NUMBER() OVER (PARTITION BY KullaniciAdi ORDER BY EklenmeTarihi) AS RowNum FROM Populer ) x GROUP BY RowNum ' 
EXECUTE(@query)

My Query Result:

但是,我想要作为;

AHMET GÜNDÜZ                    ALİ AKKUM             YASEMİN AYSEN
------------                    ------------          -------------
" Ned Hsoleb(Guzel Alli) "      Mustafa Saidmurodov   Pepinier El Fertas
Raghuvar Masala                 Tarimkoop Ihsangazi   .
.                               .                     . 
.                               .                     .
.                               .                     .
.                               .                     Sarker Tredres
.                               .                     NULL
.                               .                     NULL
.                               Ali Uçar              NULL
Mint Agro                       NULL                  NULL

我无法编辑查询。您能帮我吗?

3 个答案:

答案 0 :(得分:1)

您可以使用ROW_NUMBER()获得结果。以下SQL将在最后显示NULL值。我刚刚用ROW_NUMBER()修改了您的SQL。

DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX); 
CREATE TABLE #tempPopuler (KullaniciAdi VARCHAR(50), KisiAdi VARCHAR(50))
INSERT INTO #tempPopuler VALUES('AHMET', 'Ned Hsoleb')
INSERT INTO #tempPopuler VALUES('AHMET', 'Raghuvar')
INSERT INTO #tempPopuler VALUES('AHMET', 'Mint Agro')
INSERT INTO #tempPopuler VALUES('ALI', 'Mustafa')
INSERT INTO #tempPopuler VALUES('ALI', 'Tarimkoop')
INSERT INTO #tempPopuler VALUES('YASEMIN', 'Pepinier')
INSERT INTO #tempPopuler VALUES('AHMET', 'A')
INSERT INTO #tempPopuler VALUES('AHMET', 'B')
INSERT INTO #tempPopuler VALUES('AHMET', 'C')
INSERT INTO #tempPopuler VALUES('ALI', 'A')
INSERT INTO #tempPopuler VALUES('ALI', 'B')
INSERT INTO #tempPopuler VALUES('YASEMIN', 'A')
INSERT INTO #tempPopuler VALUES('YASEMIN', 'B')

SET @cols = STUFF((SELECT distinct ',(SELECT KisiAdi FROM (SELECT ROW_NUMBER() OVER (ORDER BY KisiAdi ASC) AS rownumber, KullaniciAdi, KisiAdi FROM [#tempPopuler] where KullaniciAdi = '''  + p.KullaniciAdi + ''') AS foo WHERE rownumber = x.ROW and  KullaniciAdi ='''  + p.KullaniciAdi + ''') AS'  + QUOTENAME(p.KullaniciAdi) FROM #tempPopuler p FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)')  ,1,1,'') 
set @query = 'SELECT ' + @cols + ' FROM  (SELECT KullaniciAdi,KisiAdi, ROW_NUMBER() OVER(ORDER BY KisiAdi ASC) AS [ROW] FROM #tempPopuler) x' 

EXECUTE(@query)

DROP TABLE #tempPopuler 

输出

enter image description here

答案 1 :(得分:0)

在group by语句之后,您需要添加一个order by子句,以便将空值推到末尾。

ORDER BY 
ISNULL(AHMET GÜNDÜZ ,'zzzzz'),ISNULL(ALİ AKKUM  ,'zzzzz'),ISNULL(YASEMİN AYSEN,'zzzzz')

此查询将把所有非空值放在最前面,并将空值推到最后一行。试试吧。

DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX), @ordercols as NVARCHAR(MAX); 
SET @ordercols = STUFF((SELECT distinct ',case when isnull([' + p.KullaniciAdi + '],''0'')=''0'' then ''0'' else ''1'' end ' FROM Populer p FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)')  ,1,1,'') 
SET @cols = STUFF((SELECT distinct ',MAX(CASE WHEN KullaniciAdi = '''  + p.KullaniciAdi + ''' THEN KisiAdi END) AS '  + QUOTENAME(p.KullaniciAdi) FROM Populer p FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)')  ,1,1,'') 
set @query = 'SELECT * from(SELECT ' + @cols + ' FROM  (   SELECT KullaniciAdi,KisiAdi,ROW_NUMBER() OVER (PARTITION BY KullaniciAdi ORDER BY EklenmeTarihi) AS RowNum FROM Populer ) x GROUP BY RowNum)a order by concat(  ' +@ordercols+') desc'
EXECUTE(@query)

答案 2 :(得分:0)

您必须将以下代码添加到运算符 order by case when KisiAdi is null then 0 else 1 end desc

    DECLARE 
        @cols AS NVARCHAR(MAX), 
        @query AS NVARCHAR(MAX); 

    SET @cols = STUFF((
        SELECT distinct ', MAX(CASE WHEN KullaniciAdi = '''  + 
            p.KullaniciAdi + ''' THEN KisiAdi END) AS '  + 
            QUOTENAME(p.KullaniciAdi) 
        FROM Populer p FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)'), 1, 1,'');

    SET @query = 
        'SELECT ' + @cols + 
        ' FROM  (   
            SELECT KullaniciAdi, KisiAdi, 
                ROW_NUMBER() OVER 
                (PARTITION BY KullaniciAdi ORDER BY case when KisiAdi is null then 0 else 1 end desc, EklenmeTarihi) AS RowNum 
            FROM Populer ) x 
        GROUP BY RowNum '; 

    EXECUTE(@query);