SQL Server Count Query

  • Hi All,

    I have a data in column A in where in need to achieve the count which I have mentioned in column B. Please find the attached excel dataset for reference.

    Thanks

    Attachments:
    You must be logged in to view attached files.
  • OK. What is your question?

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Do either of these give you what you need?

    --ONE ROW PER NAME WITH COUNT
    SELECT firstlastmerged,
    COUNT(firstlastmerged) AS Cnt
    FROM dbo.YourTable
    GROUP BY firstlastmerged

    --REPEATED NAMES WITH REPEATED COUNT
    SELECT firstlastmerged,
    COUNT(*) OVER (PARTITION BY firstlastmerged) AS Cnt
    FROM dbo.YourTable
  • sathishkm wrote:

    Hi All,

    I have a data in column A in where in need to achieve the count which I have mentioned in column B. Please find the attached excel dataset for reference.

    Thanks

    A lot of us won't open attached spreadsheets for many reasons.  Please see the article at the first link in my signature line below for the preferred method of using readily consumable data.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Here is the query to insert the data in table.

     

    -- Step 1: Create the table

    CREATE TABLE sat_temp (

    Domain NVARCHAR(255),

    [Required Result] INT

    );

    -- Step 2: Insert the data

    INSERT INTO sat_temp (Domain, [Required Result])

    VALUES

    ('AlissaParteemarvin.com', 10),

    ('AlissaParteemarvin.com', 10),

    ('AlissaParteemarvin.com', 10),

    ('AlissaParteemarvin.com', 10),

    ('AlissaParteemarvin.com', 10),

    ('AlissaParteemarvin.com', 10),

    ('AlissaParteemarvin.com', 10),

    ('AlissaParteemarvin.com', 10),

    ('AlissaParteemarvin.com', 10),

    ('AlissaParteemarvin.com', 10),

    ('DanielOoiocbc.com', 5),

    ('DanielOoiocbc.com', 5),

    ('DanielOoiocbc.com', 5),

    ('DanielOoiocbc.com', 5),

    ('DanielOoiocbc.com', 5),

    ('GargPrashantocbc.com', 1),

    ('GaryKeildurangotrain.com', 2),

    ('GaryKeildurangotrain.com', 2),

    ('GuSuyangsdic.com.cn', 2),

    ('GuSuyangsdic.com.cn', 2);

    Need a query to get the result which I had mentioned in column [Required Result]

    • This reply was modified 4 months, 3 weeks ago by  sathishkm.
  • If you're going to create a temp table, at least make it temporary!

    DROP TABLE IF EXISTS #sat_temp;

    CREATE TABLE #sat_temp
    (
    Domain NVARCHAR(255)
    );

    INSERT #sat_temp
    (
    Domain
    )
    VALUES
    ('AlissaParteemarvin.com')
    ,('AlissaParteemarvin.com')
    ,('AlissaParteemarvin.com')
    ,('AlissaParteemarvin.com')
    ,('AlissaParteemarvin.com')
    ,('AlissaParteemarvin.com')
    ,('AlissaParteemarvin.com')
    ,('AlissaParteemarvin.com')
    ,('AlissaParteemarvin.com')
    ,('AlissaParteemarvin.com')
    ,('DanielOoiocbc.com')
    ,('DanielOoiocbc.com')
    ,('DanielOoiocbc.com')
    ,('DanielOoiocbc.com')
    ,('DanielOoiocbc.com')
    ,('GargPrashantocbc.com')
    ,('GaryKeildurangotrain.com')
    ,('GaryKeildurangotrain.com')
    ,('GuSuyangsdic.com.cn')
    ,('GuSuyangsdic.com.cn');

    SELECT st.Domain
    ,ct = COUNT (st.Domain)
    FROM #sat_temp st
    GROUP BY st.Domain;

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • @sathishkm-2

    Did either of Ed B's solutions work for you?

    If not, could you post what the output should look like using the test data you've provided?  I'm also not sure what your request actually is.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply