Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.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_Join - Fatal编程技术网

简化多连接SQL列

简化多连接SQL列,sql,sql-server,join,Sql,Sql Server,Join,我已经使用join STATTENT选择了select STATTENT查询,我使用它从连接表中获取值 以下是我的选择查询: select case when p1.Value2 is not null then p1.Value3 end as fix1, case when p1.Value2 is not null then p2.Value3 end as fix2, case when p1.Value2 is not null then p3.Value3 end as fix3,

我已经使用join STATTENT选择了select STATTENT查询,我使用它从连接表中获取值

以下是我的选择查询:

select 
case when p1.Value2 is not null then p1.Value3 end as fix1,
case when p1.Value2 is not null then p2.Value3 end as fix2,
case when p1.Value2 is not null then p3.Value3 end as fix3,
case when p1.Value2 is not null then p4.Value3 end as fix4,
case when p1.Value2 is not null then p5.Value3 end as fix5,
case when p1.Value2 is not null then p6.Value3 end as fix6
from tc t
left join (select * from  Parameter where paramtype='fix') as p1 on p1.Value2 = t.FIX 
left join (select * from  Parameter where paramtype='fix') as p2 on p2.Value2 = t.FIX1
left join (select * from  Parameter where paramtype='fix') as p3 on p3.Value2 = t.FIX2
left join (select * from  Parameter where paramtype='fix') as p4 on p4.Value2 = t.FIX3
left join (select * from  Parameter where paramtype='fix') as p5 on p5.Value2 = t.FIX4
left join (select * from  Parameter where paramtype='fix') as p6 on p6.Value2 = t.FIX5
where CUST_ACCT ='10053996'
select 
IsNull(p1.Value2 ,p1.Value3) as fix1,
IsNull(p1.Value2 ,p1.Value3) as fix2,
IsNull(p1.Value2 ,p1.Value3) as fix3,
IsNull(p1.Value2 ,p1.Value3) as fix4,
IsNull(p1.Value2 ,p1.Value3) as fix5,
IsNull(p1.Value2 ,p1.Value3) as fix6,
from tc t
left join (select * from  Parameter where paramtype='fix') as p1 on p1.Value2 = t.FIX 
left join (select * from  Parameter where paramtype='fix') as p2 on p2.Value2 = t.FIX1
left join (select * from  Parameter where paramtype='fix') as p3 on p3.Value2 = t.FIX2
left join (select * from  Parameter where paramtype='fix') as p4 on p4.Value2 = t.FIX3
left join (select * from  Parameter where paramtype='fix') as p5 on p5.Value2 = t.FIX4
left join (select * from  Parameter where paramtype='fix') as p6 on p6.Value2 = t.FIX5
where CUST_ACCT ='10053996'
我认为这太复杂了,所以我修改代码如下:

select 
fix +' '+(select Value3 from Parameter where ParamType = 'fix' and Value2 = fix) as fix,
FIX1 +' '+(select Value3 from Parameter where ParamType = 'fix' and Value2 = FIX1) as FIX1,
FIX2 +' '+(select Value3 from Parameter where ParamType = 'fix' and Value2 = FIX2) as FIX2,
FIX3 +' '+(select Value3 from Parameter where ParamType = 'fix' and Value2 = FIX3) as FIX3,
FIX4 +' '+(select Value3 from Parameter where ParamType = 'fix' and Value2 = FIX4) as FIX4,
FIX5 +' '+(select Value3 from Parameter where ParamType = 'fix' and Value2 = FIX5) as FIX5
from tc where CUST_ACCT ='10053996'
可以简化我的代码吗?或者如果我想做这样的事情,我应该创建这样的select语句?

您可以在第一个语句中用IsNullexpression、valueIfNull替换Case语句

我已经使用join STATTENT选择了select STATTENT查询,我使用它从连接表中获取值

以下是我的选择查询:

