Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

keywords count

Posted on 2014-01-29
6
Medium Priority
?
409 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 200 total points
ID: 39817388
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 1800 total points
ID: 39817397
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
ID: 39817414
excellent
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

916 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