I have two tables:
doc_types:
doc_id | doc_name | expiry_days
-------|----------|-------------
1 | Doc_1 | 365
2 | Doc_2 | 90
3 | Doc_3 | 30
docs_supplied:
evidence_id | doc_id | client_id | date_supplied
------------|---------|-----------|-----------------
1 | 1 | 5432 | 13-05-2012
2 | 1 | 3165 | 25-04-2011
3 | 2 | 5432 | 23-10-2011
The output I would like to see is:
doc_id | doc_name | expiry_days | client_id | date_supplied
-------|----------|-------------|-----------|----------------
1 | Doc_1 | 365 | 5432 | 13-05-2012
2 | Doc_2 | 90 | 5432 | NULL
3 | Doc_3 | 30 | 5432 | NULL
1 | Doc_1 | 365 | 3165 | 25-04-2012
2 | Doc_2 | 90 | 3165 | NULL
3 | Doc_3 | 30 | 3165 | NULL
Essentially for each client, I want to show the complete list of documents (14) but with the extra fields for date supplied etc populated, should there be an entry for that client and document in the DB. So for each client record I’d expect there to be 14 rows, one for each doc_type. Some of these rows will have client-specific data in them.
This is so that in my grid I can just filter against a client_id and see the list of all documents and their status for that person.
Is this possible?
See it on sqlfiddle.
If you have a table with a unique
client_idcolumn, you can simply use that instead of generating it from theSELECT DISTINCTsubquery:Learn about SQL joins.