select 
case when p1.Value2 is not null then p1.Value3 end as fix1,
case when p1.Value2 is not null then p2.Value3 end as fix2,
case when p1.Value2 is not null then p3.Value3 end as fix3,
case when p1.Value2 is not null then p4.Value3 end as fix4,
case when p1.Value2 is not null then p5.Value3 end as fix5,
case when p1.Value2 is not null then p6.Value3 end as fix6
from tc t
left join (select * from  Parameter where paramtype='fix') as p1 on p1.Value2 = t.FIX 
left join (select * from  Parameter where paramtype='fix') as p2 on p2.Value2 = t.FIX1
left join (select * from  Parameter where paramtype='fix') as p3 on p3.Value2 = t.FIX2
left join (select * from  Parameter where paramtype='fix') as p4 on p4.Value2 = t.FIX3
left join (select * from  Parameter where paramtype='fix') as p5 on p5.Value2 = t.FIX4
left join (select * from  Parameter where paramtype='fix') as p6 on p6.Value2 = t.FIX5
where CUST_ACCT ='10053996'
select 
IsNull(p1.Value2 ,p1.Value3) as fix1,
IsNull(p1.Value2 ,p1.Value3) as fix2,
IsNull(p1.Value2 ,p1.Value3) as fix3,
IsNull(p1.Value2 ,p1.Value3) as fix4,
IsNull(p1.Value2 ,p1.Value3) as fix5,
IsNull(p1.Value2 ,p1.Value3) as fix6,
from tc t
left join (select * from  Parameter where paramtype='fix') as p1 on p1.Value2 = t.FIX 
left join (select * from  Parameter where paramtype='fix') as p2 on p2.Value2 = t.FIX1
left join (select * from  Parameter where paramtype='fix') as p3 on p3.Value2 = t.FIX2
left join (select * from  Parameter where paramtype='fix') as p4 on p4.Value2 = t.FIX3
left join (select * from  Parameter where paramtype='fix') as p5 on p5.Value2 = t.FIX4
left join (select * from  Parameter where paramtype='fix') as p6 on p6.Value2 = t.FIX5
where CUST_ACCT ='10053996'
您可以在第一条语句中用IsNullexpression、valueIfNull替换Case语句

我已经使用join STATTENT选择了select STATTENT查询,我使用它从连接表中获取值

以下是我的选择查询:

select 
case when p1.Value2 is not null then p1.Value3 end as fix1,
case when p1.Value2 is not null then p2.Value3 end as fix2,
case when p1.Value2 is not null then p3.Value3 end as fix3,
case when p1.Value2 is not null then p4.Value3 end as fix4,
case when p1.Value2 is not null then p5.Value3 end as fix5,
case when p1.Value2 is not null then p6.Value3 end as fix6
from tc t
left join (select * from  Parameter where paramtype='fix') as p1 on p1.Value2 = t.FIX 
left join (select * from  Parameter where paramtype='fix') as p2 on p2.Value2 = t.FIX1
left join (select * from  Parameter where paramtype='fix') as p3 on p3.Value2 = t.FIX2
left join (select * from  Parameter where paramtype='fix') as p4 on p4.Value2 = t.FIX3
left join (select * from  Parameter where paramtype='fix') as p5 on p5.Value2 = t.FIX4
left join (select * from  Parameter where paramtype='fix') as p6 on p6.Value2 = t.FIX5
where CUST_ACCT ='10053996'
select 
IsNull(p1.Value2 ,p1.Value3) as fix1,
IsNull(p1.Value2 ,p1.Value3) as fix2,
IsNull(p1.Value2 ,p1.Value3) as fix3,
IsNull(p1.Value2 ,p1.Value3) as fix4,
IsNull(p1.Value2 ,p1.Value3) as fix5,
IsNull(p1.Value2 ,p1.Value3) as fix6,
from tc t
left join (select * from  Parameter where paramtype='fix') as p1 on p1.Value2 = t.FIX 
left join (select * from  Parameter where paramtype='fix') as p2 on p2.Value2 = t.FIX1
left join (select * from  Parameter where paramtype='fix') as p3 on p3.Value2 = t.FIX2
left join (select * from  Parameter where paramtype='fix') as p4 on p4.Value2 = t.FIX3
left join (select * from  Parameter where paramtype='fix') as p5 on p5.Value2 = t.FIX4
left join (select * from  Parameter where paramtype='fix') as p6 on p6.Value2 = t.FIX5
where CUST_ACCT ='10053996'

