如何通过这样的结果集来获得组的类型。

时间:2017-10-03 21:44:39

标签: sql sql-server

我将此结果集设置为#temptable,这是我从加入2个父表和子表时得到的。这是JOIN SQL QUERY我有

    SELECT P.Parent_Id, C.Child_ID,   C.C1_PID   , C.C2_PID
FROM PARENT P INNER JOIN CHILD C ON P.Parent_Id
CASE 
WHEN C.C1_PID = '' OR C.C1_PID IS NULL 
THEN C.C2_PID
WHEN C.C1_PID <>'' THEN C.C1_PID
END 

当前#temptable结果集我正在

Parent_Id  Child_ID   C1_PID    C2_PID
========  =========  ======== =========
 1          X1          1       1
 1          X2                  1
 1          X3          1       
 2          Y1                  2
 2          Y2                  2
 2          Y3                  2
 3          Z1          3       3
 3          Z2          3       3
 3          Z3          3       3

 Now I would like to convert result set of this #temptable to like this below
What I want to show IS DISPLAY EMPTY LIKE A SPACE OR BLANK OR NULL IN PLACE OF CHILD COLUMNS DATA AND SHOW THE CHILD DATA IN NEXT ROW WITH BLANK OR NULL OR EMPTY SPACE IN PLACE OF PARENT ID 


Parent_Id  Child_ID   C1_PID    C2_PID
========  =========  ======== =========
 1          
            X1          1       1       
            X2                  1
            X3          1       
 2          
            Y1                  2
            Y2                  2
            Y3                  2
 3                      
            Z1          3       3
            Z2          3       3
            Z3          3       3

3 个答案:

答案 0 :(得分:0)

好的,现在,在你编辑之后,我明白了,我的解决方案很简单,看看:

;with
f as (
    select Parent_Id, null Child_ID, null C1_PID, null C2_PID from #temptable
    union
    select * from #temptable
)
select 
    CASE WHEN Child_ID IS NULL THEN Parent_Id ELSE NULL END Parent_Id,
    Child_ID, C1_PID, C2_PID
from f
order by f.Parent_Id, child_id

小心
必须指定ORDER BY子句,因为SQL没有&#34;插入顺序&#34;您需要指定条件或结果可能是意外的。

答案 1 :(得分:0)

假设您有以下Y

Dataset

并且您希望以下列格式显示Parent_Id Child_ID C1_PID C2_PID ======== ========= ======== ========= 1 X1 1 1 1 X2 1 1 X3 1 2 Y1 2 2 Y2 2 2 Y3 2 3 Z1 3 3 3 Z2 3 3 3 Z3 3 3 以上:

Dataset

如果以上所有内容均正确无误,请尝试以下Parent_Id Child_ID C1_PID C2_PID ======== ========= ======== ========= 1 X1 1 1 X2 1 X3 1 2 Y1 2 Y2 2 Y3 2 3 Z1 3 3 Z2 3 3 Z3 3 3 ,以帮助您实现上述结果:

SQL Query

期望输出:

enter image description here

希望它会对你有所帮助。

答案 2 :(得分:0)

我建议使用此查询以获得更好的性能

                /*NULL Columns Which you don't want data*/
SELECT  CASE  WHEN ID = 1 THEN Parent_Id END AS Parent_Id,
        CASE  WHEN ID != 1 THEN Child_ID END AS Child_ID,
        CASE  WHEN ID != 1 THEN C1_PID END AS C1_PID,
        CASE  WHEN ID != 1 THEN C2_PID END AS C2_PID
FROM 
(
        SELECT ROW_NUMBER() OVER(PARTITION BY Parent_Id ORDER BY Parent_Id)ID,Parent_ID,Child_ID,C1_PID,C2_PID
        FROM 
        (
            /*This is your Query*/
            SELECT  ROW_NUMBER() OVER(PARTITION BY P.Parent_Id ORDER BY P.Parent_Id)ID, P.Parent_Id, C.Child_ID, C.C1_PID, C.C2_PID
            FROM    @PARENT P
            INNER JOIN @CHILD C ON P.Parent_Id=( CASE WHEN C.C1_PID='' OR C.C1_PID IS NULL THEN C.C2_PID
                                                                WHEN C.C1_PID<>'' THEN C.C1_PID END )
        )Y
        /*Logic Query*/
        LEFT OUTER JOIN  (  SELECT 1 PId
                            UNION ALL
                            SELECT 1 PId

                            ) X ON X.PId = Y.ID
)FinalData