Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
JimAmigo
Posting Yak Master
119 Posts |
Posted - 2007-06-13 : 11:53:30
|
I need to put together a query that uses three different tables.1. tblBMS_BudgetCategories, has budget catgorys, (PK = CategoryID)2. tblBMS_BudgetCategories_Subcategories, has budget subcatgorys (PK = SubCategoryID, FK = CategoryID)3. tblBMS_Data_rel_BudgetCategories, has budget data, (PK = KeyID, FK = CategoryID, FK = SubCategoryID).My end result will have the max number of categorys and subcategorys, 36 rows of data representing all the categorys and subcategorys along with the data for each subcategory. (There is only one subcategory entry of data for each record.)This attempt gives me the exact query I need but DOESN'T include the 3rd table with the data..tblBMS_Data_rel_BudgetCategoriesSELECT c.categoryid, c.ShortDescription, sc.ShortDescription AS SubCategoryDescription, c.Active, c.DisplayOrderFROM tblBMS_BudgetCategories cINNER JOIN tblBMS_BudgetCategories_Subcategories sc ON sc.CategoryID = c.CategoryIDWHERE c.Active=1 ORDER BY c.DisplayOrderHere I try to add the data table to the query..SELECT c.categoryid, c.ShortDescription, sc.ShortDescription AS SubCategoryDescription, sc.SubCategoryID, d.profileid, d.value1, d.value2FROM tblBMS_BudgetCategories c, tblBMS_Data_rel_BudgetCategories d, tblBMS_BudgetCategories_Subcategories scWHERE c.CategoryID = d.CategoryID AND c.CategoryID = sc.CategoryIDAND c.Active=1 AND d.profileid = 34129ORDER BY c.DisplayOrder, sc.ShortDescriptionThis repeats some fo the subcategorys and values associated with them, even though there is only one entry for the corrseponding subcategory in the tblBMS_Data_rel_BudgetCategories table.Can I do this with a JOIN? Can this be done and how? |
|
JimAmigo
Posting Yak Master
119 Posts |
Posted - 2007-06-13 : 15:12:17
|
This seems to work.. but I would like to join the category table somehow to pull in the category name.SELECT d.KeyID, sc.categoryid, sc.ShortDescription, sc.SubCategoryID, d.profileid, d.value1, d.value2, sc.Value1_Exists, sc.Value1_UserEntered, sc.Value2_Exists, sc.Value2_UserEntered, d.budgetyearFROM tblBMS_BudgetCategories_Subcategories scLEFT OUTER JOIN tblBMS_Data_rel_BudgetCategories d on d.SubCategoryID = sc.SubCategoryID AND sc.Active=1 AND d.profileid = 34129ORDER BY sc.CategoryID, sc.DisplayOrder |
 |
|
JimAmigo
Posting Yak Master
119 Posts |
Posted - 2007-06-14 : 13:29:28
|
No one has any ideas? |
 |
|
JimAmigo
Posting Yak Master
119 Posts |
Posted - 2007-06-15 : 09:18:40
|
No one can assist me on how to add one more join/table table (tblBMS_BudgetCategories)to the query below to pull in the category name? Or is it so obvious that no one will answer me.. SELECT c.categoryid, c.ShortDescription, sc.ShortDescription AS SubCategoryDescription, sc.SubCategoryID, d.profileid, d.value1, d.value2FROM tblBMS_BudgetCategories c, tblBMS_Data_rel_BudgetCategories d, tblBMS_BudgetCategories_Subcategories scWHERE c.CategoryID = d.CategoryID AND c.CategoryID = sc.CategoryIDAND c.Active=1 AND d.profileid = 34129ORDER BY c.DisplayOrder, sc.ShortDescription |
 |
|
JimAmigo
Posting Yak Master
119 Posts |
Posted - 2007-06-18 : 11:50:57
|
No one can help? |
 |
|
JimAmigo
Posting Yak Master
119 Posts |
Posted - 2007-06-21 : 10:16:05
|
I guess I'll have to look elsewhere for my SQL answers.. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-21 : 10:25:47
|
Wow ! a one man thread !Maybe if you can post some sample data from these tables and the expected result. KH |
 |
