简体   繁体   English

sql查询结果格式

[英]sql query result format

Sorry for inconvenience,i thought i am simplifying the question but may be i made it more complex,previously the data was like this, 很抱歉给我带来不便,我认为我正在简化这个问题,但可能会让我更复杂,之前的数据是这样的,

Table: 表:

BRANCHCD       BAL1    BAL2   ACMCD
SH14           10        -      111
SH14           11        -      112
SH14            -         1     211 

in one table had bal1 for acmcd and bal2 for acmcd,onl one will be available at a time so don't worry about that case,so and i need it in following format. 在一个表中有一个用于acmcd的bal1和一个用于acmcd的bal2,onl将一次可用,所以不要担心这种情况,所以我需要它以下面的格式。

  BRANCHCD       BAL1    ACMCD   bal2  acmcd
    SH14           10      111      1    211
    SH14           11      112

if new rows added in Table are: 如果表中添加的新行是:

BRANCHCD       BAL1    BAL2   ACMCD
SH14                     2      212
SH14                     3      213

then o/p should be 那么o / p应该是

  BRANCHCD       BAL1    ACMCD   bal2  acmcd
    SH14           10      111      1    211
    SH14           11      112      2    212
    SH14                            3    213

I presume your desired output is to basically compress the new records to show them juxtaposed rather than as new entries below. 我假设你想要的输出是基本压缩新记录,以显示它们并列而不是下面的新条目。 So, there is no relation between acmcd = 112 and acmcd = 212 other than the fact that they are 2nd available entries under respective "bal"s for a branchcd in the table. 因此, acmcd = 112acmcd = 212之间没有任何关系, acmcd = 112它们是表格中的branchcd相应“bal”下的第二个可用条目。 It also appears that the column acmcd gets a unique value for each newly added row ( sequentially or not). 似乎列acmcd为每个新添加的行(顺序或不顺序)获取唯一值。

If the above statements are true, you could use row_number() to generate ids for each unique acmcds for a given BRANCHCD . 如果以上陈述为真,则可以使用row_number()为给定BRANCHCD每个唯一acmcds生成id。 Creating bal1 and bal2 records as separate data sets through a cte or sub-query, we could do a FULL OUTER JOIN on row_number . 通过cte或子查询将bal1和bal2记录创建为单独的数据集,我们可以在row_number上执行FULL OUTER JOIN

SQL Fiddle SQL小提琴

Query : 查询

WITH a 
     AS (SELECT row_number() 
                  OVER( 
                    partition BY branchcd 
                    ORDER BY acmcd ) AS rn, 
                t.* 
         FROM   t 
         WHERE  bal1 IS NOT NULL), 
     b 
     AS (SELECT row_number() 
                  OVER( 
                    partition BY branchcd 
                    ORDER BY acmcd ) AS rn, 
                t.* 
         FROM   t 
         WHERE  bal2 IS NOT NULL) 
SELECT COALESCE(a.branchcd,b.branchcd) as branchcd,
       a.bal1, 
       a.acmcd, 
       b.bal2, 
       b.acmcd 
FROM   a 
       FULL OUTER JOIN b 
                    ON ( a.branchcd = b.branchcd 
                         AND a.rn = b.rn )

Results : 结果

| BRANCHCD |   BAL1 |  ACMCD | BAL2 | ACMCD |
|----------|--------|--------|------|-------|
|     SH14 |     10 |    111 |    1 |   211 |
|     SH14 |     11 |    112 |    2 |   212 |
|     SH14 | (null) | (null) |    3 |   213 |

Unless your tables have more columns that you aren't sharing with us, it seems you need to add to your left join of table C something that compares the last two digits of ACMCD. 除非您的表中有更多列不与我们共享,否则您似乎需要在表C的左侧连接中添加一个比较ACMCD最后两位数的内容。 Otherwise you aren't giving any indication to the query as to how those records are related. 否则,您不会向查询提供有关这些记录如何相关的任何指示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM