Author |
Topic |
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2013-03-08 : 02:31:58
|
Select BC.CatID , BC.Category , BC.Slug , BC.ParentID , Case When CC.Cnt Is Null Then 0 Else CC.Cnt End As CntFrom dbo.Blog_Categories BC Left Outer Join (Select ec.CatID , Count(0) As Cnt From dbo.Blog_Entry_Categories ec Inner Join dbo.Blog_Entries e On e.EntryId = ec.EntryId Group By ec.CatID , e.Published Having e.Published = 1) CC On BC.CatID = CC.CatID I have a table Blog_Categories which have self refrencing catid and parentidWhat i wanted is that the result set should come item is coming in random order according to catid and parentidwhat i want's is that first a catid and it's child come then the second catid and it's child and so onHow can i modify above query Kamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-08 : 02:51:20
|
May be this?ORDER BY ParentID, CatID One more thing is.. you can use COALESCE instead of CASE statement COALESCE( CC.Cnt, 0) cnt instead of Case When Is Null Then 0 Else CC.Cnt End As Cnt--Chandu |
|
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2013-03-08 : 04:33:03
|
quote: Originally posted by bandi May be this?ORDER BY ParentID, CatID One more thing is.. you can use COALESCE instead of CASE statement COALESCE( CC.Cnt, 0) cnt instead of Case When Is Null Then 0 Else CC.Cnt End As CntCoalesce is fine but actual problem didn't work by order by--Chandu
Kamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-08 : 04:48:01
|
Can you post sample data for 3 tables and also expected output ?--Chandu |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-08 : 05:33:16
|
can blogs categories go multiple lkevels down? like categories,level 1 subcategories,level2 subcategories etc?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2013-03-08 : 07:11:05
|
At most 1 level downMy sample data is (i have remove slug column)CatID Category ParentID Cnt-1 All 0 2241 Test Defect - Zimmer NexGen CR-Flex Knee 11 13 Other 0 125 ABC Defense 0 266 Family Law 0 47 Test Defect - DePuy ASR 11 7611 Test Defects 0 8514 Accident and Injury law 0 519 Drug Litigation 0 3620 Drug Litigation - Topamax 19 121 Drug Litigation - Depakote 19 122 Test Defect - DePuy Pinnacle 11 1523 Drug Litigation - Actos 19 3124 Test Defect - Transvaginal Mesh 11 2825 Texas Warrant Roundups 26 026 Traffic Tickets 0 4127 Drug Litigation - Pradaxa 19 2128 Test Defect - Biomet M2A 11 129 Test Defect - Smith & Nephew R3 11 030 Test Defect - Wright Conserve 11 031 Test Defect - Stryker Rejuvenate/ABG 11 2232 Wrongful Death 14 133 Immigration 0 1Kamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-08 : 07:59:18
|
check this one...DECLARE @Categories TABLE(CatID INT, Category VARCHAR(50), ParentID INT, Cnt INT)INSERT INTO @CategoriesSELECT -1, 'All', 0, 224 UNION ALLSELECT 1, 'Test Defect - Zimmer NexGen CR-Flex Knee', 11, 1 UNION ALLSELECT 3, 'Other', 0, 12 UNION ALLSELECT 5, 'ABC Defense', 0, 26 UNION ALLSELECT 6, 'Family Law', 0, 4 UNION ALLSELECT 7, 'Test Defect - DePuy ASR', 11, 76 UNION ALLSELECT 11, 'Test Defects', 0, 85 UNION ALLSELECT 14, 'Accident and Injury law', 0, 5 UNION ALLSELECT 19, 'Drug Litigation', 0, 36 UNION ALLSELECT 20, 'Drug Litigation - Topamax', 19, 1 UNION ALLSELECT 21, 'Drug Litigation - Depakote', 19, 1 UNION ALLSELECT 22, 'Test Defect - DePuy Pinnacle', 11, 15UNION ALLSELECT 23, 'Drug Litigation - Actos', 19, 31UNION ALLSELECT 24, 'Test Defect - Transvaginal Mesh', 11, 28UNION ALLSELECT 25, 'Texas Warrant Roundups', 26, 0UNION ALLSELECT 26, 'Traffic Tickets', 0, 41UNION ALLSELECT 27, 'Drug Litigation - Pradaxa', 19, 21UNION ALLSELECT 28, 'Test Defect - Biomet M2A', 11, 1UNION ALLSELECT 29, 'Test Defect - Smith & Nephew R3', 11, 0UNION ALLSELECT 30, 'Test Defect - Wright Conserve', 11, 0 UNION ALLSELECT 31, 'Test Defect - Stryker Rejuvenate/ABG', 11, 22UNION ALLSELECT 32, 'Wrongful Death', 14, 1UNION ALLSELECT 33, 'Immigration', 0, 1SELECT ParentID, CatID, Category, cntFROM @CategoriesORDER BY ParentID, CatID--Chandu |
|
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2013-03-08 : 09:04:51
|
It's wrong result.After Test defect [ATID = 11] row I wnat's it's child rows that is 1,7,22,24 ...CatID ParentID Category cnt-1 0 All 2243 0 Other 125 0 ABC Defense 266 0 Family Law 411 0 Test Defects 8514 0 Accident and Injury law 519 0 Drug Litigation 3626 0 Traffic Tickets 4133 0 Immigration 11 11 Test Defect - Zimmer NexGen CR-Flex Knee 17 11 Test Defect - DePuy ASR 7622 11 Test Defect - DePuy Pinnacle 1524 11 Test Defect - Transvaginal Mesh 2828 11 Test Defect - Biomet M2A 129 11 Test Defect - Smith & Nephew R3 030 11 Test Defect - Wright Conserve 031 11 Test Defect - Stryker Rejuvenate/ABG 2232 14 Wrongful Death 120 19 Drug Litigation - Topamax 121 19 Drug Litigation - Depakote 123 19 Drug Litigation - Actos 3127 19 Drug Litigation - Pradaxa 2125 26 Texas Warrant Roundups 0Kamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-08 : 09:07:50
|
quote: Originally posted by bandi check this one...DECLARE @Categories TABLE(CatID INT, Category VARCHAR(50), ParentID INT, Cnt INT)INSERT INTO @CategoriesSELECT -1, 'All', 0, 224 UNION ALLSELECT 1, 'Test Defect - Zimmer NexGen CR-Flex Knee', 11, 1 UNION ALLSELECT 3, 'Other', 0, 12 UNION ALLSELECT 5, 'ABC Defense', 0, 26 UNION ALLSELECT 6, 'Family Law', 0, 4 UNION ALLSELECT 7, 'Test Defect - DePuy ASR', 11, 76 UNION ALLSELECT 11, 'Test Defects', 0, 85 UNION ALLSELECT 14, 'Accident and Injury law', 0, 5 UNION ALLSELECT 19, 'Drug Litigation', 0, 36 UNION ALLSELECT 20, 'Drug Litigation - Topamax', 19, 1 UNION ALLSELECT 21, 'Drug Litigation - Depakote', 19, 1 UNION ALLSELECT 22, 'Test Defect - DePuy Pinnacle', 11, 15UNION ALLSELECT 23, 'Drug Litigation - Actos', 19, 31UNION ALLSELECT 24, 'Test Defect - Transvaginal Mesh', 11, 28UNION ALLSELECT 25, 'Texas Warrant Roundups', 26, 0UNION ALLSELECT 26, 'Traffic Tickets', 0, 41UNION ALLSELECT 27, 'Drug Litigation - Pradaxa', 19, 21UNION ALLSELECT 28, 'Test Defect - Biomet M2A', 11, 1UNION ALLSELECT 29, 'Test Defect - Smith & Nephew R3', 11, 0UNION ALLSELECT 30, 'Test Defect - Wright Conserve', 11, 0 UNION ALLSELECT 31, 'Test Defect - Stryker Rejuvenate/ABG', 11, 22UNION ALLSELECT 32, 'Wrongful Death', 14, 1UNION ALLSELECT 33, 'Immigration', 0, 1SELECT ParentID, CatID, Category, cntFROM @CategoriesORDER BY ParentID, CatID--Chandu
see the differenceDECLARE @Categories TABLE(CatID INT, Category VARCHAR(50), ParentID INT, Cnt INT)INSERT INTO @CategoriesSELECT -1, 'All', 0, 224 UNION ALLSELECT 1, 'Test Defect - Zimmer NexGen CR-Flex Knee', 11, 1 UNION ALLSELECT 3, 'Other', 0, 12 UNION ALLSELECT 5, 'ABC Defense', 0, 26 UNION ALLSELECT 6, 'Family Law', 0, 4 UNION ALLSELECT 7, 'Test Defect - DePuy ASR', 11, 76 UNION ALLSELECT 11, 'Test Defects', 0, 85 UNION ALLSELECT 14, 'Accident and Injury law', 0, 5 UNION ALLSELECT 19, 'Drug Litigation', 0, 36 UNION ALLSELECT 20, 'Drug Litigation - Topamax', 19, 1 UNION ALLSELECT 21, 'Drug Litigation - Depakote', 19, 1 UNION ALLSELECT 22, 'Test Defect - DePuy Pinnacle', 11, 15UNION ALLSELECT 23, 'Drug Litigation - Actos', 19, 31UNION ALLSELECT 24, 'Test Defect - Transvaginal Mesh', 11, 28UNION ALLSELECT 25, 'Texas Warrant Roundups', 26, 0UNION ALLSELECT 26, 'Traffic Tickets', 0, 41UNION ALLSELECT 27, 'Drug Litigation - Pradaxa', 19, 21UNION ALLSELECT 28, 'Test Defect - Biomet M2A', 11, 1UNION ALLSELECT 29, 'Test Defect - Smith & Nephew R3', 11, 0UNION ALLSELECT 30, 'Test Defect - Wright Conserve', 11, 0 UNION ALLSELECT 31, 'Test Defect - Stryker Rejuvenate/ABG', 11, 22UNION ALLSELECT 32, 'Wrongful Death', 14, 1UNION ALLSELECT 33, 'Immigration', 0, 1SELECT ParentID, CatID, Category, cntFROM @CategoriesORDER BY CASE WHEN ParentID=0 THEN CatID ELSE ParentID END,ParentIDoutput---------------------------------------------ParentID CatID Category cnt---------------------------------------------0 -1 All 2240 3 Other 120 5 ABC Defense 260 6 Family Law 40 11 Test Defects 8511 7 Test Defect - DePuy ASR 7611 1 Test Defect - Zimmer NexGen CR-Flex Knee 111 22 Test Defect - DePuy Pinnacle 1511 24 Test Defect - Transvaginal Mesh 2811 28 Test Defect - Biomet M2A 111 29 Test Defect - Smith & Nephew R3 011 30 Test Defect - Wright Conserve 011 31 Test Defect - Stryker Rejuvenate/ABG 220 14 Accident and Injury law 514 32 Wrongful Death 10 19 Drug Litigation 3619 20 Drug Litigation - Topamax 119 21 Drug Litigation - Depakote 119 27 Drug Litigation - Pradaxa 2119 23 Drug Litigation - Actos 310 26 Traffic Tickets 4126 25 Texas Warrant Roundups 00 33 Immigration 1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2013-03-08 : 09:20:39
|
Thanks a lot one more thingCan i somehow get result of Alphabetical order pranet category first then it's child categoryLike ---------------------------------------------ParentID CatID Category cnt---------------------------------------------0 5 ABC Defense 260 14 Accident and Injury law 514 32 Wrongful Death 10 -1 All 2240 19 Drug Litigation 3619 20 Drug Litigation - Topamax 119 21 Drug Litigation - Depakote 119 27 Drug Litigation - Pradaxa 2119 23 Drug Litigation - Actos 310 6 Family Law 40 33 Immigration 10 3 Other 120 11 Test Defects 8511 7 Test Defect - DePuy ASR 7611 1 Test Defect - Zimmer NexGen CR-Flex Knee 111 22 Test Defect - DePuy Pinnacle 1511 24 Test Defect - Transvaginal Mesh 2811 28 Test Defect - Biomet M2A 111 29 Test Defect - Smith & Nephew R3 011 30 Test Defect - Wright Conserve 011 31 Test Defect - Stryker Rejuvenate/ABG 220 26 Traffic Tickets 4126 25 Texas Warrant Roundups 0Kamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-08 : 09:27:35
|
how is this as per alphabetical order of parent? how did Other come after Traffic tikets? also Family Law after Immigration------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-11 : 01:25:25
|
quote: Originally posted by kamii47 As o comes after t and I comes after F o doesn't has any child so did FKamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net)
then hos is this alphabetic? in English alphabets o comes before t------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2013-03-11 : 01:49:47
|
It was a typo and wrong sample data.I have corrected the sample data.Kamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
|
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2013-03-11 : 06:02:02
|
I have resolved my problemSelect BC.CatID , Case When ParentID = 0 Then Bc.Category Else '---' + BC.CAtegory End As Category , BC.Slug , BC.ParentID , Case When CC.Cnt Is Null Then 0 Else CC.Cnt End As CntFrom dbo.Blog_Categories BC Left Outer Join (Select ec.CatID , Count(0) As Cnt From dbo.Blog_Entry_Categories ec Inner Join dbo.Blog_Entries e On e.EntryId = ec.EntryId Group By ec.CatID , e.Published Having e.Published = 1) CC On BC.CatID = CC.CatIDWhere BC.PortalID = 1Order By Case When ParentID = 0 Then CAtegory Else (Select CAtegory From dbo.Blog_Categories parent Where parent.CatID = bc.ParentID) End , Case When ParentID = 0 Then 1 End Desc , CategoryKamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-11 : 06:34:32
|
ok...great------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|