THE SQL Server Blog Spot on the Web
Welcome to SQLblog.com - The SQL Server blog spot on the web Sign in | Join | Help
in Search

help on select statement

Last post 07-02-2008, 13:38 by Peter DeBetta. 3 replies.
Sort Posts: Previous Next
  •  07-01-2008, 0:55 7588

    help on select statement

    Hi all,

    I have the table as follows:

    table name hits:

    Fields: hitsID, ipAdd, date

    This table collects ipaddress and date of visit from the visitor online to my site.

    now the problem is i want to display all the ip address date wise counting the same ip address and grouping it

    my required result set example:

    Date : 01/01/2008

    IP Address: 206.13.20.12 (22 times)

    IP Address: 203.16.20.166 (10 times)

    IP Address: 149.212.25.11 (5 times

    Date : 01/02/2008

    IP Address: 206.13.20.12 (2 times)

    IP Address: 203.16.20.166 (1 times)

    IP Address: 149.212.25.11 (15 times

    Date : 01/03/2008

    IP Address: 206.13.20.12 (22 times)

    IP Address: 203.16.20.166 (11 times)

    IP Address: 149.212.25.11 (152 times

     Please help me how can i get result set. I tried using

    SELECT ipAdd, date FROM hits GROUP BY date

    but did not work

    Thanks

    KT

    http://www.wholesaleclothingbiz.com

  •  07-02-2008, 4:32 7615 in reply to 7588

    Re: help on select statement

    SELECT

        CONVERT(date, varchar(10), 101) as DateDay,

        ipAdd AS IpAddress,

        COUNT(*) AS NumOfTimes

    FROM hits

    GROUP BY CONVERT(date, varchar(10), 101), ipAdd

  •  07-02-2008, 13:13 7623 in reply to 7588

    Re: help on select statement

    Thanks for the help.

    I tried your statement, but it won't return the result set i want. It returns

    date and 1 ip address and the date repeats, for example if there is 10 different ip address on the same day, it repeats the date 10 times.

    what i want is Date and list all the ip address on that day under the date, and repeat the same for another day.

    thanks again

     

  •  07-02-2008, 13:38 7624 in reply to 7623

    Re: help on select statement

    A SELECT statement isn't designed to do what you want to do. You will need to either write code and process the data from the SELECT statement I sent to you, or write a report using that SELECT statement.

    That being said, you could write something like this (although I make no promises on how well it will perform).

    ;WITH ipReport (Col1Col2Col3Col4AS 
    (  SELECT
           
    2
           
    CONVERT(VARCHAR(10), date101),
           
    ipAdd,
           
    'IP Address: ' ipAdd ' (' CAST(COUNT(*) AS VARCHAR(10)) + ' times)'
       
    FROM hits 
       
    GROUP BY CONVERT(VARCHAR(10), date101), ipAdd
       
       
    UNION ALL

       
    SELECT DISTINCT 
           
    1,
           
    CONVERT(VARCHAR(10), date101),
           NULL,
           
    'Date: ' CONVERT(VARCHAR(10), date101)
       
    FROM hits 
    )
    SELECT Col4
    FROM ipReport
    ORDER BY Col2Col1Col3

    Which will give you results like these: 

    Date: 01/01/2008
    IP Address: 149.212.25.11 (5 times)
    IP Address: 203.16.20.166 (4 times)
    IP Address: 206.13.20.12 (3 times)
    Date: 01/02/2008
    IP Address: 149.212.25.11 (4 times)
    IP Address: 203.16.20.166 (3 times)
    IP Address: 206.13.20.12 (2 times)
    Date: 01/03/2008
    IP Address: 149.212.25.11 (3 times)
    IP Address: 203.16.20.166 (2 times)
    IP Address: 206.13.20.12 (1 times)

Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement