Inquiry on finding if a fields data is unique for the recordset

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 2
Reputation: Zangow is an unknown quantity at this point 
Solved Threads: 0
Zangow Zangow is offline Offline
Newbie Poster

Inquiry on finding if a fields data is unique for the recordset

 
0
  #1
Mar 6th, 2008
Hello all. This is my first post. I'm a software engineer with a general knowledge of SQL, but am no where close to an expert, let alone an intermediate.

My current issue is I'm working on a query which combines 2 tables:
TableA
UniqueID Auto Increment
ProjectName String
<Extra Data>
TableB
UniqueID Auto Increment
ForeignKey Points to TableA.UniqueID - NOT Unique
<Extra Data>

More than one record (ForeignKey) in TableB can point to the same record (UniqueID) in TableA.

I have a web page that's printing out all the entries in TableB in a simple table format. However, the <Extra Data> needs to be ignored and the cells "blacked out" if the ForeignKey occurs more than once in TableB. So basically I'm looking for a way to add to my SELECT statement so when I start reading through the recordset in ASP, there's a column for each record to say how many times that ForeignKey exists in TableB.

I hope this makes sense as I've had trouble trying to put it into words. Any help is greatly appreciated :-D
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Inquiry on finding if a fields data is unique for the recordset

 
0
  #2
Mar 7th, 2008
I'll read your question again but can you please provide us with sample input and the output, it'd help me
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Inquiry on finding if a fields data is unique for the recordset

 
0
  #3
Mar 7th, 2008
However, the <Extra Data> needs to be ignored and the cells "blacked out" if the ForeignKey occurs more than once in TableB
It should be occur more than once in table b as the relationship is 1-to-many!!
I think you must support me with inputs and output..
Last edited by Ramy Mahrous; Mar 7th, 2008 at 10:21 am.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 9
Reputation: space1000 is an unknown quantity at this point 
Solved Threads: 1
space1000 space1000 is offline Offline
Newbie Poster

Re: Inquiry on finding if a fields data is unique for the recordset

 
0
  #4
Mar 7th, 2008
Please suplly the original query. I find it a bit hard to imagine what the result must be.

The possible sollution solution is a "select ProjectName,extra_data) with a union, where the first sql statement should be "having count(A.UniqueID = 1) and the second should have "select ProjectName ,count(*) from .... having count(A.UniqueID) > 1"
Last edited by space1000; Mar 7th, 2008 at 10:28 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 31
Reputation: AaronASterling is an unknown quantity at this point 
Solved Threads: 5
AaronASterling's Avatar
AaronASterling AaronASterling is offline Offline
Light Poster

Re: Inquiry on finding if a fields data is unique for the recordset

 
0
  #5
Mar 7th, 2008
You want something like:
  1. SELECT columnyouwant1,...,columnyouwantn, rowcount = COUNT(B.ForeignKey)
  2. FROM TableB AS B
  3. GROUPBY B.ForeignKey

Load that into a temporary table. lets call it T. then you can select based on rowcount
to handle the two cases differently. You may be able to do it in one query but this will be faster then using cursors
Last edited by AaronASterling; Mar 7th, 2008 at 12:01 pm. Reason: made a typo
Aaron Sterling
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 9
Reputation: space1000 is an unknown quantity at this point 
Solved Threads: 1
space1000 space1000 is offline Offline
Newbie Poster

Re: Inquiry on finding if a fields data is unique for the recordset

 
0
  #6
Mar 7th, 2008
@AaronASterling

dont' know about that, That is a static solution and should be recalculated every time.
Nil volentibus arduum!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 31
Reputation: AaronASterling is an unknown quantity at this point 
Solved Threads: 5
AaronASterling's Avatar
AaronASterling AaronASterling is offline Offline
Light Poster

Re: Inquiry on finding if a fields data is unique for the recordset

 
0
  #7
Mar 7th, 2008
Originally Posted by space1000 View Post
@AaronASterling

dont' know about that, That is a static solution and should be recalculated every time.

It seems to be exactly what the original poster wanted. What exactly do you mean by 'its a static solution?' Wouldn't any query need to be recalculated every time to ensure accurate real time information? based on rereading the original post, he/she wouldn't even need to use a temporary table. the result my query gives returns the extra column asked for for the asp processing.
Aaron Sterling
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 2
Reputation: Zangow is an unknown quantity at this point 
Solved Threads: 0
Zangow Zangow is offline Offline
Newbie Poster

Re: Inquiry on finding if a fields data is unique for the recordset

 
0
  #8
Mar 7th, 2008
Thanks everyone. I ended up using Aaron's suggestion and doing a quick query in the beginning that stores the column count in an array that I can access later while printing out the table.

I can see how the split second could effect a system that is constantly updating since the rows may not match up by the 2nd call. And sure, there's ways to make sure they do match up, but I decided in this case where data is rarely updated and also not viewed very often, it's not worth the headache at this time. It is working as I wanted it to though, so thanks again!
Zangow
Software Engineer
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 865 | Replies: 7
Thread Tools Search this Thread



Tag cloud for MS SQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC