我有以下四個表中的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]作爲用於以下的邏輯:
- 的ChildGroupSequence列應父組內表示子組的序列,從1000開始並遞增通過100.即第一個子組將爲1000,第二個子組將爲1100.
- 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 :非常感謝你指出這一點。
非常感謝你這個答案,非常感謝。 – Sathish 2014-09-03 13:37:46