gpt4 book ai didi

SQL服务器: calculate sequence in a SQL query

转载 作者:行者123 更新时间:2023-12-02 20:42:43 32 4
gpt4 key购买 nike

我在 SQL Server 2008R2 中有以下四个表:

DECLARE @ParentGroup TABLE (ParentGroup_ID INT, ParentGroup_Name VARCHAR(100));
DECLARE @ChildGroup TABLE (ChildGroup_id INT, ChildGroup_name VARCHAR(100), ParentGroup_id INT);
DECLARE @Entity TABLE ([Entity_id] INT, [Entity_name] VARCHAR(100));
DECLARE @ChildGroupEntity TABLE (ChildGroupEntity_id INT, ChildGroup_id INT, [Entity_ID] INT);
INSERT INTO @parentGroup VALUES (1, 'England'), (2, 'USA');
INSERT INTO @ChildGroup VALUES (10, 'Sussex', 1), (11, 'Essex', 1), (12, 'Middlesex', 1);
INSERT INTO @entity VALUES (100, 'Entity0'),(101, 'Entity1'),(102, 'Entity2'),(103, 'Entity3'),(104, 'Entity4'),(105, 'Entity5'),(106, 'Entity6');
INSERT INTO @ChildGroupEntity VALUES (1000, 10, 100), (1001, 10, 101), (1002, 10, 102), (1003, 11, 103), (1004, 11, 104), (1005, 12, 100), (1006, 12, 105), (1007, 12, 106);
/*
SELECT * FROM @parentGroup
SELECT * FROM @ChildGroup
SELECT * FROm @entity
SELECT * FROM @ChildGroupEntity
*/

各表之间的关系如下:

SELECT ParentGroup_Name, ChildGroup_name, [Entity_name],  0 [ChildGroupSequence], 0 [EntitySequence]
FROM @ChildGroupEntity cge
INNER JOIN @ChildGroup cg ON cg.ChildGroup_id=cge.ChildGroup_id
INNER JOIN @parentGroup pg ON pg.parentGroup_id=cg.parentGroup_id
INNER JOIN @entity e ON e.[entity_id]=cge.[Entity_ID]
ORDER BY ParentGroup_Name, ChildGroup_name, [Entity_name]

上述查询的输出是:

-------------------------------------------------------------------------------
ParentGroup_Name|ChildGroup_name|Entity_name|ChildGroupSequence|EntitySequence|
-------------------------------------------------------------------------------
England |Essex |Entity3 |0 |0 |
England |Essex |Entity4 |0 |0 |
England |Middlesex |Entity0 |0 |0 |
England |Middlesex |Entity5 |0 |0 |
England |Middlesex |Entity6 |0 |0 |
England |Sussex |Entity0 |0 |0 |
England |Sussex |Entity1 |0 |0 |
England |Sussex |Entity2 |0 |0 |
-------------------------------------------------------------------------------

现在,我想找出父组 1 的子组以及与子组关联的所有实体。另外,我想计算 [ChildGroupSequence]、[EntitySequence],逻辑如下:

  1. ChildGroupSequence 列应表示子组在父组中的顺序,从 1000 开始并以 100 递增。即第一个子组将为 1000,第二个子组将为 1100。
  2. EntitySequence 列应表示子组内的实体序列,从 100 开始并以个位数递增,并为每个子组重置。 IE。子组 1 中的第一个实体从 100 开始,子组 2 中的第一个实体也是如此。

因此,输出应采用以下格式:

-------------------------------------------------------------------------------
ParentGroup_Name|ChildGroup_name|Entity_name|ChildGroupSequence|EntitySequence|
-------------------------------------------------------------------------------
England |Essex |Entity3 |1000 |100 |
England |Essex |Entity4 |1000 |101 |
England |Middlesex |Entity0 |1100 |100 |
England |Middlesex |Entity5 |1100 |101 |
England |Middlesex |Entity6 |1100 |102 |
England |Sussex |Entity0 |1200 |100 |
England |Sussex |Entity1 |1200 |101 |
England |Sussex |Entity2 |1200 |102 |
-------------------------------------------------------------------------------