这样的解决方案怎么样

SELECT /*extra fields from tc ==>>*/ x, y /*<<==*/
    , FIX, FIX1, FIX2, FIX3, FIX4, FIX5
FROM (
    SELECT fields, P.Value3, /*extra fields from tc ==>>*/ x, y /*<<==*/
    FROM (
        SELECT *
        FROM tc
        WHERE CUST_ACCT ='10053996'
    ) T
    UNPIVOT (
        unpvt FOR fields in (FIX,FIX1,FIX2,FIX3,FIX4,FIX5)
    ) UPV
    LEFT JOIN Parameter P ON P.paramtype='fix' AND P.Value2 = UPV.unpvt
) T
PIVOT (
    MIN(Value3) FOR fields in (FIX,FIX1,FIX2,FIX3,FIX4,FIX5)
) AS PV

这样的解决方案怎么样

SELECT /*extra fields from tc ==>>*/ x, y /*<<==*/
    , FIX, FIX1, FIX2, FIX3, FIX4, FIX5
FROM (
    SELECT fields, P.Value3, /*extra fields from tc ==>>*/ x, y /*<<==*/
    FROM (
        SELECT *
        FROM tc
        WHERE CUST_ACCT ='10053996'
    ) T
    UNPIVOT (
        unpvt FOR fields in (FIX,FIX1,FIX2,FIX3,FIX4,FIX5)
    ) UPV
    LEFT JOIN Parameter P ON P.paramtype='fix' AND P.Value2 = UPV.unpvt
) T
PIVOT (
    MIN(Value3) FOR fields in (FIX,FIX1,FIX2,FIX3,FIX4,FIX5)
) AS PV

您肯定不需要多次加入同一个表。以下是避免它的简单技巧:

select 
case when pt.f = 1 then pt.Value3 end as fix1,
case when pt.f2 = 1 then pt.Value3 end as fix2,
... 
from tc
left join (select 
      CASE WHEN p.Value2 = t.FIX THEN 1 ELSE 0 END AS f, 
      CASE WHEN p.Value2 = t.FIX1 THEN 1 ELSE 0 END AS f1, 
      CASE WHEN p.Value2 = t.FIX2 THEN 1 ELSE 0 END AS f2, 
      CASE WHEN p.Value2 = t.FIX3 THEN 1 ELSE 0 END AS f3, 
      CASE WHEN p.Value2 = t.FIX4 THEN 1 ELSE 0 END AS f4, 
      CASE WHEN p.Value2 = t.FIX5 THEN 1 ELSE 0 END AS f5
    from  Parameter AS p where paramtype = 'fix') AS pt
    ON pt.Value2 IN (tc.FIX, tc.FIX2, tc.FIX3, tc.FIX4, tc.FIX5)
where CUST_ACCT ='10053996'

也许可以根据具体情况简化联接条件。其思想是引入参数而不是多个联接。

您肯定不需要多次联接同一个表。以下是避免它的简单技巧:

select 
case when pt.f = 1 then pt.Value3 end as fix1,
case when pt.f2 = 1 then pt.Value3 end as fix2,
... 
from tc
left join (select 
      CASE WHEN p.Value2 = t.FIX THEN 1 ELSE 0 END AS f, 
      CASE WHEN p.Value2 = t.FIX1 THEN 1 ELSE 0 END AS f1, 
      CASE WHEN p.Value2 = t.FIX2 THEN 1 ELSE 0 END AS f2, 
      CASE WHEN p.Value2 = t.FIX3 THEN 1 ELSE 0 END AS f3, 
      CASE WHEN p.Value2 = t.FIX4 THEN 1 ELSE 0 END AS f4, 
      CASE WHEN p.Value2 = t.FIX5 THEN 1 ELSE 0 END AS f5
    from  Parameter AS p where paramtype = 'fix') AS pt
    ON pt.Value2 IN (tc.FIX, tc.FIX2, tc.FIX3, tc.FIX4, tc.FIX5)
