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 |
shahid00704
Starting Member
11 Posts |
Posted - 2009-12-15 : 08:09:51
|
I have Five tables and i want to get result from these five tables providing some checks.Site----------------------------SiteID int,SiteTypeID int,PowerTypeID int,LocationID int,SiteCode nvarchar(500)Sites_SiteType-----------------------------ID int,Type nvarchar(500)Sites_PowerType-----------------------------ID int,Type nvarchar(500)SiteTypeRenewal-----------------------------RenewalID int,SiteTypeId int,SiteIDPowerTypeRenewal-----------------------------RenewalID int,PowerTypeId int,SiteIDSite Data----------------SiteID SiteTypeID PowerTypeID LocationID SiteCode1 10 3 100 xxx2 20 1 200 yyy3 30 2 300 zzz4 30 1 100 xxxSites_siteType Data----------------------------ID Type10 RoofTop20 Transformer30 GreenFieldSiteTypeRenewal----------------------------RenewalID SiteTypeID SiteID1 20 12 30 23 10 34 10 25 20 3Sites_PowerType Table Data----------------------------ID Type1 Generator2 T.VStation3 TransformerPowerTypeRenewal----------------------------RenewalID PowerTypeID SiteID1 2 12 3 23 1 34 1 25 2 3here i want to check in the renewals(SiteTypeRenewal and PowerTypeRenewal) tables if there are records for a particular (SiteID) then extract maximum(last) record based on SiteID and display Type of Site_siteType and Site_PowerType by inner joining from renewal table. here if SiteID=3, it should give me LocationID=300,SiteCode=zzz SiteType=Transformer and PowerType=T.VStation.else check in the Site Table whether that SiteID is available or not if available extract that record joining Sites_SiteType and Site_PowerType and display it ex here for this if SiteID=4, it should give me LocationID=100,SiteCode=xxx SiteType=GreenField and PowerType=Generator.my final result looks like this...for SiteID=3SiteID LocationID SiteCode SiteType PowerType3 300 zzz Transformer T.VStationfor SiteID=4 which is not available in renewals tablesSiteID LocationID SiteCode SiteType PowerType4 100 xxx GreenField Generator |
|
vikky
Yak Posting Veteran
54 Posts |
Posted - 2009-12-15 : 09:04:13
|
HI,declare @Site table(SiteID int,SiteTypeID int,PowerTypeID int,LocationID int,SiteCode nvarchar(500))insert into @Site (SiteID, SiteTypeID, PowerTypeID, LocationID, SiteCode)select 1, 10, 3, 100, 'xxx' union all select 2, 20, 1, 200, 'yyy' union all select 3, 30, 2, 300, 'zzz' union all select 4, 30, 1, 100, 'xxx' declare @Sites_SiteType table(ID int,Type nvarchar(500))insert into @Sites_siteType(ID, Type)select 10, 'RoofTop' union all select 20, 'Transformer' union all select 30, 'GreenField' declare @Sites_PowerType table(ID int,Type nvarchar(500) )insert into @Sites_PowerType (ID, Type)select 1, 'Generator' union all select 2 ,'T.VStation' union all select 3, 'Transformer'declare @SiteTypeRenewal table(RenewalID int,SiteTypeId int,SiteID Int )insert into @SiteTypeRenewal(RenewalID ,SiteTypeID, SiteID)select 1, 20, 1 union all select 2, 30, 2 union all select 3, 10,3 union all select 4, 10,2 union all select 5, 20, 3declare @PowerTypeRenewal table (RenewalID int,PowerTypeId int,SiteID int)insert into @PowerTypeRenewal (RenewalID, PowerTypeID, SiteID)select 1 ,2 ,1 union all select 2 ,3 ,2 union all select 3 ,1 ,3 union all select 4 ,1, 2 union all select 5 ,2, 3DECLARE @siteid INTSELECT @siteid = 3SELECT s.siteid,LocationID,SiteCode,ISNULL(st1.type,st.type) AS SiteType,ISNULL(pt1.type,pt.type)FROM @SITE AS SINNER JOIN @Sites_siteType AS ST ON s.SiteTypeID = st.IDINNER JOIN @Sites_PowerType AS pt ON s.powertypeid = pt.idLEFT JOIN ( SELECT TOP 1 siteid,type FROM @SiteTypeRenewal INNER JOIN @Sites_siteType on id = SiteTypeID WHERE SITEID = @siteid ORDER BY RenewalID DESC ) AS st1 ON s.siteid = st1.SiteIDLEFT JOIN ( SELECT TOP 1 siteid,type from @PowerTypeRenewal INNER JOIN @Sites_siteType on id = powerTypeID WHERE SITEID = @siteid ORDER BY RenewalID DESC ) AS pt1 ON s.siteid = pt1.SiteIDWHERE s.siteid = @siteidThanks,vikky. |
 |