|
JimAmigo
Posting Yak Master
119 Posts |
Posted - 2007-06-22 : 12:23:03
|
Here is the sample data.Table: tblBMS_BudgetCategoriesCategoryID Short Description Active DisplayOrder1 Clinical Protocols 1 402 Contracts 1 503 CSE 1 604 Animal Costs 1 105 Travel & Training 1 206 Supplies & Services 1 308 Personnel Ceilings 1 709 CANS 1 510 Center Numbers 1 611 Budget Adjustments 1 9 tblBMS_BudgetCategories_SubcategoriesSubCategoryID CategoryID ShortDescription Active DisplayOrder1 4 Purchases 1 22 4 Technical Services 1 33 4 Holding 1 1 44 4 Charles River 1 15 4 Contracts 1 56 5 Travel (Budget Total) 1 17 5 Training (Budget Total) 1 28 5 Patient Travel 1 39 6 NULL 1 310 6 NULL 1 411 1 NULL 1 112 1 NULL 1 213 1 NULL 1 314 2 NULL 1 315 2 NULL 1 416 2 NULL 1 517 3 NULL 1 118 3 NULL 1 219 3 NULL 1 320 3 Discretionary Funding 1 421 6 S&S (Budget Total) 1 123 8 FTE 1 124 8 Non-FTE 1 225 8 SAIC, Orkand 1 326 8 Students 1 427 8 SV/GR 1 528 2 Personnel Services 1 229 9 NULL 1 130 9 NULL 1 231 9 NULL 1 332 10 NULL 1 133 10 NULL 1 234 10 NULL 1 335 6 Pathology/Histology 1 236 11 Fellowship Adjustments 1 137 2 Maintenance 1 1 Table: tblBMS_Data_rel_BudgetCategoriesKeyID ProfileID CategoryID SubCategoryID Description BudgetYear Value1 Value160453 33071 11 36 Fellowship Adjustment 2007 0 060454 33071 4 4 Charles River 2007 0 060455 33071 4 1 Purchases 2007 0 060456 33071 4 2 Technical Services 2007 0 060457 33071 4 3 Holding 2007 0 060458 33071 4 5 Contracts 2007 0 060459 33071 5 6 Travel (Budget Total) 2007 25015 060460 33071 5 7 Training (Budget Total) 2007 17915 060461 33071 5 8 Patient Travel 2007 0 060462 33071 6 21 S&S (Budget Total) 2007 165515 060463 33071 6 35 Pathology/Histology 2007 0 060464 33071 1 11 Clincal Protocols 2007 0 060465 33071 2 28 Personnel Services 2007 0 060466 33071 2 37 Maintenance 2007 0 060467 33071 2 15 New COntracts 2007 120000 1000060468 33071 3 20 Discretionary Funding 2007 0 060469 33071 8 23 FTE 2007 0 060470 33071 8 24 Non-FTE 2007 0 060471 33071 8 25 SAIC, Orkand 2007 0 060472 33071 8 26 Students 2007 0 060473 33071 8 27 SV/GR 2007 0 075472 33071 9 29 898765635 2007 62000 075473 33071 9 30 343535346 2007 25000 075474 33071 9 31 354324234 2007 1500 075475 33071 10 32 2007 0 075476 33071 10 33 2007 0 075477 33071 10 34 2007 0 0 Here are the desired results...KeyID Category CategoryID ShortDescription SubCategoryID ProfileID Value1 Value260464 Clinical Protocols 1 NULL 11 33071 0 0NULL 1 NULL 12 NULL NULL NULLNULL 1 NULL 13 NULL NULL NULL60466 Contracts 2 Maintenance 37 33071 0 060465 2 Personnel Services 28 33071 0 0NULL 2 NULL 14 NULL NULL NULL60467 2 NULL 15 33071 120000 10000NULL 2 NULL 16 NULL NULL NULLNULL CSE 3 NULL 17 NULL NULL NULLNULL 3 NULL 18 NULL NULL NULLNULL 3 NULL 19 NULL NULL NULL60468 3 Discretionary Funding 20 33071 0 060454 Animal Costs 4 Charles River 4 33071 0 060455 4 Purchases 1 33071 0 060456 4 Technical Services 2 33071 0 060457 4 Holding 3 33071 0 060458 4 Contracts 5 33071 0 060459 Travel & Training 5 Travel (Budget Total) 6 33071 25015 060460 5 Training (Budget Total)7 33071 17915 060461 5 Patient Travel 8 33071 0 060462 Supplies & Services 6 S&S (Budget Total) 21 33071 165515 060463 6 Pathology/Histology 35 33071 0 0NULL 6 NULL 9 NULL NULL NULLNULL 6 NULL 10 NULL NULL NULL60469 Personnel Ceilings 8 FTE 23 33071 0 060470 8 Non-FTE 24 33071 0 060471 8 SAIC, Orkand 25 33071 0 060472 8 Students 26 33071 0 060473 8 SV/GR 27 33071 0 075472 CANS 9 NULL 29 33071 62000 075473 9 NULL 30 33071 25000 075474 9 NULL 31 33071 1500 075475 Center Numbers 10 NULL 32 33071 0 075476 10 NULL 33 33071 0 075477 10 NULL 34 33071 0 060453 Budget Adjustments 11 Fellowship Adjustments 36 33071 0 0 Basically the results should display all the Categorys and subcategorys even if an entry doesn't exist in the tblBMS_Data_rel_BudgetCategories table.Also as it shows in the grid I would like to only display the category for the first record of the results. I want to display all the options as a grid to display to the user and bind to a grid in asp.net.Thanks |
 |
