-2

I have the following Oracle database, and I need to return the following: d.directdomain, d.domaindisplayname, r.lastdate (The latest), and count(how many times, r.directdomain = d.directdomain)

basically, I have lots of "people" in r db, and "domains" in the d. I need to return how many times a person in R visited each "domain", and also return the last time they visited the domain. I tried a few things, but it seems by using count, i need to GROUP BY the date, so that's confusing me.

Example return: 1, Site1, 21/05/13, 5 2, Site2, 20/05/13, 2

d
directdomain (PK)
domaindisplayname

r
rsld         (PK)
lastdate
directdomain (FK)
4

1 回答 1

1

Are you looking for something like this?

SELECT d.directdomain, 
       d.domaindisplayname,
       MAX(r.lastdate) lastdate,
       COUNT(*) rcount
  FROM d JOIN r
    ON d.directdomain = r.directdomain
 GROUP BY d.directdomain, d.domaindisplayname

Sample output:

| DIRECTDOMAIN | DOMAINDISPLAYNAME |                      LASTDATE | RCOUNT |
|--------------|-------------------|-------------------------------|--------|
|            1 |             Site1 | August, 15 2013 00:00:00+0000 |      4 |
|            2 |             Site2 | August, 18 2013 00:00:00+0000 |      3 |

Here is SQLFiddle demo

于 2013-08-30T03:48:07.963 回答