Sql 按描述顺序获取具有多个子项的父项

Sql 按描述顺序获取具有多个子项的父项,sql,sql-server,Sql,Sql Server,如何在SQL Server中以desc的顺序获取包含多个子的父项,以在列表顶部显示新创建的父项,其子项 选择CRMEvents.ID,ISNULL(CRMEvents.ParentEventId,'')作为父项 从CRMEvents 其中partyid=4133 按ISNULL(CRMEvents.ParentEventID、CRMEvents.ID)、CRMEvents.CreatedOn排序 我需要按照父母1及其子女、父母2及其子女的顺序进行描述。 MS SQL Server 2017

如何在SQL Server中以
desc
的顺序获取包含多个子
父项
,以在列表顶部显示新创建的
父项
,其子项

选择CRMEvents.ID,ISNULL(CRMEvents.ParentEventId,'')作为父项
从CRMEvents
其中partyid=4133
按ISNULL(CRMEvents.ParentEventID、CRMEvents.ID)、CRMEvents.CreatedOn排序


我需要按照父母1及其子女、父母2及其子女的顺序进行描述。

MS SQL Server 2017架构设置

CREATE TABLE myproblem (
   id int,
   parent int);

INSERT INTO myproblem values (5098,0),(5099,5098),(5100,5099),(5101,5099);
INSERT INTO myproblem values (5102,0),(5107,5102);
INSERT INTO myproblem values (5106,0),(5108,5106),(5110,5106);
INSERT INTO myproblem values (5109,0),(5111,5109);
INSERT INTO myproblem values (5112,0);
SELECT ParentUserType.Id As Parent ,count(ChildUserType.Id) AS 'Number of children'
    FROM myproblem AS ChildUserType
    LEFT JOIN myproblem AS ParentUserType ON ChildUserType.Parent = ParentUserType.Id
    GROUP BY ParentUserType.Id
    ORDER BY COUNT(ChildUserType.Id)
| Parent | Number of children |
|--------|--------------------|
|   5098 |                  1 |
|   5102 |                  1 |
|   5109 |                  1 |
|   5106 |                  2 |
|   5099 |                  2 |
| (null) |                  5 |
查询1

CREATE TABLE myproblem (
   id int,
   parent int);

INSERT INTO myproblem values (5098,0),(5099,5098),(5100,5099),(5101,5099);
INSERT INTO myproblem values (5102,0),(5107,5102);
INSERT INTO myproblem values (5106,0),(5108,5106),(5110,5106);
INSERT INTO myproblem values (5109,0),(5111,5109);
INSERT INTO myproblem values (5112,0);
SELECT ParentUserType.Id As Parent ,count(ChildUserType.Id) AS 'Number of children'
    FROM myproblem AS ChildUserType
    LEFT JOIN myproblem AS ParentUserType ON ChildUserType.Parent = ParentUserType.Id
    GROUP BY ParentUserType.Id
    ORDER BY COUNT(ChildUserType.Id)
| Parent | Number of children |
|--------|--------------------|
|   5098 |                  1 |
|   5102 |                  1 |
|   5109 |                  1 |
|   5106 |                  2 |
|   5099 |                  2 |
| (null) |                  5 |

CREATE TABLE myproblem (
   id int,
   parent int);

INSERT INTO myproblem values (5098,0),(5099,5098),(5100,5099),(5101,5099);
INSERT INTO myproblem values (5102,0),(5107,5102);
INSERT INTO myproblem values (5106,0),(5108,5106),(5110,5106);
INSERT INTO myproblem values (5109,0),(5111,5109);
INSERT INTO myproblem values (5112,0);
SELECT ParentUserType.Id As Parent ,count(ChildUserType.Id) AS 'Number of children'
    FROM myproblem AS ChildUserType
    LEFT JOIN myproblem AS ParentUserType ON ChildUserType.Parent = ParentUserType.Id
    GROUP BY ParentUserType.Id
    ORDER BY COUNT(ChildUserType.Id)
| Parent | Number of children |
|--------|--------------------|
|   5098 |                  1 |
|   5102 |                  1 |
|   5109 |                  1 |
|   5106 |                  2 |
|   5099 |                  2 |
| (null) |                  5 |

不要把图像放在这里。。。(除非他们真的把事情说得更清楚,在这种情况下不是这样的…)我之前的评论是想说这个链接更清楚:看起来不错!但是你知道解决我问题的办法吗。请给我一个解决方案您想要订购物品,但不清楚
id
Parentx
之间的链接是什么,如果我得到了正确的描述,您想按
Parentx
排序吗?父级可以有一个或多个子级,同样,子级也可以有一个或多个子级。因此,我希望最新的家长及其子女在顶部