473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help with MS SQL Query

Hello,

I need some help with a sql query. Here's my scenario:

I have a table like this:

ID | Value

10 | 45
10 | 46
10 | 47
11 | 45
11 | 46
12 | 45

What I need to do is create a stored procedure that would return mutual exclusive set of values for any number of id's passed in as parameters.

For instance:

Parameters passed in: id = 10, id = 11

Return set:

45
46

(47 would not be in the set, since it's not common to both 10 and 11).

Example 2.

Parameters passed in: id = 10, id = 11, id = 12

Return set:

45

(that's the only number all three IDs have in common).

I do not know the number of IDs that should be passed in since that depends on user selected inputs.

Any help would be GREATLY appreciated.

Thank You
Aug 5 '07 #1
4 1287
Any help on this? Anybody?

Thanks
Aug 6 '07 #2
ck9663
2,878 Expert 2GB
Hello,

I need some help with a sql query. Here's my scenario:

I have a table like this:

ID | Value

10 | 45
10 | 46
10 | 47
11 | 45
11 | 46
12 | 45

What I need to do is create a stored procedure that would return mutual exclusive set of values for any number of id's passed in as parameters.

For instance:

Parameters passed in: id = 10, id = 11

Return set:

45
46

(47 would not be in the set, since it's not common to both 10 and 11).

Example 2.

Parameters passed in: id = 10, id = 11, id = 12

Return set:

45

(that's the only number all three IDs have in common).

I do not know the number of IDs that should be passed in since that depends on user selected inputs.

Any help would be GREATLY appreciated.

Thank You
try something like this:

select value from YourTable
where ID in (@para1, @para2...and so on)
group by value having count(*) > 1
Aug 6 '07 #3
azimmer
200 Expert 100+
A complete one:
Expand|Select|Wrap|Line Numbers
  1. create procedure foundinall(@idcsv as varchar(200))
  2. as
  3. declare @pos as int, @lastpos as int
  4. declare @temptbl TABLE (tmpid int)
  5.  
  6. set @lastpos = 1
  7. set @pos = charindex(',',@idcsv)
  8. while (@pos > 0)
  9. begin
  10.     insert into @temptbl values (substring(@idcsv,@lastpos,@pos-@lastpos))
  11.     set @lastpos=@pos+1
  12.     set @pos=charindex(',',@idcsv,@lastpos)
  13. end
  14. insert into @temptbl values (substring(@idcsv,@lastpos,len(@idcsv)-@lastpos+1))
  15.  
  16. select val from idvalue
  17. where ID in (select tmpid from @temptbl)
  18. group by val having count(*) = (select count(*) from @temptbl)
  19.  
  20. go
  21.  
Usage: exec foundinall '10,11,12'
Aug 6 '07 #4
Thank You so much AZIMMER!

Your code worked without a glitch on the first try. Exactly what I needed and your code does it perfectly. Many thanks.
Aug 10 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
5
by: Steve Patrick | last post by:
Hi All You guys are my last hope, despite spending money on books and hours reading them I still can not achieve the results I need. I have designed a database in Access 2000 based on 1 table,...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
3
by: mcmahonb | last post by:
Hey people... I've been searching this forum for a few hours and even though this topic has been went over from many different angles; I cannot seem to figure out how to make things work on my...
4
by: n | last post by:
Hello! Here is a problem I hope you can point me to a solution. It Problem: A teacher needs to know which lesson to teach. A school has a curriculum with 26 lessons, A-Z. For a given class,...
47
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.