views:

30

answers:

1

I totally can't get my head around writing a correct query for my problem this morning so here's hoping that someone out there can help me out.

I have a database table called Sessions which basically looks like this

Sessions:
  SessionID
  SessionStarted
  IPAddress
  ..other meta data..

I have a requirement where I am to show how many new Sessions (where new is defined as from a previously unseen IPAddress) arrive each day over a given period. Basically, each IPAddress should count only once in the results, namely for the day of the first session from the IPAddress. So I'm looking for a result like:

[Date]      [New]
2009-10-01 : 11
2009-10-02 : 6
2009-10-03 : 19
..and so on

...which I can plot on some nice chart and show to important people. I would very much prefer a Linq2SQL query as that is what we are currently using for data access, but if I'm out of luck I may be able to go with some raw SQL (accessed via stored procedure, but I would really, really, really prefer Linq2SQL).

(As a bonus my next step will very likely be qualifying which sessions should be included by filtering on some of the other meta data)

Hoping that someone clever will help me out here...

+1  A: 

I would use something like this.

var result = data.OrderBy(x => x.SessionStarted)
    .GroupBy(x => x.IPAddress)
    .Select(x => x.First())
    .GroupBy(x => x.SessionStarted.Date)
    .Select(x => new { Date = x.Key, New = x.Count() });
Matajon
Thank you...probably saved me a headache since is was going in a totally other direction.
soren.enemaerke