Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8182959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:00:59+00:00 2026-06-07T01:00:59+00:00

i have a table which has many fields but i want to get count

  • 0

i have a table which has many fields but i want to get count of every word in any three fields of that table

find all title in a table that exist more than once…so for that i can issue this statement

SELECT title, COUNT(title) AS NumOccurrences FROM users
GROUP BY titleHAVING ( COUNT(title) > 1 )

suppose my table has three fields called title,url,description.
basically i do not know which word has been stored in which 3 fields in that table maximum time.

i want to issue a sql statement which can show me which word found maximum time…like

word-name          occurance
---------          -------
sqlserver           300
jquery              120
ajax                110

please guide me with sample sql for sql server 2000/2005 thanks

Here is my updated full code…..please have look

    IF OBJECT_ID('tempdb..#tempSearch') IS NOT NULL
BEGIN
    DROP TABLE #tempSearch
END

CREATE TABLE #tempSearch(
    ID INT,
    Title nvarchar(4000),
    Description ntext,
    Url nvarchar(4000),
    Type char(1))

INSERT INTO #tempSearch 
    SELECT * from vwProductSearch

INSERT INTO #tempSearch 
    SELECT * from vwContentSearch

    SELECT  Word, 
    COUNT(Word) AS TotalOccurrences,
    COUNT(CASE WHEN Field = 'Title' THEN Word END) AS OccurancesInTitle,
    COUNT(CASE WHEN Field = 'URL' THEN Word END) AS OccurancesInURL,
    COUNT(CASE WHEN Field = 'Description' THEN Word END) AS OccurancesInDescription
    FROM    (   SELECT  CONVERT(NTEXT, Title) AS Word, 'Title' AS Field
        FROM    #tempSearch
        UNION ALL
        SELECT  CONVERT(NTEXT, URL), 'URL' AS Field
        FROM    #tempSearch
        UNION ALL
        SELECT  CONVERT(NTEXT, Description), 'Description' AS Field
        FROM    #tempSearch
    ) As Fields
    GROUP BY Word
    HAVING  COUNT(Word) > 1

   DROP TABLE #tempSearch
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:01:02+00:00Added an answer on June 7, 2026 at 1:01 am

    You need to use UNION to combine your 3 fields into a single column so you can use this to group by. I’ve also added a few more counts in case you need to drill down as to where the word occurs the most.

    SELECT  Word, 
            COUNT(Word) AS TotalOccurrences,
            COUNT(CASE WHEN Field = 'Title' THEN Word END) AS OccurancesInTitle,
            COUNT(CASE WHEN Field = 'URL' THEN Word END) AS OccurancesInURL,
            COUNT(CASE WHEN Field = 'Description' THEN Word END) AS OccurancesInDescription
    FROM    (   SELECT  Title AS Word, 'Title' AS Field
                FROM    Users
                UNION ALL
                SELECT  URL, 'URL' AS Field
                FROM    Users
                UNION ALL
                SELECT  Description, 'Description' AS Field
                FROM    Users
            ) As Fields
    GROUP BY Word
    HAVING  COUNT(Word) > 1
    

    EDIT

    I know you have asked about SQL_Server 2005 and 2000, but if you were ever to upgrade to 2008 or later there is a much cleaner solution:

    SELECT  Word, 
            COUNT(Word) AS TotalOccurrences,
            COUNT(CASE WHEN Field = 'Title' THEN Word END) AS OccurancesInTitle,
            COUNT(CASE WHEN Field = 'URL' THEN Word END) AS OccurancesInURL,
            COUNT(CASE WHEN Field = 'Description' THEN Word END) AS OccurancesInDescription
    FROM    Users
            CROSS APPLY 
            (   VALUES 
                    (Title, 'Title'), 
                    (URL, 'URL'), 
                    (Description, 'Description')
            ) AS T (Word, Field)
    GROUP BY Word
    HAVING  COUNT(Word) > 1
    

    EDIT 2

    If all your columns are different datatypes you will need to explicitly convert them:

    SELECT  Word, 
            COUNT(Word) AS TotalOccurrences,
            COUNT(CASE WHEN Field = 'Title' THEN Word END) AS OccurancesInTitle,
            COUNT(CASE WHEN Field = 'URL' THEN Word END) AS OccurancesInURL,
            COUNT(CASE WHEN Field = 'Description' THEN Word END) AS OccurancesInDescription
    FROM    (   SELECT  CONVERT(NTEXT, Title) AS Word, 'Title' AS Field
                FROM    Users
                UNION ALL
                SELECT  CONVERT(NTEXT, URL), 'URL' AS Field
                FROM    Users
                UNION ALL
                SELECT  CONVERT(NTEXT, Description), 'Description' AS Field
                FROM    Users
            ) As Fields
    GROUP BY Word
    HAVING  COUNT(Word) > 1
    

    EDIT 3

    There is no way around the error you are getting, you cannot group by NTEXT. The best solution I can come up with feels very dirty, and I’m not particularly happy with it…

    SELECT  COALESCE(Title, URL, Description) AS Word,
            COALESCE(Title.Occurances, 0) + COALESCE(URL.Occurances, 0) + COALESCE(Description.Occurances, 0) AS TotalOccurances,
            COALESCE(Title.Occurances, 0) AS TitleOccurances,
            COALESCE(URL.Occurances, 0) AS URLOccurances,
            COALESCE(Description.Occurances, 0) AS DescriptionOccurances
    FROM    (   SELECT  CONVERT(NTEXT, Title) AS Title, COUNT(*) AS Occurances
                FROM    #tempSearch
                GROUP BY Title
            ) AS Title
            FULL JOIN 
            (   SELECT  CONVERT(NTEXT, URL) AS URL, COUNT(*) AS Occurances
                FROM    #tempSearch
                GROUP BY URL
            ) AS URL
                ON URL LIKE Title
            FULL JOIN
            (   SELECT  Description, 1 AS Occurances
                FROM    #tempSearch
            ) AS Description
                ON Description LIKE Title
    

    This works, but like I said, it isn’t perfect and probably won’t perform very well. Strongly consider upgrading to a later version of SQL-Server!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that has 8 million records, with many fields, including lat/long
I have a table which has several one to many relationships with other tables.
if i have a table which has columns with fixed lenght, Will mySQL count
I have a table Submission, which has a many to one mapping to a
I have 2 tables xcart_products which has productid , meta_description and many other fields
I have a hierarchy of tables 3 levels deep (QualificaionType has many QualificationGroups, which
I have a table which has two text boxes and an image beside it
I have a table which has employee relationship defined within itself. i.e. EmpID Name
I have a table which has essentially boolean values in a legacy database. The
I have a table which has a column called Direct of type char(1). It's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.