我可以通过将值读入应用程序层(.Net 程序)来轻松完成此操作,但想通过尝试一些像这样的小事情来学习 SQL Server。谁能帮我写这个 SQL 查询吗?

任何帮助将不胜感激。提前致谢。

编辑:我的示例数据似乎没有正确反射(reflect)第一条规则,该规则规定 ChildGroupSequence 应增加 100,示例输出增加 1。第二个查询反射(reflect)增量 100。@jpw:非常感谢您的指出这个出来了。

最佳答案

我相信这可以使用 partitioning 来完成和 ranking函数如下:

SELECT ParentGroup_Name, ChildGroup_name, [Entity_name], 
999 + DENSE_RANK() OVER(PARTITION BY ParentGroup_Name ORDER BY ChildGroup_name) AS [ChildGroupSequence],
99 + ROW_NUMBER() OVER(PARTITION BY ParentGroup_Name, ChildGroup_name ORDER BY ChildGroup_name, Entity_name) AS [EntitySequence]
FROM @ChildGroupEntity cge
INNER JOIN @ChildGroup cg ON cg.ChildGroup_id=cge.ChildGroup_id
INNER JOIN @parentGroup pg ON pg.parentGroup_id=cg.parentGroup_id
INNER JOIN @entity e ON e.[entity_id]=cge.[Entity_ID]
ORDER BY ParentGroup_Name, ChildGroup_name, [Entity_name]

此查询生成您所描述的示例输出。您的示例数据似乎没有正确反射(reflect)第一个规则,但该规则规定 ChildGroupSequence 应增加 100 并且示例输出增加 1。第二个查询反射(reflect)增量为 100:

SELECT ParentGroup_Name, ChildGroup_name, [Entity_name], 
900 + 100 * DENSE_RANK() OVER(PARTITION BY ParentGroup_Name ORDER BY ChildGroup_name) AS [ChildGroupSequence],
99 + ROW_NUMBER() OVER(PARTITION BY ParentGroup_Name, ChildGroup_name ORDER BY ChildGroup_name, Entity_name) AS [EntitySequence]
FROM @ChildGroupEntity cge
INNER JOIN @ChildGroup cg ON cg.ChildGroup_id=cge.ChildGroup_id
INNER JOIN @parentGroup pg ON pg.parentGroup_id=cg.parentGroup_id
INNER JOIN @entity e ON e.[entity_id]=cge.[Entity_ID]
ORDER BY ParentGroup_Name, ChildGroup_name, [Entity_name]

请参阅此sample SQL Fiddle有关这两个查询的示例。

也许查询应该按 ID 而不是名称分区,如果是这样,苏塞克斯将排在埃塞克斯之前,因为它的 ID 较低,查询将是这样的:

SELECT ParentGroup_Name, ChildGroup_name, [Entity_name], 
900 + 100 * DENSE_RANK() OVER(PARTITION BY pg.ParentGroup_ID ORDER BY cg.ChildGroup_ID) AS [ChildGroupSequence],
99 + ROW_NUMBER() OVER(PARTITION BY pg.ParentGroup_ID, cg.ChildGroup_ID ORDER BY cg.ChildGroup_ID, cge.Entity_ID) AS [EntitySequence]
FROM @ChildGroupEntity cge
INNER JOIN @ChildGroup cg ON cg.ChildGroup_id=cge.ChildGroup_id
INNER JOIN @parentGroup pg ON pg.parentGroup_id=cg.parentGroup_id
INNER JOIN @entity e ON e.[entity_id]=cge.[Entity_ID]
ORDER BY pg.ParentGroup_ID, cg.ChildGroup_ID, [Entity_name]

关于SQL服务器: calculate sequence in a SQL query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25643857/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com