Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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 server 将多行合并为一行_Sql Server_Tsql_Sql Server 2000_Pivot - Fatal编程技术网

Sql server 将多行合并为一行

Sql server 将多行合并为一行,sql-server,tsql,sql-server-2000,pivot,Sql Server,Tsql,Sql Server 2000,Pivot,我有一个包含用户帐户权限的表,我正试图编写一个查询,为每个用户帐户组合返回一行 这是我的 CltKey AcctKey TranTypeID Access 10 2499 10 0 10 2499 11 1 10 2499 12 1 10 2764 10 1 10 2764 11 1 10 2764 12

我有一个包含用户帐户权限的表,我正试图编写一个查询,为每个用户帐户组合返回一行

这是我的

CltKey  AcctKey TranTypeID  Access
10      2499    10          0
10      2499    11          1
10      2499    12          1
10      2764    10          1
10      2764    11          1
10      2764    12          0
这是我想要的

CltKey  AcctKey TranTypeID1 Access1 TranTypeID2 Access2 TranTypeID3 Access3
10      2499    10          0       11        1       12        1
10      2764    10          1       11        1       12        0
或者像这样更好

CltKey  AcctKey HasTranTypeID1  HasTranTypeID2 HasTranTypeID3
10      2499    0               1              1
10      2764    1               1              0    
我已经尝试过进行自连接,但我一直为每个TranTypeID获取多行。一个等于0,另一个等于1。我也尝试过使用嵌套的“Select”语句,但性能非常糟糕。有人知道怎么做吗

谢谢


编辑:不幸的是,这必须在SQL 2000中起作用。

我已经有一段时间没有使用SQL Server 2000了,但这可能会起作用

select cltkey, acctkey, 
max( case when trantypeid = 10 and access = 1 
      then 1 else 0 end ) as hastrantypeid1,
max( case when trantypeid = 11 and access = 1 
      then 1 else 0 end ) as hastrantypeid2,
max( case when trantypeid = 12 and access = 1 
      then 1 else 0 end ) as hastrantypeid3
from table
group by cltkey, acctkey;
如果没有,请尝试以下操作:

create view has_access as 
select cltkey, acctkey, 
max( case when trantypeid = 10 and access = 1 
      then 1 else 0 end ) as hastrantypeid1,
max( case when trantypeid = 11 and access = 1 
      then 1 else 0 end ) as hastrantypeid2,
max( case when trantypeid = 12 and access = 1 
      then 1 else 0 end ) as hastrantypeid3
from table;
然后从中得到你的结果

select cltkey, acctkey, 
max( hastrantypeid1) as hastrantypeid1,
max( hastrantypeid2 ) as hastrantypeid2,
max( hastrantypeid2 ) as hastrantypeid2
from has_access
group by cltkey, acctkey;
请注意,如果(cltkey,acctkey)元组的任何行对该特定类型具有访问权限,则这将告诉您(cltkey,acctkey)具有(特定类型的)访问权限。也就是说,它本质上是一个按行的

如果该元组的所有行都必须具有访问权限才能使该元组具有访问权限,也就是说,如果需要按行执行
,则需要执行以下操作:

min( case when trantypeid = 10 
      then case when access = 1 
            then 1 else 0 end else null end) as hastrantypeid1,
etc.

使用PIVOT-以下是一个示例:

用于哪个版本的SQL Server?2005+具有
PIVOT
语法。但它不是动态的。。。
SELECT CltKey, AcctKey,
    MAX(CASE TrantypeId WHEN 10 THEN Access ELSE NULL END) AS HasTranTypeID1,
    MAX(CASE TrantypeId WHEN 11 THEN Access ELSE NULL END) AS HasTranTypeID2,
    MAX(CASE TrantypeId WHEN 12 THEN Access ELSE NULL END) AS HasTranTypeID3
FROM PermissionsTable
GROUP BY CltKey, AcctKey
ORDER BY CltKey, AcctKey
;