简体   繁体   English

如何编写此SQL查询?

[英]How do I write this SQL query?

I have a table that records a history of child items linked to a parent, with two columns: 我有一个表,该表记录了链接到父项的子项的历史记录,分为两列:

  • ParentId ParentId
  • ChildId ChildId

Some example data looks like this: 一些示例数据如下所示:

ParentId -- ChildId ParentId-ChildId
001 -- 001 001-001
001 -- 001 001-001
001 -- 001 001-001
001 -- 002 001-002
001 -- 002 001-002
001 -- 002 001-002
001 -- 003 001-003
001 -- 003 001-003
001 -- 003 001-003
001 -- 003 001-003
001 -- 004 001-004
001 -- 004 001-004
001 -- 005 001-005
001 -- 005 001-005
001 -- 005 001-005
001 -- 005 001-005

I need to select all the rows with the highest value ChildId for a given ParentId. 我需要为给定的ParentId选择所有值为ChildId最高的行。 So in the above example, I need the query to return the following rows, given an input parameter of '@parentId = 001': 因此,在上面的示例中,给定输入参数'@parentId = 001',我需要查询返回以下行:

001 -- 005 001-005
001 -- 005 001-005
001 -- 005 001-005
001 -- 005 001-005

Thanks for any help! 谢谢你的帮助!

This aught to do it: 要做到这一点:

SELECT * FROM MyTable
WHERE parentId = '001'
AND childId = (SELECT MAX(childId) FROM MyTable WHERE parentId = '001')

How about this? 这个怎么样?

SELECT ParentID, MAX(ChildID) AS ChildID
FROM TableName
GROUP BY ParentID

Updated to edit missed requirement to return all rows: 更新以编辑错过的要求以返回所有行:

Test Data 测试数据

-- Populate Test Data
CREATE TABLE #table (
  ParentID varchar(3) NOT NULL, 
  ChildID varchar(3) NOT NULL
)

INSERT INTO #table VALUES ('001','001')
INSERT INTO #table VALUES ('001','001')
INSERT INTO #table VALUES ('001','001')
INSERT INTO #table VALUES ('001','002')
INSERT INTO #table VALUES ('001','002')
INSERT INTO #table VALUES ('001','002')
INSERT INTO #table VALUES ('001','003')
INSERT INTO #table VALUES ('001','003')
INSERT INTO #table VALUES ('001','003')
INSERT INTO #table VALUES ('001','003')
INSERT INTO #table VALUES ('001','004')
INSERT INTO #table VALUES ('001','004')
INSERT INTO #table VALUES ('001','005')
INSERT INTO #table VALUES ('001','005')
INSERT INTO #table VALUES ('001','005')
INSERT INTO #table VALUES ('001','005')

Results 结果

-- Return Results
DECLARE @ParentID varchar(8)
SET @ParentID = '001'

SELECT T1.ParentID, T1.ChildID
FROM #table T1
JOIN (
    SELECT Q1.ParentID, MAX(Q1.ChildID) AS ChildID
    FROM #table Q1
    GROUP BY ParentID
) ParentChildMax ON ParentChildMax.ParentID = T1.ParentID AND ParentChildMax.ChildID = T1.ChildID
WHERE T1.ParentID = @ParentID

Note: The performance of this solution is identical to the accepted solution (according to SQL Server profiler) using the following statement in the WHERE clause. 注意:使用WHERE子句中的以下语句,此解决方案的性能与接受的解决方案(根据SQL Server Profiler)相同。 But I like my solution better as it seems cleaner to me and can be easily extended to include other ParentIDs is required. 但是我更喜欢我的解决方案,因为它对我来说似乎更干净,可以轻松扩展以包含其他ParentID。 (For example, reporting purposes.) (例如,出于报告目的。)

(SELECT MAX(childId) FROM #table WHERE parentId = @ParentID)
SELECT *
FROM TABLENAME
WHERE parentId = '001'
AND childid = (select MAX (ChildId) 
               from TABLENAME
               where parentId = '001')

SELECT * 选择 *
FROM table_name FROM table_name
WHERE ParentId = @parentId 在哪里ParentId = @parentId
GROUP BY ParentId GROUP BY ParentId
HAVING ChildId = MAX(ChildId) 拥有ChildId = MAX(ChildId)

To cater for multiple parents in the table, the following should help 为了照顾表中的多个父母,以下内容应有所帮助

select parentId, max(childid) as ChildId
from <table_name>
group by parentId

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

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