where CUST_ACCT ='10053996'
也许可以根据具体情况简化联接条件。想法是引入参数,而不是多个联接。

如何:

select
case when p1.Value2 is not null then p1.Value3 end as fix1,
case when p1.Value2 is not null then p2.Value3 end as fix2, 
case when p1.Value2 is not null then p3.Value3 end as fix3, 
case when p1.Value2 is not null then p4.Value3 end as fix4, 
case when p1.Value2 is not null then p5.Value3 end as fix5, 
case when p1.Value2 is not null then p6.Value3 end as fix6 
from tc t 
left join Parameter as p1 
on p1.Value2 = t.FIX  AND p1.paramtype='fix'  
left join Parameter as p2 
on p2.Value2 = t.FIX1 AND p2.paramtype='fix'  
left join Parameter as p3
on p3.Value2 = t.FIX2 AND p3.paramtype='fix' 
left join Parameter as p4
on p4.Value2 = t.FIX3 AND p4.paramtype='fix' 
left join Parameter as p5
on p5.Value2 = t.FIX4 AND p5.paramtype='fix' 
left join Parameter as p6
on p6.Value2 = t.FIX5 AND p6.paramtype='fix' 
where CUST_ACCT ='10053996'
那么:

select
case when p1.Value2 is not null then p1.Value3 end as fix1,
case when p1.Value2 is not null then p2.Value3 end as fix2, 
case when p1.Value2 is not null then p3.Value3 end as fix3, 
case when p1.Value2 is not null then p4.Value3 end as fix4, 
case when p1.Value2 is not null then p5.Value3 end as fix5, 
case when p1.Value2 is not null then p6.Value3 end as fix6 
from tc t 
left join Parameter as p1 
on p1.Value2 = t.FIX  AND p1.paramtype='fix'  
left join Parameter as p2 
on p2.Value2 = t.FIX1 AND p2.paramtype='fix'  
left join Parameter as p3
on p3.Value2 = t.FIX2 AND p3.paramtype='fix' 
left join Parameter as p4
on p4.Value2 = t.FIX3 AND p4.paramtype='fix' 
left join Parameter as p5
on p5.Value2 = t.FIX4 AND p5.paramtype='fix' 
left join Parameter as p6
on p6.Value2 = t.FIX5 AND p6.paramtype='fix' 
where CUST_ACCT ='10053996'

啊哈,它看起来更简单,但它仍然需要6个左连接语句吗?是的,但是连接比子查询好。select语句中的子查询针对每一行执行,其中,当join查询针对onesyes执行时,我尝试了一些场景,我发现子查询可能会出错,因为如果它找到的值超过1,当p1.Value2不为null时,它将抛出一个errorcase,然后p1.Value3 end和IsNullp1.Value2、p1.Value3给出完全不同的结果。在第二种情况下,如果p1.Value2为null,您将得到p1.Value3,而在第一种情况下,我们有相反的条件。而且,第一个子句永远不会返回p1.Value2,但是第二个子句会返回p1.Value2。啊哈,看起来更简单,但它仍然需要6个左join语句吗?是的,但是join比sub query好。select语句中的子查询针对每一行执行,其中,当join查询针对onesyes执行时,我尝试了一些场景,我发现子查询可能会出错,因为如果它找到的值超过1,当p1.Value2不为null时,它将抛出一个errorcase,然后p1.Value3 end和IsNullp1.Value2、p1.Value3给出完全不同的结果。在第二种情况下,如果p1.Value2为null,您将得到p1.Value3,而在第一种情况下,我们有相反的条件。此外,第一个子句永远不会返回p1.Value2,但第二个子句会返回p1.Value2。您如何尝试解释您试图通过查询实现的目标?实际上,我想要更简单有效的代码,我还想避免子查询的错误。你如何解释你试图通过查询实现的目标?实际上,我想要更简单有效的代码,我还想避免子查询的错误。我只是好奇,如何用这段代码显示所有TC字段?用pivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivo?用pivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot things unpivot unpivot unpivot unpivot unpivot unpivot unpivot unpivot,unpivot unpivot unpivot unpivot unpivot