如何在结果集中为不同的id插入虚拟行

时间:2017-07-08 17:26:42

标签: sql-server sql-server-2008 tsql

我的结果集如下:

+---------+-----------+--------+-----------------+
| ChildID | ReleaseID | Status | ParentSignature |
+---------+-----------+--------+-----------------+
|    1152 |        68 | Yes    |                 |
|    1152 |        70 | Yes    |                 |
|    5059 |        68 | Yes    | ad              |
|    5410 |        68 | Yes    | 111             |
|    5410 |        70 | Yes    | 111             |
|    5410 |        71 | Yes    |                 |
+---------+-----------+--------+-----------------+

在上面的结果集中,ReleaseID列中有3个不同的值,例如68,70和71。 只有一个子ID(5410)具有与所有释放对应的记录。

我的要求是获取一个输出结果集,其中每个版本ID的每个子项都有记录,其余列的空白值。 最大不同释放物的数量可以变化。在此示例中,有3个释放。

预期产出: -

+---------+-----------+--------+-----------------+
| ChildID | ReleaseID | Status | ParentSignature |
+---------+-----------+--------+-----------------+
|    1152 |        68 | Yes    |                 |
|    1152 |        70 | Yes    |                 |
|    1152 |        71 |        |                 |
|    5059 |        68 | Yes    | ad              |
|    5059 |        70 |        |                 |
|    5059 |        71 |        |                 |
|    5410 |        68 | Yes    | 111             |
|    5410 |        70 | Yes    | 111             |
|    5410 |        71 | Yes    |                 |
+---------+-----------+--------+-----------------+

3 个答案:

答案 0 :(得分:2)

/*
Node is defined as 

typedef struct node
{
   int data;
   node * left;
   node * right;
}node;

*/


bool find(node *root,int val)  //to check if the value exist under the given node or not
{
    if(root==NULL)
        return false;
    if(root->data==val)
        return true;

    if((root->left&&find(root->left,val))||(root->right&&find(root->right,val)))
        return true;
    return false;
}


node * lca(node * root, int v1,int v2)   //to find the lowest common ancestor
{
    if(root==NULL)
        return NULL;
    static node* ans=NULL;
    lca(root->left,v1,v2);  //traversing to the bottom of the tree
    lca(root->right,v1,v2);

    if((find(root->left,v1)&&find(root->right,v2))||(find(root->left,v2)&&find(root->right,v1)))   //checking the existence of both nodes under the tree
    {
        if(ans==NULL)
            ans=root;
    }

    return ans;  //returning the lca
}

答案 1 :(得分:1)

您可以查询如下:

;With cte as (
    SELECT distinct clientid, a.releaseid FROM #input x
    cross apply (select distinct releaseid from #input) a
) Select c.*, i.[Status], i.ParentSignature from cte c
    left join #input i
    on c.clientid = i.clientid
    and c.releaseid = i.releaseid

答案 2 :(得分:0)

select  *
into    #input
from (
    select   1152  as clientid,        68 as releaseid , 'Yes' as status    ,  '' as ParentSignature union all   
    select   1152 ,        70 , 'Yes'    , ''     union all       
    select   5059 ,        68 , 'Yes'    , 'ad '  union all       
    select   5410 ,        68 , 'Yes'    , '111 ' union all       
    select   5410 ,        70 , 'Yes'    , '111 ' union all       
    select   5410 ,        71 , 'Yes'    , ''     
) x 

SELECT * FROM #input x

SELECT  b.clientid
FROM    #input b
GROUP BY b.clientid
HAVING  COUNT(DISTINCT b.releaseid) = (SELECT COUNT(DISTINCT a.releaseid) FROM #input a)

enter image description here

注意:如果在UNIQUE上定义了{clientid, releaseid}索引/约束,那么我会从DISTINCT中删除COUNT(DISTINCT b.releaseid)