|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Query Help, Efficient Exclusionhave a duplicate staffNames as long as the region is different. Please note that, I can't correct the problem of the same staffName having additional entries with the same region code, there are good reasons this is not ever going to be fixed. -- How do I exclude a staffName when the staffName and Region already exist in my staff_rank table, but allow duplicate staffNames as long as the region is different? create table #staff (staffName varchar(40) ,SalesCount int ,Region varchar(1)) create table #Staff_Rank (staffName varchar(40) ,SalesCount int ,Region varchar(1) ,Rank int) create table #regions (staffName varchar(40) ,Region varchar(1)) insert #regions select 'Leslie Smith' ,'W' insert #regions select 'Martin Alme' ,'W' insert #regions select 'Rosa Minproc' ,'E' insert #regions select 'Lasiter Morgan' ,'E' insert #regions select 'Ronnie Hams' ,'W' insert #regions select 'Lasiter Morgan' ,'W' insert #regions select 'Martin Alme' ,'W' insert #staff select 'Leslie Smith' ,21 ,'W' insert #staff select 'Martin Alme' ,12 ,'W' insert #staff select 'Rosa Minproc' ,28 ,'E' insert #staff select 'Lasiter Morgan' ,31 ,'E' insert #staff select 'Ronnie Hams' ,22 ,'W' insert #staff select 'Lasiter Morgan' ,9 ,'W' insert #staff select 'Martin Alme' ,1 ,'W' insert #staff_rank select s.staffname ,s.salescount ,r.region ,null from #staff s with(nolock) join #regions r with(nolock) on s.staffname = r.staffname and s.region = r.region select * from #staff_rank order by staffname drop table #staff drop table #Staff_Rank drop table #regions When do you need to exclude them? Do you have the query that you are trying
to modify? Show quote "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message news:epde6C3TGHA.4772@TK2MSFTNGP09.phx.gbl... >I need to have in my staff_rank table each staffname and region, but it's >OK to > have a duplicate staffNames as long as the region is different. > > Please note that, I can't correct the problem of the same staffName having > additional entries with the same region code, there are good reasons this > is not > ever going to be fixed. > > -- How do I exclude a staffName when the staffName and Region already > exist in > my staff_rank table, but allow duplicate staffNames as long as the region > is > different? > > create table #staff (staffName varchar(40) ,SalesCount int ,Region > varchar(1)) > create table #Staff_Rank (staffName varchar(40) ,SalesCount int ,Region > varchar(1) ,Rank int) > create table #regions (staffName varchar(40) ,Region varchar(1)) > > insert #regions select 'Leslie Smith' ,'W' > insert #regions select 'Martin Alme' ,'W' > insert #regions select 'Rosa Minproc' ,'E' > insert #regions select 'Lasiter Morgan' ,'E' > insert #regions select 'Ronnie Hams' ,'W' > insert #regions select 'Lasiter Morgan' ,'W' > insert #regions select 'Martin Alme' ,'W' > > insert #staff select 'Leslie Smith' ,21 ,'W' > insert #staff select 'Martin Alme' ,12 ,'W' > insert #staff select 'Rosa Minproc' ,28 ,'E' > insert #staff select 'Lasiter Morgan' ,31 ,'E' > insert #staff select 'Ronnie Hams' ,22 ,'W' > insert #staff select 'Lasiter Morgan' ,9 ,'W' > insert #staff select 'Martin Alme' ,1 ,'W' > > insert #staff_rank select s.staffname ,s.salescount ,r.region ,null > from #staff s with(nolock) > join #regions r with(nolock) on s.staffname = r.staffname and s.region = > r.region > > select * from #staff_rank order by staffname > > drop table #staff > drop table #Staff_Rank > drop table #regions > > insert #staff_rank
select distinct s.staffname ,s.salescount ,r.region ,null from #staff s with(nolock) join #regions r with(nolock) on s.staffname = r.staffname and s.region = r.region -- Show quote-oj "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message news:epde6C3TGHA.4772@TK2MSFTNGP09.phx.gbl... >I need to have in my staff_rank table each staffname and region, but it's >OK to > have a duplicate staffNames as long as the region is different. > > Please note that, I can't correct the problem of the same staffName having > additional entries with the same region code, there are good reasons this > is not > ever going to be fixed. > > -- How do I exclude a staffName when the staffName and Region already > exist in > my staff_rank table, but allow duplicate staffNames as long as the region > is > different? > > create table #staff (staffName varchar(40) ,SalesCount int ,Region > varchar(1)) > create table #Staff_Rank (staffName varchar(40) ,SalesCount int ,Region > varchar(1) ,Rank int) > create table #regions (staffName varchar(40) ,Region varchar(1)) > > insert #regions select 'Leslie Smith' ,'W' > insert #regions select 'Martin Alme' ,'W' > insert #regions select 'Rosa Minproc' ,'E' > insert #regions select 'Lasiter Morgan' ,'E' > insert #regions select 'Ronnie Hams' ,'W' > insert #regions select 'Lasiter Morgan' ,'W' > insert #regions select 'Martin Alme' ,'W' > > insert #staff select 'Leslie Smith' ,21 ,'W' > insert #staff select 'Martin Alme' ,12 ,'W' > insert #staff select 'Rosa Minproc' ,28 ,'E' > insert #staff select 'Lasiter Morgan' ,31 ,'E' > insert #staff select 'Ronnie Hams' ,22 ,'W' > insert #staff select 'Lasiter Morgan' ,9 ,'W' > insert #staff select 'Martin Alme' ,1 ,'W' > > insert #staff_rank select s.staffname ,s.salescount ,r.region ,null > from #staff s with(nolock) > join #regions r with(nolock) on s.staffname = r.staffname and s.region = > r.region > > select * from #staff_rank order by staffname > > drop table #staff > drop table #Staff_Rank > drop table #regions > > Oh, Jaaa
Thank you.... I knew that I would have made it more complicated, using where not in(selects... JeffP.... Show quote "oj" <nospam_ojngo@home.com> wrote in message news:OOFpeX6TGHA.4492@TK2MSFTNGP09.phx.gbl... > insert #staff_rank > select distinct s.staffname ,s.salescount ,r.region ,null > from #staff s with(nolock) > join #regions r with(nolock) on s.staffname = r.staffname and s.region = > r.region > > -- > -oj > > > "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message > news:epde6C3TGHA.4772@TK2MSFTNGP09.phx.gbl... > >I need to have in my staff_rank table each staffname and region, but it's > >OK to > > have a duplicate staffNames as long as the region is different. > > > > Please note that, I can't correct the problem of the same staffName having > > additional entries with the same region code, there are good reasons this > > is not > > ever going to be fixed. > > > > -- How do I exclude a staffName when the staffName and Region already > > exist in > > my staff_rank table, but allow duplicate staffNames as long as the region > > is > > different? > > > > create table #staff (staffName varchar(40) ,SalesCount int ,Region > > varchar(1)) > > create table #Staff_Rank (staffName varchar(40) ,SalesCount int ,Region > > varchar(1) ,Rank int) > > create table #regions (staffName varchar(40) ,Region varchar(1)) > > > > insert #regions select 'Leslie Smith' ,'W' > > insert #regions select 'Martin Alme' ,'W' > > insert #regions select 'Rosa Minproc' ,'E' > > insert #regions select 'Lasiter Morgan' ,'E' > > insert #regions select 'Ronnie Hams' ,'W' > > insert #regions select 'Lasiter Morgan' ,'W' > > insert #regions select 'Martin Alme' ,'W' > > > > insert #staff select 'Leslie Smith' ,21 ,'W' > > insert #staff select 'Martin Alme' ,12 ,'W' > > insert #staff select 'Rosa Minproc' ,28 ,'E' > > insert #staff select 'Lasiter Morgan' ,31 ,'E' > > insert #staff select 'Ronnie Hams' ,22 ,'W' > > insert #staff select 'Lasiter Morgan' ,9 ,'W' > > insert #staff select 'Martin Alme' ,1 ,'W' > > > > insert #staff_rank select s.staffname ,s.salescount ,r.region ,null > > from #staff s with(nolock) > > join #regions r with(nolock) on s.staffname = r.staffname and s.region = > > r.region > > > > select * from #staff_rank order by staffname > > > > drop table #staff > > drop table #Staff_Rank > > drop table #regions > > > > > > ya welcome. ;-)
-- Show quote-oj "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message news:ePfr8n8TGHA.4276@TK2MSFTNGP10.phx.gbl... > Oh, Jaaa > > Thank you.... > > I knew that I would have made it more complicated, using where not > in(selects... > > JeffP.... > > "oj" <nospam_ojngo@home.com> wrote in message > news:OOFpeX6TGHA.4492@TK2MSFTNGP09.phx.gbl... >> insert #staff_rank >> select distinct s.staffname ,s.salescount ,r.region ,null >> from #staff s with(nolock) >> join #regions r with(nolock) on s.staffname = r.staffname and s.region = >> r.region >> >> -- >> -oj >> >> >> "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message >> news:epde6C3TGHA.4772@TK2MSFTNGP09.phx.gbl... >> >I need to have in my staff_rank table each staffname and region, but >> >it's >> >OK to >> > have a duplicate staffNames as long as the region is different. >> > >> > Please note that, I can't correct the problem of the same staffName >> > having >> > additional entries with the same region code, there are good reasons >> > this >> > is not >> > ever going to be fixed. >> > >> > -- How do I exclude a staffName when the staffName and Region already >> > exist in >> > my staff_rank table, but allow duplicate staffNames as long as the >> > region >> > is >> > different? >> > >> > create table #staff (staffName varchar(40) ,SalesCount int ,Region >> > varchar(1)) >> > create table #Staff_Rank (staffName varchar(40) ,SalesCount int ,Region >> > varchar(1) ,Rank int) >> > create table #regions (staffName varchar(40) ,Region varchar(1)) >> > >> > insert #regions select 'Leslie Smith' ,'W' >> > insert #regions select 'Martin Alme' ,'W' >> > insert #regions select 'Rosa Minproc' ,'E' >> > insert #regions select 'Lasiter Morgan' ,'E' >> > insert #regions select 'Ronnie Hams' ,'W' >> > insert #regions select 'Lasiter Morgan' ,'W' >> > insert #regions select 'Martin Alme' ,'W' >> > >> > insert #staff select 'Leslie Smith' ,21 ,'W' >> > insert #staff select 'Martin Alme' ,12 ,'W' >> > insert #staff select 'Rosa Minproc' ,28 ,'E' >> > insert #staff select 'Lasiter Morgan' ,31 ,'E' >> > insert #staff select 'Ronnie Hams' ,22 ,'W' >> > insert #staff select 'Lasiter Morgan' ,9 ,'W' >> > insert #staff select 'Martin Alme' ,1 ,'W' >> > >> > insert #staff_rank select s.staffname ,s.salescount ,r.region ,null >> > from #staff s with(nolock) >> > join #regions r with(nolock) on s.staffname = r.staffname and s.region >> > = >> > r.region >> > >> > select * from #staff_rank order by staffname >> > >> > drop table #staff >> > drop table #Staff_Rank >> > drop table #regions >> > >> > >> >> > >
Other interesting topics
|
|||||||||||||||||||||||