SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Searching for specific words
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cocoa11
Starting Member

2 Posts

Posted - 03/13/2013 :  09:48:35  Show Profile  Reply with Quote
Below is an example of the field I am looking at. I am trying to count specific occurances of words. The field is how users are searching. More than one criteria can show up per field.

blahblahblah, Last Name: fakename
blahblahblah, ClientID: fakenumber
blahblahblah, Birth Date: fakedate
blahblahblah, First Name: fakename, Last Name: fakename

This works for me, but I do not want each criteria to be it's own column. I want then all in a column with a count of how many times it appears.

DECLARE @BirthDate Integer
DECLARE @SSN Integer
DECLARE @LastName Integer
DECLARE @FirstName Integer

SET @BirthDate = 0
SET @SSN = 0
SET @LastName = 0
SET @FirstName = 0

SELECT
SUM(CASE WHEN Searchlog.Criteria LIKE '%Birth Date%' THEN @Birthdate + 1 END) as [DOB],
SUM(CASE WHEN Searchlog.Criteria LIKE '%SSN%' THEN @SSN + 1 END) AS [SSN],
SUM(CASE WHEN Searchlog.Criteria LIKE '%Last Name%' THEN @LastName + 1 END) AS [LastName],
SUM(CASE WHEN Searchlog.Criteria LIKE '%First Name%' THEN @FirstName+ 1 END) AS [FirstName]

FROM Searchlog


DOB|SSN|LastName|FirstName|
2 |4 |17 |3 |

I feel like I might be over commplicating things.



Edited by - cocoa11 on 03/13/2013 09:53:48

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 03/13/2013 :  10:24:08  Show Profile  Reply with Quote

SELECT t.val,t1.cnt
FROM
(
SELECT 'Birth Date' AS val UNION ALL
SELECT 'SSN'  UNION ALL
SELECT 'Last Name'  UNION ALL
SELECT 'First Name' 
)t
CROSS APPLY (SELECT COUNT(*) AS Cnt
             FROM SearchLog
             WHERE Criteria LIKE '%' + t.val + '%'
             )t1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

cocoa11
Starting Member

2 Posts

Posted - 03/13/2013 :  10:45:58  Show Profile  Reply with Quote
Perfect! And thanks for the speedy reply!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 03/13/2013 :  11:17:56  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000