Solved

keywords count

Posted on 2014-01-29
6
375 Views
Last Modified: 2014-01-29
Dear all,
I have this table @KeyWords ( word, Count)
how to add all rows in @KeyWords  to another Table @AnotherKeyWords (word, Count)
if the word not exist in @AnotherKeyWords.


but if the word exist in @AnotherKeyWords   then @AnotherKeyWords .Count=@AnotherKeyWords .Count+@KeyWords.Count


thanks,
0
Comment
Question by:ethar1
  • 4
6 Comments
 
LVL 7

Assisted Solution

by:aplusexpert
aplusexpert earned 50 total points
Comment Utility
Execute the following two queries:

Query1: add all rows in @KeyWords  to another Table @AnotherKeyWords (word, Count)
if the word not exist in @AnotherKeyWords:

INSERT INTO @AnotherKeyWords
FROM SELECT @KeyWords.Word, @KeyWords.COUNT FROM @KeyWords WHERE @KeyWords.Word NOT IN (SELECT @AnotherKeyWords.Word FROM @AnotherKeyWords)

Query2: if word exist in @AnotherKeyWords then @AnotherKeyWords .Count=@AnotherKeyWords .Count+@KeyWords.Count

UPDATE AKW SET AKW.COUNT = AKW.Count + @KeyWords.COUNT
FROM @AnotherKeyWords AKW INNER JOIN @KeyWORDS ON AKW.Word = @KeyWords.Word

(The inner join will ensure that only when Word is there in both tables, then COUNT will be updated).
0
 
LVL 12

Accepted Solution

by:
Harish Varghese earned 450 total points
Comment Utility
Hello,
First do an update of the count in @AnotherKeyWords table for the matching words
And then insert the additional words from @KeyWords table along with count to @AnotherKeyWords.

Update AK
SET AK.Count = AK.Count + K.Count
From @AnotherKeyWords AK, @KeyWords K
Where AK.Word = K.Word

Insert into @AnotherKeyWords (Word, Count)
Select Word, Count
From @KeyWords K
Where Not Exists (Select 1 from @AnotherKeyWords AK Where AK.Word = K.Word)

-Harish
0
 

Author Closing Comment

by:ethar1
Comment Utility
excellent
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ethar1
Comment Utility
0
 

Author Comment

by:ethar1
Comment Utility
0
 

Author Comment

by:ethar1
Comment Utility
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now