|
JimAmigo
Posting Yak Master
119 Posts |
Posted - 2007-06-25 : 14:11:58
|
The tables info didn't help? |
 |
|
PSamsig
Constraint Violating Yak Guru
384 Posts |
Posted - 2007-06-26 : 01:59:43
|
[code]SELECT d.KeyID ,Category = c.ShortDescription ,c.CategoryID ,sc.ShortDescription ,sc.SubCategoryID ,d.ProfileID ,d.Value1 ,d.Value2FROM tblBMS_BudgetCategories cINNER JOIN tblBMS_BudgetCategories_Subcategories scON sc.CategoryID = c.CategoryID AND sc.Active = 1LEFT OUTER JOIN tblBMS_Data_rel_BudgetCategories dON d.CategoryID = c.CategoryID AND d.SubCategoryID = sc.SubCategoryIDWHERE c.Active = 1ORDER BY c.CategoryID, sc.DisplayOrder[/code]quote: Also as it shows in the grid I would like to only display the category for the first record of the results.
That you handle in the application, it will kill the poor server to do it there. -- If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime. |
 |
|
JimAmigo
Posting Yak Master
119 Posts |
Posted - 2007-06-26 : 09:47:02
|
quote: Originally posted by PSamsig
SELECT d.KeyID ,Category = c.ShortDescription ,c.CategoryID ,sc.ShortDescription ,sc.SubCategoryID ,d.ProfileID ,d.Value1 ,d.Value2FROM tblBMS_BudgetCategories cINNER JOIN tblBMS_BudgetCategories_Subcategories scON sc.CategoryID = c.CategoryID AND sc.Active = 1LEFT OUTER JOIN tblBMS_Data_rel_BudgetCategories dON d.CategoryID = c.CategoryID AND d.SubCategoryID = sc.SubCategoryIDWHERE c.Active = 1ORDER BY c.CategoryID, sc.DisplayOrder quote: Also as it shows in the grid I would like to only display the category for the first record of the results.
That you handle in the application, it will kill the poor server to do it there.-- If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.
Thanks for the effort, but when I run the query it only returns subcategories where the specific record has data. In the grid I need to display ALL 36 subcategorys as an option for the user to change/enter data. Your query returned 25 of the 36 which are sub categorys the record has data for.I tried changing the OUTER Join to RIGHT but got the same results. |
 |
|
PSamsig
Constraint Violating Yak Guru
384 Posts |
Posted - 2007-06-26 : 09:55:21
|
It has to be a LEFT join, did you by any chance add 'd.profileid = 34129' to the WHERE?-- If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime. |
 |
|
JimAmigo
Posting Yak Master
119 Posts |
Posted - 2007-06-26 : 14:37:46
|
Yes I did. The results have to be for one individual. |
 |
|
PSamsig
Constraint Violating Yak Guru
384 Posts |
Posted - 2007-06-26 : 15:49:49
|
You misplaced it then, you have to add in this line, as 'ON d.CategoryID = c.CategoryID AND d.SubCategoryID = sc.SubCategoryID AND d.profileid = 34129'.If you put it in the where clause, then you limit the reslut set to only include row where d.profileid has the given value, it wont be the case where there is no coresponding entry from tblBMS_Data_rel_BudgetCategories, in those cases d.profileid will be NULL (along with all other coloumns from that table).If you insist on putting it in the WHERE clause, then you can write it as: 'WHERE c.Active = 1 AND (d.profileid IS NULL OR d.profileid = 34129).-- If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime. |
 |
|
JimAmigo
Posting Yak Master
119 Posts |
Posted - 2007-06-28 : 10:19:43
|
That seems to have done it.. Pure Genius.. thanks for your help! |
 |
|
|
|
|
|
|