|
shahid00704
Starting Member
11 Posts |
Posted - 2009-12-15 : 09:09:30
|
Hi Vikky,First of all thanks for ur Reply.Here i am unable to understand what u r doing?? can u explain me what u r doing...Thank you. |
 |
|
vikky
Yak Posting Veteran
54 Posts |
Posted - 2009-12-15 : 09:22:52
|
hi,r u get correct result or not ?first i declared the @siteid.1) i select the values form sites and then joined with the @Sites_siteType AND @Sites_PowerType.2)Then i getted the resent renevalsites and renevalpowers for the given siteid = ( 3 or 4) in subquery.3) i joined the subquery subquery with siteids.4) in select isnull(st1.type,st.type) AS SiteType,ISNULL(pt1.type,pt.type)Gives the if avalable recent reneval other wise gets which entries available in table.r u understand or not ?Thanks,vikky. |
 |
|
shahid00704
Starting Member
11 Posts |
Posted - 2009-12-15 : 09:44:22
|
Hello vicky,I got ur points. But the thing is i have to manually select data into @Site,@Site_SiteType,@Site_PowerType,@RenewalSite,@RenewalPower... Is there any way by which it automatically selects data into these temp tables... because if i have large amount of data then it is impossible.Please reply. |
 |
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-12-15 : 10:37:24
|
Those are just table definitions (sample data for testing) that he has built...use the query alone and replace all the @tables with your actual table names.If you face any issues with the query, post back. |
 |
