Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在一个表中显示来自不同诱惑的值_Sql_Sql Server - Fatal编程技术网

Sql 在一个表中显示来自不同诱惑的值

Sql 在一个表中显示来自不同诱惑的值,sql,sql-server,Sql,Sql Server,我的sql表如下所示:- CREATE TABLE #TmpA ( Type1 Varchar(10), Col1 VARCHAR(10), Request INT, Due INT ); CREATE TABLE #TmpB ( Type1 Varchar(10), Col1 VARCHAR(10), Request INT, Due INT ); CREATE TABLE #TmpC ( Type1 Varchar(10), Col1 VARCHAR(10),

我的sql表如下所示:-

 CREATE TABLE #TmpA (
 Type1 Varchar(10),
 Col1 VARCHAR(10),
 Request INT,
 Due INT
  );

CREATE TABLE #TmpB (
 Type1 Varchar(10),
 Col1 VARCHAR(10),
 Request INT,
 Due INT );

CREATE TABLE #TmpC (
 Type1 Varchar(10),
 Col1 VARCHAR(10),
 Request INT,
 Due INT );

 INSERT INTO #TmpA VALUES('P', 'Name1',0,278),('P', 'Name2',10,89),('R', 'Name3',5,89)

 INSERT INTO #TmpB VALUES ('P', 'Name1',0,10),('P', 'Name2',1,78),('A', 'Name4',4,289 )

 INSERT INTO #TmpC VALUES ('P', 'Name1',54,67),('P', 'Name5',5,47),('A', 'Name6',3,90 )

SELECT * FROM #TmpA
SELECT * FROM #TmpB
SELECT * FROM #TmpC
我想将我所有的表合并到一个表中,因为我又创建了一个表#TmpD。我想以以下格式显示:-


#TmpD列需要是动态的,在本例中我取了3个,但也可以多于3个或少于3个。

一种方法使用
联合所有
和聚合:

SELECT  T.Col1, 
        ISNULL(A.Request,0) AS Request1,
        ISNULL(A.Due,0) AS Due1,
        ISNULL(B.Request,0) AS Request2,
        ISNULL(B.Due,0) AS Due2,
        ISNULL(C.Request,0) AS Request3,
        ISNULL(C.Due,0) AS Due3
FROM 
(
    SELECT Col1
    FROM #TmpA
    UNION 
    SELECT Col1
    FROM #TmpB
    UNION
    SELECT Col1
    FROM #TmpC
)T
LEFT JOIN #TmpA A
    ON A.Col1=T.Col1
LEFT JOIN #TmpB B
    ON B.Col1=T.Col1
LEFT JOIN #TmpC C
    ON C.Col1=T.Col1
select type1, col1,
       max(request1) as request1, max(due1) as due1,
       max(request2) as request2, max(due2) as due2,
       max(request3) as request3, max(due3) as due3
from ((select type1, col1,
              request as request1, due as due1,
              0 as request2, 0 as due2,
              0 as request3, 0 as due3
       from #tmpa
      ) union all
      (select type1, col1,
              0 as request1, 0 as due1,
              request as request2, due as due2,
              0 as request3, 0 as due3
       from #tmpb
      ) union all
      (select type1, col1,
              0 as request1, 0 as due1,
              0 as request2, 0 as due2,
              request as request3, due as due3
       from #tmpc
      )
     ) abc
group by type1, col1;
另一种方法是
完全连接
,但这可能很棘手:

select a.type1, a.col1,
       coalesce(a.request, 0) as request1, coalesce(a.due, 0) as due1,
       coalesce(b.request, 0) as request2, coalesce(b.due, 0) as due2,
       coalesce(c.request, 0) as request3, coalesce(c.due, 0) as due3
from #tmpA a full join
     #tmpB b
     on b.type1 = a.type1 and b.col1 = a.col1 full join
     #tmpC c
     on c.type1 = coalesce(a.type1, b.type1) and
        c.col1 = coalesce(a.col1, b.col1);

使用动态轴的另一种方法:

---- create new table #tmpD by using select ... into
select *
into #tmpD
from  
    (      
        select *, 1 as reqNr  from #TmpA union all
        select *, 2  from #tmpB union all
        select *, 3  from #tmpc 
    ) t

