Solved

keywords count

Posted on 2014-01-29
6
386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 7

Assisted Solution

by:aplusexpert
aplusexpert earned 50 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 450 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:ethar1
ID: 39817751
0
 

Author Comment

by:ethar1
ID: 39818223
0
 

Author Comment

by:ethar1
ID: 39818493
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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

710 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