如何在主描述列中包含父描述?

gstyhher  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(332)

我有一个sql查询:

SELECT
    Object.Description,
    categories.Description AS ParentDescription
FROM ObjectInspectionLog oil
INNER JOIN Object ON oil.ObjectId = Object.ObjectId
INNER JOIN Object as categories ON Object.ParentId = categories.ObjectId
WHERE oil.Notes IS NULL
GROUP BY Object.Description, categories.Description
ORDER BY MIN(Object.ObjectId)

仅供参考:我的实际命名不使用name对象 ObjectInspectionLog :

ObjectInspectionLogId   JobId   ObjectId    Notes
3,  6669,   15, NULL    
4,  6669,   2,  NULL    
5,  6669,   3,  Rear tires worn 
6,  6669,   4,  NULL    
7,  6669,   5,  NULL    
8,  6669,   14, NULL
``` `Object` ```
ObjectId    ParentId    Description
1,  NULL,   Chassis
2,  1,  Front Tires
3,  1,  Rear Tires
4,  1,  Windshield Condition
5,  1,  Headlights
13, NULL,   Adaptive Equipment
14, 13, Occupied Wheelchair Lift
15, 14, Dual post lift

目前,我得到的返回结果如下:(为简洁起见删除了一些信息)

Description ParentDescription
Windshield Condition,   Chassis
Headlights, Chassis
Occupied Wheelchair Lift,   Adaptive Equipment
ETC

我的目标是得到一个包含父级描述(底盘、自适应设备等)的列表
我想要的是:

Description ParentDescription
Chassis, NULL
Windshield Condition,   Chassis
Headlights, Chassis
Adaptive Equipment, NULL
Occupied Wheelchair Lift,   Adaptive Equipment
ETC
laximzn5

laximzn51#

如果我理解的很好,你在找这样的东西:

SELECT obj.description,categories.description
FROM Object obj
LEFT JOIN Object as categories ON obj.ParentId = categories.ObjectId
WHERE EXISTS(
    SELECT *
    FROM ObjectInspectionLog oil
    INNER JOIN Object obj2 ON oil.ObjectId = obj2.ObjectId
    WHERE oil.Notes IS NULL
    AND obj.ObjectId in ( obj2.ObjectId, obj2.parentId)
    )
t40tm48m

t40tm48m2#

无论对象是否被 ObjectInspectionLog ,例如。
ObjectId 1 应该生成一个输出行。不清楚你为什么要用 group by .
以下代码使用 outer 加入以更接近您可能需要的内容:

-- Sample data.
declare @ObjectInsopectionLog as Table ( ObjectInspectionLogId Int, JobId Int, ObjectId Int, Notes VarChar(32) );

insert into @ObjectInsopectionLog ( ObjectInspectionLogId, JobId, ObjectId, Notes ) values
  ( 3,  6669,   15, NULL ),
  ( 4,  6669,   2,  NULL ),
  ( 5,  6669,   3,  'Rear tires worn' ),
  ( 6,  6669,   4,  NULL ),
  ( 7,  6669,   5,  NULL ),
  ( 8,  6669,   14, NULL );

select * from @ObjectInsopectionLog;

declare @Objects as Table ( ObjectId Int, ParentId Int, Description VarChar(32) );

insert into @Objects ( ObjectId, ParentId, Description ) values
  ( 1,  NULL, 'Chassis' ),
  ( 2,  1, 'Front Tires' ),
  ( 3,  1, 'Rear Tires' ),
  ( 4,  1, 'Windshield Condition' ),
  ( 5,  1, 'Headlights' ),
  ( 13, NULL, 'Adaptive Equipment' ),
  ( 14, 13, 'Occupied Wheelchair Lift' ),
  ( 15, 14, 'Dual post lift' );

select * from @Objects;

-- Do the work.
select Child.Description, Parent.Description as ParentDescription, OIL.ObjectId as OIL_ObjectId
  from @ObjectInsopectionLog as OIL full outer join -- Return all rows from either side of this   join .
    @Objects as Child on Child.ObjectId = OIL.ObjectId left outer join -- Return all   Child   rows regardless of   Parent .
    @Objects as Parent on Parent.ObjectId = Child.ParentId
  where OIL.Notes is NULL
  order by Child.ObjectId;

相关问题