|
shahid00704
Starting Member
11 Posts |
Posted - 2009-12-15 : 12:27:15
|
Hello Vijayisonly and Vicky.I tried using ur code but getting errorsdeclare @New_Site table(SiteID int,SiteType int,PowerType int,LocationID int,SiteCode nvarchar(500))insert into @New_Site(SiteID, SiteType, PowerType, LocationID, SiteCode)select SiteID,SiteType,PowerType,LocationID,SiteCode from New_SiteDeclare @Site_SiteTypes table(ID int,Type nvarchar(200))insert into @Site_SiteTypes(ID, Type)Select ID,Type from Site_SiteTypesdeclare @Sites_PowerTypes table(ID int,Type nvarchar(500))insert into @Sites_PowerTypes(ID, Type)Select ID,Type from Sites_PowerTypesdeclare @Site_Renewal_SiteType table(SiteTypeRenewalID int,SiteTypeID int,SiteID Int )insert into @Site_Renewal_SiteType(SiteTypeRenewalID ,SiteTypeID, SiteID)select SiteTypeRenewalID ,SiteTypeID, SiteID from Site_Renewal_SiteTypedeclare @Site_Renewal_PowerType table (PowerTypeRenewalID int,PowerTypeID int,SiteID int)insert into @Site_Renewal_PowerType (PowerTypeRenewalID, PowerTypeID, SiteID)select PowerTypeRenewalID, PowerTypeID, SiteID from Site_Renewal_PowerTypeDECLARE @siteid INTSELECT S.SiteId,S.LocationID,S.SiteCode,ISNULL(st1.type,st.type) AS SiteType,ISNULL(pt1.type,pt.type)FROM @New_Site AS SINNER JOIN @Site_SiteTypes AS ST ON S.SiteType = ST.IDINNER JOIN @Sites_PowerTypes AS PT ON S.PowerType = PT.IDLEFT JOIN ( SELECT TOP 1 SiteID,SiteTypeID FROM @Site_Renewal_SiteTypeINNER JOIN @Site_SiteTypes on ST.ID = SiteTypeID WHERE SITEID = @siteid ORDER BY SiteTypeRenewalID DESC ) AS st1 ON s.SiteId = st1.SiteIDLEFT JOIN ( SELECT TOP 1 SiteID,PowerTypeID from @Site_Renewal_PowerTypeINNER JOIN @Sites_PowerTypes on PT.ID = PowerTypeID WHERE SITEID = @siteid ORDER BY PowerTypeRenewalID DESC ) AS pt1 ON s.SiteId = pt1.SiteIDWHERE S.SiteID = @siteidthe errors areMsg 4104, Level 16, State 1, Line 35The multi-part identifier "ST.ID" could not be bound.Msg 4104, Level 16, State 1, Line 35The multi-part identifier "PT.ID" could not be bound.Msg 207, Level 16, State 1, Line 35Invalid column name 'type'.Msg 207, Level 16, State 1, Line 35Invalid column name 'type'.there is no column with the name type in Site_Renewal_SiteType and Site_renewal_PowerType so it gives error. and i dont know y the multipart identifier cannot be bound error. please reply as soon as possible.Thanks,Shahid |
 |
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-12-15 : 15:05:44
|
Just execute this...and let me know if the results are ok...Make sure you are using your correct table names and column names (also check for case sensitivity with column names..looks like you have a case sensitive collation.SELECT a.SiteID, a.LocationID,a.SiteCode,coalesce(st1.Type,st.Type) as SiteType,coalesce(pt1.Type,pt.Type)FROM [Site] ainner join Sites_SiteType st on a.SiteTypeID = st.IDinner join Sites_PowerType pt on a.PowerTypeID = pt.IDleft join (select t1.SiteID,t2.Type from(select row_number() over(partition by SiteID order by RenewalID desc) as seq, * from SiteTypeRenewal) t1 inner join Sites_SiteType t2 on t1.SiteTypeID = t2.ID where t1.seq = 1)st1 on a.SiteID = st1.SiteIDleft join (select t3.SiteID,t4.Type from(select row_number() over(partition by SiteID order by RenewalID desc) as seq, * from PowerTypeRenewal) t3 inner join Sites_PowerType t4 on t3.PowerTypeID = t4.ID where t3.seq = 1)pt1 on a.SiteID = pt1.SiteID |
 |
|
shahid00704
Starting Member
11 Posts |
Posted - 2009-12-16 : 02:34:29
|
Hello Vijay,Thanks Man... I really Thank u from bottom of my heart... u dont know what u did for me.. i was really very much tensed about this query because i dont know sql queries and procedures very well and no body is here to assist me. I also thank to Mr.Vicky from bottom of my heart who also supported me.Thanks SqlTeam.Shahid. |
 |
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-12-16 : 09:51:21
|
Np. You're welcome |
 |
|
shahid00704
Starting Member
11 Posts |
Posted - 2010-01-05 : 13:31:05
|
Hello All I want a procedure based on some criteria.Its very Urgent. i may lose my job if dont get this.I have Five Table. here are the details.Site----------------------------SiteID int,SiteTypeID int,PowerTypeID int,LocationID int,SiteCode nvarchar(500),creationdate datetimeSites_SiteType-----------------------------ID int,Type nvarchar(500)Sites_PowerType-----------------------------ID int,Type nvarchar(500)SiteTypeRenewal-----------------------------RenewalID int,SiteTypeId int,SiteID,creationdatePowerTypeRenewal-----------------------------RenewalID int,PowerTypeId int,SiteID,creationdateSite Data----------------SiteID SiteTypeID PowerTypeID LocationID SiteCode1 10 3 100 xxx2 20 1 200 yyy3 30 2 300 zzz4 30 1 100 xxxSites_siteType Data----------------------------ID Type10 RoofTop20 Transformer30 GreenFieldSiteTypeRenewal----------------------------RenewalID SiteTypeID SiteID creationdate1 20 1 31/12/20092 30 1 02/1/2010 3 10 1 05/1/20104 10 1 15/1/20105 20 2 05/1/2010Sites_PowerType Table Data----------------------------ID Type1 Generator2 T.VStation3 TransformerPowerTypeRenewal----------------------------RenewalID PowerTypeID SiteID creationdate1 2 1 31/12/20092 3 1 02/1/20103 1 1 05/1/20104 1 1 15/1/20105 2 2 05/1/2010here i want to check in the renewals(SiteTypeRenewal and PowerTypeRenewal) tables if there are records for a particular (SiteID) if SiteID is available in the renewals table then go for check for date column, if the current date matches any column of creationdate then extract that record from the renewals tables else extract the creationdate column record which is just less than the currentdate and max of all old dates. here if i say for siteID=1 and creationdate=5/1/2010, it should give the siteID=1,LocationID=100,SiteCode=xxx,SiteType=RoofTop and powertype=Generator.assume if siteid=1 and currentdate=04/1/2010 then it should go for first siteID check and if available in renewals tables then check for dates. here in renewals we dont have creationdate=04/1/2010 then it should extract the record siteid=1,LocationID=100,SiteCode=xxx,SiteType=GreenField and powertype=Transformer.All checking in Renewals tables only.and if there is no record present in the renewals table based on SiteID then extract the record which is in Site Table.for example if i say siteID=3siteid=3,LocationID=300,SiteCode=zzz,SiteType=GreenField and powertype=T.VStationmy final result looks like this...for SiteID=1SiteID LocationID SiteCode SiteType PowerType1 100 xxx RoofTop Generatorfor SiteID=3 which is not available in renewals tablesSiteID LocationID SiteCode SiteType PowerType 3 300 zzz GreenField T.VStationHope to get a postitive reply.Thanks in advance. Please Help.I need it very urgently. Please. |
 |
|
|
|
|
|
|