Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.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查询联接并将中的重复列值设置为null_Sql_Sql Server - Fatal编程技术网

SQl查询联接并将中的重复列值设置为null

SQl查询联接并将中的重复列值设置为null,sql,sql-server,Sql,Sql Server,我想在sql查询中加入2个表,然后在结果中加入 重复列单元格其中一个重复项变为空。 我的第一张桌子是: id corp_code pay_authority_no authority_price status 1 C286 210995 85020000 True 2 C286 210879 61040000 True 3 C139 21247

我想在sql查询中加入2个表,然后在结果中加入 重复列单元格其中一个重复项变为空。 我的第一张桌子是:

id  corp_code   pay_authority_no    authority_price status
1    C286           210995              85020000    True
2    C286           210879              61040000    True
3    C139           212475              77708280    True
4    C139           212465              77878320    True
5    C296           216177              101335000   True
13   C321           214526              56680000    True
id  pay_authority_no    order_kind  order_no
2         210879         Reorder    84182
1         210995         Reorder    83251
4         212465         Sup        459950
3         212475         Sup        459948
15        212475         Sup        65878
13        214526         Reorder    86019
14        214526         Reorder    86020
5         216177         Reorder    83715
第二个表是:

id  corp_code   pay_authority_no    authority_price status
1    C286           210995              85020000    True
2    C286           210879              61040000    True
3    C139           212475              77708280    True
4    C139           212465              77878320    True
5    C296           216177              101335000   True
13   C321           214526              56680000    True
id  pay_authority_no    order_kind  order_no
2         210879         Reorder    84182
1         210995         Reorder    83251
4         212465         Sup        459950
3         212475         Sup        459948
15        212475         Sup        65878
13        214526         Reorder    86019
14        214526         Reorder    86020
5         216177         Reorder    83715
结果也是:

corp_code   pay_authority_no    authority_price order_no    order_kind
   C139           212465           77878320      459950      Sup
   C139           212475           77708280      459948      Sup
   C139           212475           77708280      65878       Sup
   C286           210879           61040000      84182       Reorder
   C286           210995           85020000      83251       Reorder
   C296           216177           101335000     83715       Reorder
   C321           214526           56680000      86019       Reorder
   C321           214526           56680000      86020       Reorder
我希望结果如下所示:

corp_code   pay_authority_no  authority_price   order_no    order_kind
    C139         212465         77878320         459950      Sup
    C139         212475         77708280         459948      Sup
    C139         212475         Null or 0        65878       Sup
    C286         210879         61040000         84182       Reorder
    C286         210995         85020000         83251       Reorder
    C296         216177         101335000        83715       Reorder
    C321         214526         56680000         86019       Reorder
    C321         214526         Null or 0        86020       Reorder
请解决这个问题

试试这个:

SELECT t1.corp_code, t1.pay_authority_no, 
       CASE 
          WHEN t2.rn = 1 THEN t1.authority_price
          ELSE NULL
      END authority_price,
      t2.order_no, t2.order_kind
FROM tab1 AS t1
JOIN (
   SELECT order_kind, order_no, pay_authority_no,
          ROW_NUMBER() OVER (PARTITION BY pay_authority_no ORDER BY id) AS rn
   FROM tab2
) AS t2 ON t1.pay_authority_no = t2.pay_authority_no
ORDER BY t1.corp_code,t1.pay_authority_no

你可以试试这样的

;WITH cte AS(
                SELECT t1.corp_code,t1.pay_authority_no,t1.authority_price,
                       t2.order_no,t2.order_kind,ROW_NUMBER() OVER(PARTITION BY authority_price ORDER BY corp_code) ROWN
                FROM   tab1 t1
                       INNER JOIN tab2 t2 ON  t2.pay_authority_no = t1.pay_authority_no )

SELECT corp_code,pay_authority_no,
        CASE 
            WHEN ROWN = 1 THEN authority_price 
            ELSE NULL
        END authority_price,
       order_no,order_kind
FROM   cte
ORDER BY corp_code,pay_authority_no

使用标准化
此技术可删除数据库中的重复条目

始终标记您可以使用的sql Server版本(
ROW\u NUMBER())(按权限分区\u价格顺序,按任何方式…)
然后是一个CASE语句,如当RN=1时的
CASE,然后是authority\u price END
而不是选择authority\u price。我希望将此结果用于stimulreports.net,但当stimulsoft reports.net designer将重复集处理为merge时,则total不为真。因此,要正确计算我的sql server版本为2016年的总数量,必须将重复am的总和为零