tags:

views:

96

answers:

5

I am coding facebook kind request page for a project. I am stuck with grouping requests in all requests printed page.

my sql as

SQL = "SELECT R.REQUESTID, R.BYID, R.TOID, R.TYPE, R.DATEENTERED, M.MEMBERID, M.FIRSTNAME, M.LASTNAME"
SQL = SQL & " FROM REQUESTS R, MEMBERS M"
SQL = SQL & " WHERE R.ACTIVE = 1 AND R.TOID = "& Session("MEMBERID") &" AND R.BYID = M.MEMBERID"
SQL = SQL & " GROUP BY R.REQUESTID, R.BYID, R.TOID, R.TYPE, R.DATEENTERED, M.MEMBERID, M.FIRSTNAME, M.LASTNAME"
SQL = SQL & " ORDER BY R.DATEENTERED DESC"

Returns me results as
Friend
Edie
Friend
Frank
Group
George

But I need it as

Friend
Edie
Frank
Group
George

A: 

Grouping by is going to give you unique values for the column by which your grouping.

Why do you want the Friend row listed twice?

Aaron F.
Thats the thing. I dont want it listed twice but currently it is.
Efe
A: 

if there is more than one REQUESTS for one of the MEMBERS you are after, you will get duplicates.

look at this:

SELECT --select all columns required, can produce duplicates
    R.REQUESTID, R.BYID, R.TOID, R.TYPE, R.DATEENTERED, M.MEMBERID, M.FIRSTNAME, M.LASTNAME
    FROM REQUESTS            R
        INNER JOIN MEMBERS   M ON R.BYID = M.MEMBERID
    WHERE M.MEMBERID IN (SELECT --get distinct list of members to display
                             M.MEMBERID
                             FROM REQUESTS            R
                                 INNER JOIN MEMBERS   M ON R.BYID = M.MEMBERID
                             WHERE  R.ACTIVE = 1 AND R.TOID = ___MEMBERID__
                             GROUP BY M.MEMBERID
                        )
    ORDER BY R.DATEENTERED DESC
KM
I think I am gonna need to use join but I think I will need to joiN REQUESTS table with REQUESTS again.
Efe
the derived table gets a distinct list of members, I then join back to the requests in an attempt to get the other columns you want to show, which introduce duplicates. Limit your result columns to only what you need. if you need all of those columns, then you have to live with the duplicates.
KM
A: 

There are a number of fields in your SELECT and GROUP BY clauses which, from the their names, sound like either primary key values such as MEMBERID or fields likely to be very unique, such as DATEENTERED (this last one probably includes time info as well.) Any one of these fields could prevent your query from doing any kind of grouping at all. i would try removing some of these items until your grouping starts to take effect. This way you'll isolate which of the fields are problematic. Or, Keep a few fields such as first and last name and then add on to see where the GROUP BY falls apart.

Paul Sasik
A: 

I just added a DISTINCT on the top of the query. I guessing that would solve if I got it right.

SQL = "SELECT DISTINCT R.REQUESTID, R.BYID, R.TOID, R.TYPE, R.DATEENTERED, M.MEMBERID, M.FIRSTNAME, M.LASTNAME"

SQL = SQL & " FROM REQUESTS R, MEMBERS M"

SQL = SQL & " WHERE R.ACTIVE = 1 AND R.TOID = "& Session("MEMBERID") &" AND R.BYID = M.MEMBERID"

SQL = SQL & " GROUP BY R.REQUESTID, R.BYID, R.TOID, R.TYPE, R.DATEENTERED, M.MEMBERID, M.FIRSTNAME, M.LASTNAME"

SQL = SQL & " ORDER BY R.DATEENTERED DESC"

Try this if works vote it!

Tamil.SQL
I tried distinct, it wont work.
Efe
+2  A: 

Try changing it to a distinct select. I think this is provider specific, but usually

SQL = "SELECT DISTINCT R.REQUESTID, R.BYID, R.TOID, R.TYPE, R.DATEENTERED, M.MEMBERID, M.FIRSTNAME, M.LASTNAME"

or

SQL = "SELECT DISTINCT ROW R.REQUESTID, R.BYID, R.TOID, R.TYPE, R.DATEENTERED, M.MEMBERID, M.FIRSTNAME, M.LASTNAME"

Will do the trick.

Kieveli
if there are multiple REQUESTS rows per MEMBERS, a distinct is very unlikely to remove the duplicates. If REQUESTS.DATEENTERED is diffeent, or REQUESTS.REQUESTID is a unique id, then there is no way a distinct will work.
KM
its amazing, this answer is flat out wrong, yet it got 20 points, while my correct (and selected) answer only got 15!
KM
My avatar contains a hypnotic suggestion. ;)
Kieveli
my avatar is lame!
KM