Author |
Topic |
no_mike
Starting Member
5 Posts |
Posted - 2012-09-27 : 13:28:59
|
This query returns what utilities are to be listed depending on a resident and works fine:SELECT S.SDESC AS 'Service'FROM UTALLOW A LEFT JOIN UTALLOWDET AD ON A.HMY = AD.HUTALLOWLEFT JOIN UTILSERV S ON AD.HUTILSERV = S.HMYLEFT JOIN H8UNIT HU ON A.HMY = HU.HUTSCHEDULELEFT JOIN UNIT U ON HU.HUNIT = U.HMYLEFT JOIN H8SUMM HS ON U.HMY = HS.HUNITWHERE HS.HTENANT = @resident_idAND S.HMY IN (SELECT HUTILSERV FROM UTRESP R LEFT JOIN H8UNIT HU ON R.HH8UNIT = HU.HMY LEFT JOIN UNIT U ON HU.HUNIT = U.HMY LEFT JOIN H8SUMM HS ON U.HMY = HS.HUNIT WHERE HS.HTENANT = @resident_id GROUP BY HUTILSERV) GROUP BY S.SDESCORDER BY S.SDESC Results:Service--------------------------------------------------------------------------------Air ConditioningCooking-ElectricHeating-ElectricMonthly Electric Fee allowanceOther ElectricRangeetc....However, now I need to add a column indicating whether or not the owner is responsible for specific utilities. The BOWNER columns will always be either 0 or -1 (never NULL). When I add the JOIN to the UTRESP table it returns each service twice as below. What is the proper way to go about this? I'm still working on it but any guidance would be appreciated.SELECT S.SDESC AS 'Service',CASE WHEN R.BOWNER = 0 THEN 'No' ELSE 'Yes' END AS 'OWNER_RESP'FROM UTALLOW A LEFT JOIN UTALLOWDET AD ON A.HMY = AD.HUTALLOWLEFT JOIN UTILSERV S ON AD.HUTILSERV = S.HMYLEFT JOIN UTRESP R ON S.HMY = R.HUTILSERVLEFT JOIN H8UNIT HU ON A.HMY = HU.HUTSCHEDULELEFT JOIN UNIT U ON HU.HUNIT = U.HMYLEFT JOIN H8SUMM HS ON U.HMY = HS.HUNITWHERE HS.HTENANT = @resident_idAND S.HMY IN (SELECT HUTILSERV FROM UTRESP R LEFT JOIN H8UNIT HU ON R.HH8UNIT = HU.HMY LEFT JOIN UNIT U ON HU.HUNIT = U.HMY LEFT JOIN H8SUMM HS ON U.HMY = HS.HUNIT WHERE HS.HTENANT = @resident_id GROUP BY HUTILSERV) GROUP BY S.SDESC,R.BOWNERORDER BY S.SDESC Results:Service OWNER_RESP-------------------------------------------------------------------------------- ----------Air Conditioning YesAir Conditioning NoCooking-Electric YesCooking-Electric NoHeating-Electric YesHeating-Electric Noetc...Also, if anybody sees a way that it would be possible to remove the subquery I'm all ears. Just getting started on this one from a DB that's been around for a while and I'm new to.Thanks,Mike |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
no_mike
Starting Member
5 Posts |
Posted - 2012-09-27 : 16:35:07
|
Thanks, Tara. I edited my post to reflect your observation. Any suggestions on how to remedy my issue? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-27 : 16:49:24
|
quote: Originally posted by no_mike Thanks, Tara. I edited my post to reflect your observation. Any suggestions on how to remedy my issue?
so whats output that you're expecting?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
no_mike
Starting Member
5 Posts |
Posted - 2012-09-27 : 16:57:31
|
Service OWNER_RESP-------------------------------------------------------------------------------- ----------Air Conditioning YesCooking-Electric YesHeating-Electric Noetc...Basically, it should be either yes or no for the OWNER_RESP field as only one value is given (either 0 or -1) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-27 : 16:59:10
|
so whats basis for choosing a value? how do you determine whether its yes or a no to be linked to a service? do you've a mapping table which contains information on shich services a owner has responsibility on?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
no_mike
Starting Member
5 Posts |
Posted - 2012-09-27 : 17:11:08
|
Yes...In the H8SUMM table there is a TENANT. The tenant row has a column linking to the UNIT table. Then the UNIT table has a FK to the H8UNIT table. In the H8UNIT table there is a field HUTSCHEDULE which determines the utility "schedule" that a this unit is on. Depending on the schedule, specific types of utilities are available to possibly be paid for by the landlord. The UTRESP table is the key. Each unit can be listed multiple times with a FK to the utility/service that is going to be paid by either the landlord or tenant. For exampleUnit __ Service581 __ 2581 __ 4581 __ 5581 __ 9etc....Sorry, this is really hard to explain and I fear that this may sound like a bunch of jumbled up words. Let me know if this makes no sense and to forget about it, you would like more description, or if you have any ideas. SELECT S.SDESC AS 'Service',CASE WHEN R.BOWNER = 0 THEN 'No' ELSE 'Yes' END AS 'OWNER_RESP'FROM UTALLOW A LEFT JOIN UTALLOWDET AD ON A.HMY = AD.HUTALLOWLEFT JOIN UTILSERV S ON AD.HUTILSERV = S.HMYLEFT JOIN UTRESP R ON S.HMY = R.HUTILSERVLEFT JOIN H8UNIT HU ON A.HMY = HU.HUTSCHEDULELEFT JOIN UNIT U ON HU.HUNIT = U.HMYLEFT JOIN H8SUMM HS ON U.HMY = HS.HUNITWHERE HS.HTENANT = @resident_idAND S.HMY IN (SELECT HUTILSERV FROM UTRESP R LEFT JOIN H8UNIT HU ON R.HH8UNIT = HU.HMY LEFT JOIN UNIT U ON HU.HUNIT = U.HMY LEFT JOIN H8SUMM HS ON U.HMY = HS.HUNIT WHERE HS.HTENANT = @resident_id GROUP BY HUTILSERV) GROUP BY S.SDESC,R.BOWNERORDER BY S.SDESC |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
no_mike
Starting Member
5 Posts |
Posted - 2012-09-27 : 17:38:50
|
Thanks for taking a look at it. I run into problems like these somewhat often working with these old 3rd party DB's that I have no point of reference with....I'm going to try rewriting it from scratch a couple of times and then if I still can't figure it out I will give your posting recommendation a shot. Thanks a lot though! |
|
|
|