473,387 Members | 1,495 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,387 software developers and data experts.

Best practice for comparing 2 tables

6
Hi all,

Having a bit of a headache trying to compare 2 tables in my MS SQL db.

Basically I have

Table 1 - dbo.teams (Contains a list of teams under field name team_NAME)
Table 2 - dbo.user-preferences (Keeps a record of Teams chosen from Table 1)

At the moment I may have

Table 1 - teams:
Column in db - team_NAME:
teamA
teamB
teamC
teamD
teamE etc...

Table 2 - user_preferences:
Column in db - prefTEAM:
teamC
teamE

What I'm trying to do is construct a select statement and condition to display a distinct list of results of all teams in Table 1 but highlight/bold any that are also in Table 2

so in the above tables example the results I would need would be...

teamA
teamB
<bold>teamC<bold>
teamD
<bold>teamE<bold>

Any ideas on the best way to do this?

If possible I need the condition that would highlight duplicates seperate from the select statement as rather than bold, I may change these to check or uncheck boxes on a form.

Hope it all makes sense.

Regards

Paigey
Feb 7 '08 #1
4 1509
Hi SQL Friend,

I understood that you need a result that you must able to identify the user_pref record. i.e as below result.
-------------------------------------------------------
Team_Name AvailableInUserPref
---------------------------------------------------------
TeamA 0
TeamB 0
TeamC 1
TeamD 0
TeamE 1
-------------------------------------------------------
I didnt tried by running it, but observe the logic what i did,

Declare @User_Preffered Table(Team_Name varchar(50), AvailableInUserPref bit Default 0)
Insert @User_Preffered(Team_Name,AvailableInUserPref)
Select Team_Name,
(Case When Team_Name IN (Select Distinct prefTeam from dbo.userPreferences ) Then 1 End) from dbo.Teams

Try it,
If u get result means its okay otherwise i try it in system and send u query

Ganesh Kumar V
Feb 7 '08 #2
ck9663
2,878 Expert 2GB
Hi all,

Having a bit of a headache trying to compare 2 tables in my MS SQL db.

Basically I have

Table 1 - dbo.teams (Contains a list of teams under field name team_NAME)
Table 2 - dbo.user-preferences (Keeps a record of Teams chosen from Table 1)

At the moment I may have

Table 1 - teams:
Column in db - team_NAME:
teamA
teamB
teamC
teamD
teamE etc...

Table 2 - user_preferences:
Column in db - prefTEAM:
teamC
teamE

What I'm trying to do is construct a select statement and condition to display a distinct list of results of all teams in Table 1 but highlight/bold any that are also in Table 2

so in the above tables example the results I would need would be...

teamA
teamB
<bold>teamC<bold>
teamD
<bold>teamE<bold>

Any ideas on the best way to do this?

If possible I need the condition that would highlight duplicates seperate from the select statement as rather than bold, I may change these to check or uncheck boxes on a form.

Hope it all makes sense.

Regards

Paigey

name is really not recommended primary key...but if this all you have to work with, try:

select team_name, case when user_preferences.prefTEAM is null then 0 else 1 end as preferred
from teams left join user_preferences on team_name = prefTEAM

the generated column preferred will be your flag if it exist on both table. you then have to adjust on your GUI that if it's 1 you have to tag it in BOLD

-- ck
Feb 7 '08 #3
Paigey
6
Hi Ck,

Thanks for the help. Seems to be along the right lines.

I've updated the script and now have

prefPREM = "SELECT team_NAME, CASE WHEN user_preferences.prefTEAM is null then 0 else 1 end as preferred " _
& "FROM football_teams LEFT JOIN user_preferences on team_NAME = prefTEAM "


Set rsPREM = conn.Execute (prefPREM)

Do Until rsPREM.EOF

' Conditional statement

rsPREM.MoveNext
Loop


I need to add a couple of things

also need to have the user_preferences.prefTEAM as a distinct list
need to select all prefTEAM from user_preferences where prefEMAIL = '"&currUSER&"'

'"&currUSER&"' is a session of the users email address that gets called in when they login.

Also how would I write the conditional statement so as you say the 0 and 1 gets flagged as bold or not bold?

Thanks for your help...

Paigey
Feb 7 '08 #4
ck9663
2,878 Expert 2GB
Hi Ck,

Thanks for the help. Seems to be along the right lines.

I've updated the script and now have

prefPREM = "SELECT team_NAME, CASE WHEN user_preferences.prefTEAM is null then 0 else 1 end as preferred " _
& "FROM football_teams LEFT JOIN user_preferences on team_NAME = prefTEAM "


Set rsPREM = conn.Execute (prefPREM)

Do Until rsPREM.EOF

' Conditional statement

rsPREM.MoveNext
Loop


I need to add a couple of things

also need to have the user_preferences.prefTEAM as a distinct list
need to select all prefTEAM from user_preferences where prefEMAIL = '"&currUSER&"'

'"&currUSER&"' is a session of the users email address that gets called in when they login.

Also how would I write the conditional statement so as you say the 0 and 1 gets flagged as bold or not bold?

Thanks for your help...

Paigey

try

select distinct prefTeam from user_preferences where where prefEMAIL = currUSER -- > you have to convert this into sql string. similar to the one you created above.

the conditional i am talking about will be in your GUI/apps side, not in sql server. so the syntax would depend on your front-end tool

-- ck
Feb 8 '08 #5

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

Similar topics

4
by: Will Hartung | last post by:
The designers have handed me a page that has 5 different blocks on it in the center column (in a typical 3 column layout with page spanning headers and footers). The blocks have elaborate...
10
by: Rich Wallace | last post by:
Hey all, I have an XML doc that I read into a SQL Server database from an integration feed.... ----------------XML snippet ---------------- <?xml version="1.0" encoding="us-ascii"?>...
2
by: digitalQ | last post by:
Okay, this should be pretty simple for those of you who have written several enterprise level web applications: I'm looking for simple concepts to deal with a client re-posting the same data...
17
by: | last post by:
I have an app that retrieves data from an Access database. At the moment I have the SQL string as a Const in my app. I understand this is not best practice. I don't want the user to have access to...
21
by: John Salerno | last post by:
If I want to make a list of four items, e.g. L = , and then figure out if a certain element precedes another element, what would be the best way to do that? Looking at the built-in list...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
7
by: sbryguy | last post by:
Greetings All, I am trying to parse a directory of images and reference a table in SQL to check if that image filename exists in the table. If not I'd like to INSERT it into a table (tbl_images)...
5
by: Frank Millman | last post by:
Hi all This is not strictly a Python question, but as I am writing in Python, and as I know there are some XML gurus on this list, I hope it is appropriate here. XML-schemas are used to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.