Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,302 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,018 people online right now. Registration is fast and FREE... Join Now!




Comparing fields from two DB tables with CF

 
Reply to this topicStart new topic

Comparing fields from two DB tables with CF

jeffmc21
post 24 Sep, 2008 - 08:19 PM
Post #1


New D.I.C Head

*
Joined: 27 Nov, 2007
Posts: 46


My Contributions


Hey guys..

I have two database tables. I want to compare fields from tbl_one with fields from tbl_two and the output the resulting similarities.

So, I want to take table_one.field_one and compare it to table_two.field_one. If they match, I'd want to output something like "CORRECT", and if not "INCORRECT". Or even better might be to simply compare the 10 fields from table one to the corresponding ten fields in table two and then output the number of matching fields.

Any help?

This post has been edited by jeffmc21: 24 Sep, 2008 - 08:20 PM
User is offlineProfile CardPM

Go to the top of the page

sansclue
post 25 Sep, 2008 - 07:24 AM
Post #2


D.I.C Head

**
Joined: 21 Nov, 2007
Posts: 105



Thanked 7 times
My Contributions


quote name='jeffmc21' date='24 Sep, 2008 - 09:19 PM' post='423580']
Hey guys..

I have two database tables. I want to compare fields from tbl_one with fields from tbl_two and the output the resulting similarities.

So, I want to take table_one.field_one and compare it to table_two.field_one. If they match, I'd want to output something like "CORRECT", and if not "INCORRECT". Or even better might be to simply compare the 10 fields from table one to the corresponding ten fields in table two and then output the number of matching fields.

Any help?
[/quote]

How are you identifying matching records in the two tables? Do they have a shared UK (unique key)?

Also, how do you wish to handle cases where a record exists in table_one, but not in table_two? Or vice versa.
User is offlineProfile CardPM

Go to the top of the page

jeffmc21
post 28 Sep, 2008 - 08:52 PM
Post #3


New D.I.C Head

*
Joined: 27 Nov, 2007
Posts: 46


My Contributions


QUOTE(sansclue @ 25 Sep, 2008 - 08:24 AM) *

How are you identifying matching records in the two tables? Do they have a shared UK (unique key)?

Also, how do you wish to handle cases where a record exists in table_one, but not in table_two? Or vice versa.


The beginnings of my problem comes in the fact that I'm not sure what a UK does, is, or should be. However, I probably need to figure that out, as I'm sure it is something that will not only help here, but possibly other db issues in the future.

As for the cases where records exist in one table but not the other, that case should never exist, however, if that case did, I'd want to disregard that "set of results".

To be more specific about what I'm trying to do:
I want to match the results from column_4 of table_one against data from column_3 thru column_8 of each row in table_two.

See the attached diagram that makes more sense. (not to exact scale of db tables)


Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM

Go to the top of the page

sansclue
post 29 Sep, 2008 - 08:31 AM
Post #4


D.I.C Head

**
Joined: 21 Nov, 2007
Posts: 105



Thanked 7 times
My Contributions


QUOTE(jeffmc21 @ 28 Sep, 2008 - 09:52 PM) *

The beginnings of my problem comes in the fact that I'm not sure what a UK does, is, or should be. However, I probably need to figure that out, as I'm sure it is something that will not only help here, but possibly other db issues in the future.


A UK (unique key) is a value(s) that can be used to uniquely identify a record. For example, if you have a table of users and it contains ten different users, all named "John Smith". A unique key or ID would tell you which one was which.

In order to compare records from the two tables they need to have a key or something in common. Otherwise you would not know which two records to compare. So what key or values do these two tables have in common?

This post has been edited by sansclue: 29 Sep, 2008 - 08:32 AM
User is offlineProfile CardPM

Go to the top of the page

jeffmc21
post 30 Sep, 2008 - 08:28 AM
Post #5


New D.I.C Head

*
Joined: 27 Nov, 2007
Posts: 46


My Contributions


QUOTE(sansclue @ 29 Sep, 2008 - 09:31 AM) *

A UK (unique key) is a value(s) that can be used to uniquely identify a record. For example, if you have a table of users and it contains ten different users, all named "John Smith". A unique key or ID would tell you which one was which.

In order to compare records from the two tables they need to have a key or something in common. Otherwise you would not know which two records to compare. So what key or values do these two tables have in common?


I haven't "set" a unique key yet, but can. Each of the rows in table one has a column named 'game_id', and the fields in this column are populated with '1', '2', '3', etc..

Is that an identifier? Then the columns, for comparison, in table two are called 'game_1', 'game_2', etc..

Would it be easier if I were comparing rows in the same table, with the same columns? Because I can do that instead.
User is offlineProfile CardPM

Go to the top of the page