declare @cols_req as nvarchar(max)
     ,  @cols_req_max as nvarchar(max)
     ,  @cols_due as nvarchar(max)
     ,  @cols_due_max as nvarchar(max)
     ,  @query  as nvarchar(max)

select @cols_req = stuff((select distinct ',' + quotename('Request'+ cast(row_number() over(partition by col1, type1 order by type1, col1) as varchar(10))) from #tmpD for xml path(''), type ).value('.', 'nvarchar(max)') ,1,1,'')
select @cols_req_max = stuff((select distinct ',' + ('max(Request'+ cast(row_number() over(partition by col1, type1 order by type1, col1) as varchar(10)) +') as Request' + cast(row_number() over(partition by col1, type1 order by col1, type1) as varchar(10))) from #tmpD for xml path(''), type ).value('.', 'nvarchar(max)') ,1,1,'')
select @cols_due = stuff((select distinct ',' + quotename('Due'+ cast(row_number() over(partition by col1, type1 order by type1, col1) as varchar(10))) from #tmpD for xml path(''), type ).value('.', 'nvarchar(max)') ,1,1,'')
select @cols_due_max = stuff((select distinct ',' + ('max(Due'+ cast(row_number() over(partition by col1, type1 order by type1, col1) as varchar(10)) +') as Due' + cast(row_number() over(partition by col1, type1 order by col1, type1) as varchar(10))) from #tmpD for xml path(''), type ).value('.', 'nvarchar(max)') ,1,1,'')

set @query = '    select Type1, Col1, isnull(Request1, 0) Request1, isnull(Due1, 0) Duel1, isnull(Request2, 0) Request2, isnull(Due2, 0) Due2, isnull(Request3, 0) Request3, isnull(Due3, 0) Due3 
                  from (
                          select Type1, Col1, ' + @cols_req_max + ', ' + @cols_due_max + '
                          from 
                                (
                                    select  Type1
                                          , Col1
                                          , Request
                                          , Due
                                          , col_req = ''Request''+ cast(reqNR as varchar(10))
                                          , col_due = ''Due''+ cast(reqNR as varchar(10))
                                    from #tmpD 
                                ) x
                          pivot ( max(request) for col_req in (' + @cols_req + ') )p  
                          pivot ( max(due) for col_due in (' + @cols_due + ') ) q
                          group by Type1, Col1
                       ) t
              '
print @query
execute sp_executesql @query;
我最初创建了一个新的临时表
(#tmpD)
,其中插入了所有三个初始表中的行,以及一个名为
'reqNr'
的额外列,该列显示了哪个表是源表

对于有3个以上表的情况,只需调整将所有行插入
#tmpD
表中的初始语句,以包括其他表


您可以查看正在运行的演示。

您可以使用join将表合并为一个表。我如何使用dynamic columnsHow创建临时表。您是否为Name6定义了该临时表,请求应该在Request3中,而不是Request1中?因为它在#TmpC中,所以它应该在Request3中。您是否使用了任何模型类,也使用了数据库优先或代码优先的方法?如果您首先使用代码,那么您可以从代码中创建查询,然后为回复执行QueryTank,但我需要动态列,可以有3个以上的表,最多12个,也可以少于3个表,我举了三个例子作为例子。@表和列名应考虑如下:代码>tMPa类型1,COL1请求,由于TMPB类型2,COL2请求(到期)< /代码>不,它应该是tTMPa类型1 COL1请求(到期)TMPB类型1 COL1请求DuESIR,谢谢回答,但是如果表的数目小于3或大于3,我以3为例,我需要它dynamic@RahulAggarwal . . . 那么你应该问另一个问题。这个问题很明显是关于三张桌子的,人们已经想方设法回答了。如果您更改此问题,您将使答案无效,这可能会导致向下投票。我如何使“select Type1、Col1、isnull(Request1、0)Request1、isnull(Due1、0)Duel1、isnull(Request2、0)Request2、isnull(Due2、0)Due2、isnull(Request3、0)Request3、isnull(Due3、0)Due3”动态化,因为可以有3个以上的表或少于3个表,这是一个小缺点-您需要键入这一行..因为在填充临时表时,您需要在union中键入这些额外的表。