2014-09-03 72 views
6

我有以下四個表中的SQL Server 2008 R2:SQL服務器:在SQL查詢計算序列

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開始並遞增單個數字,爲​​每個子組重新設置。即在100 childgroup 1點開始的第一個實體,如childgroup做的第一實體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服務器。任何人都可以幫我寫這個SQL查詢嗎?

任何幫助將不勝感激。提前致謝。

編輯: 我的示例數據似乎沒有正確反映第一條規則,該規則指出ChildGroupSequence應該增加100,並且示例輸出增加1.第二個查詢反映增量100。@jpw :非常感謝你指出這一點。

回答

1

我相信這可以通過使用像這樣partitioningranking功能來完成:

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] 

此查詢會生成您所描述的樣本輸出。您的樣本數據似乎並沒有正確地反映第一條規則,雖然作爲規則規定,ChildGroupSequence應該由100遞增和1採樣輸出增加第二個查詢反映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] 
+0

非常感謝你這個答案,非常感謝。 – Sathish 2014-09-03 13:37:46

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

結果:

enter image description here

+1

非常感謝您的回答,非常感謝。我得到的示例輸出錯誤較早,但您的查詢正好返回了我在示例輸出中說明的內容。 – Sathish 2014-09-03 13:39:59

1

您可以通過使用ranking functions

012解決這個

結果你可以在這裏找到SQL Fiddle

+0

非常感謝您的答覆,非常感謝。我得到的示例輸出錯誤較早,但您的查詢正好返回了我在示例輸出中說明的內容。 – Sathish 2014-09-03 13:38:36