sansclue
post 30 Sep, 2008 - 10:17 AM
Post #6


D.I.C Head

**
Joined: 21 Nov, 2007
Posts: 105



Thanked 7 times
My Contributions


QUOTE(jeffmc21 @ 30 Sep, 2008 - 09:28 AM) *

I haven't "set" a unique key yet, but can. Each of the rows in table one has a column named 'game_id', and the fields in this column are populated with '1', '2', '3', etc..

Is that an identifier?


It could be if each game_id is only used once. By definition a UK or PK (Primary key) must be unique. So only one record can have game_id = 1 or game_id = 2, etc.

But usually the easiest option is to just use an identity column for a UK. Add one to your table and make it the PK (primary key). Whenever you insert a new record, MS SQL will increment the value automatically. You do not have to worry about uniqueness or incrementing. There are some exceptions, but it is usually the right choice.

CREATE TABLE YourTable
(
game_id int identity(1,1) PRIMARY KEY,
otherColumn varchar(100),
.....
)

QUOTE(jeffmc21 @ 30 Sep, 2008 - 09:28 AM) *

Then the columns, for comparison, in table two are called 'game_1', 'game_2', etc..

Would it be easier if I were comparing rows in the same table, with the same columns? Because I can do that instead.


Most likely, yes. Usually when you have a table with the same column repeated multiple times (something1, something2, something3, etc), it is a sign you need to normalize and restructure the data into row format.

What are the relationships of the game_id records and what type of comparisons do you need to perform?
User is offlineProfile CardPM

Go to the top of the page

jeffmc21
post 30 Sep, 2008 - 12:26 PM
Post #7


New D.I.C Head

*
Joined: 27 Nov, 2007
Posts: 46


My Contributions


QUOTE(sansclue @ 30 Sep, 2008 - 11:17 AM) *

It could be if each game_id is only used once. By definition a UK or PK (Primary key) must be unique. So only one record can have game_id = 1 or game_id = 2, etc.

But usually the easiest option is to just use an identity column for a UK. Add one to your table and make it the PK (primary key). Whenever you insert a new record, MS SQL will increment the value automatically. You do not have to worry about uniqueness or incrementing. There are some exceptions, but it is usually the right choice.

CREATE TABLE YourTable
(
game_id int identity(1,1) PRIMARY KEY,
otherColumn varchar(100),
.....
)

Most likely, yes. Usually when you have a table with the same column repeated multiple times (something1, something2, something3, etc), it is a sign you need to normalize and restructure the data into row format.

What are the relationships of the game_id records and what type of comparisons do you need to perform?


I would like to compare winners of games, with a user(s) pick for the winners. So table one would actually be the table that held the fields that populate the form for users entries (picks) to be made. The entries would then be inserted into the second table. So, at the end of the week, I'd want to check a column from table_one, the winners we input for each game (row), against the users picks for each game (col1, col2, col3, etc) from table_two.

However, if it'd be easier to do the comparisons by making the winners "column" from table one into a row, just like the user picks would be, in table two, I can do that.


User is offlineProfile CardPM

Go to the top of the page

sansclue
post 30 Sep, 2008 - 02:58 PM
Post #8


D.I.C Head

**
Joined: 21 Nov, 2007
Posts: 105



Thanked 7 times
My Contributions


QUOTE(jeffmc21 @ 30 Sep, 2008 - 01:26 PM) *

I would like to compare winners of games, with a user(s) pick for the winners. So table one would actually be the table that held the fields that populate the form for users entries (picks) to be made. The entries would then be inserted into the second table. So, at the end of the week, I'd want to check a column from table_one, the winners we input for each game (row), against the users picks for each game (col1, col2, col3, etc) from table_two.

However, if it'd be easier to do the comparisons by making the winners "column" from table one into a row, just like the user picks would be, in table two, I can do that.


Then I would definitely go with a row format instead of columns. In table_two I would store each of the user's entries in a separate row. Something like this where each row contains the user_id, game_id and the winning team id. (I am assuming here the team information comes from a separate table with its own ID)

UserID, GameID, WinningTeamID
22 (Bob), 1 (Game One), 2 (Team Blue)
22 (Bob), 2 (Game Two), 3 (Team Alpha)
22 (Bob), 3 (Game Three), 1 (Team Green)
22 (Bob), 4 (Game Four), 9 (Team Omega)
22 (Bob), 5 (Game Five), 4 (Team Gold)
22 (Bob), 6 (Game Six), 3 (Team Alpha)

Then you could easily compare the results by doing a JOIN with table_one on the UK (ie game_id) and using a CASE to see if there is a match on the winner id column.

GameID, WinningTeamID
1 (Game One), 2 (Team Blue)
2 (Game Two), 4 (Team Gold)
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 01:56AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month