Select a group of records at a time based on them having same value in a particular column

180 Views Asked by At

I want to be able to use a single query to select all records out of a table having the same value in a particular column.

Clarification

Conditions: - document_id is not a primary key.

List ids = SELECT DISTINCT T.document_id FROM table T;

List block = SELECT * from table where document_id = ids.next();

  • perform operation on the block, then retrieve next block

Could anyone please tell me how to convert these two queries into a single query?

UPDATE

@Marco @StephanB really sorry that my question was so vauge...i should learn to be more specific...

anyway, i am using JAVA and Apache Turbine with Torque. Here's the code which i want to change:

public void runQueue (String username, Customer customer) {
    Criteria c = new Criteria();
    c.add( DataEntryQueuePeer.CUSTOMER_ID, customer.getId());//DataEntryQueue.CUSTOMER_ID is not the primary key
    c.add( DataEntryQueuePeer.STATUS, DataEntryQueue.STATUS_IN_KEYING );
    c.add( DataEntryQueuePeer.DATA_ENTRY_USERID, username );
    c.add( DataEntryQueuePeer.QUEUE_TYPE, Constants.CUSTOMER_QUEUE );

    List<DataEntryQueue> v = DataEntryQueuePeer.doSelect( c );
    if( v.size() > 0 ) {
        //do something with v
        v.setStatus(DataEntryQueue.STATUS_KEYING_COMPLETE);
        v.setModified(true);
        v.save();
    }
}

I want to add one more field(DataEntryQueuePeer.DOCUMENT_ID) in the criteria so that only records with the same document_id are selected on every "doSelect". No specific DOCUMENT_ID is passed to the method though.

please let me know if i'm still not clear enough...thanks.

1

There are 1 best solutions below

1
On

Your SQL is malformed. A correct question is more likely to retrieve a correct answer. I'll name your table A and B.

SELECT * FROM B WHERE B.document_id IN (SELECT DISTINCT A.document_id FROM A)