I'm struggling for the setup for this. This is the setup of the first table. The first query needs to only apply to items past a certain date, say 2012-06-01.
| Table: TIMELOG |
| INQUIRY_ID | … | LOGMINS | … | OPERID | … | EDATE |
| 2 | | 45 | | bob | | 2012-05-01 |
| 5 | | 3 | | richard | | 2012-06-02 |
| 5 | | 12 | | bob | | 2012-07-01 |
| 5 | | 15 | | paul | | 2012-07-01 |
| 6 | | 10 | | paul | | 2012-07-01 |
The query then needs to go into a different table using INQUIRY_ID and get some additional fields (there is only one entry per INQUIRY_ID in this next table:
| Table: INQUIRY |
| INQUIRY_ID | … | CATEGORY_ID | … | PROD_ID |
| 2 | | 45 | | 6 |
| 3 | | 3 | | 50 |
| 4 | | 12 | | 3 |
| 5 | | 15 | | 67 |
| 6 | | 10 | | 2 |
Once we have the CATEGORY_ID and PROD_ID the "friendly names" for those are in their own seperate tables
| Table: CATEGORY |
| CATEGORY_ID | … | CATNAME |
| 45 | | Server |
| 3 | | Workstation |
| 12 | | Phones |
| 15 | | Backup |
| 10 | | Network |
| Table: PROD |
| PROD_ID | … | PRODDESC |
| 6 | | SBS 2003 |
| 50 | | Windows 7 |
| 3 | | iPhone |
| 67 | | Buexec |
| 2 | | SwitchF |
I'd then like to total the LOGMINS field, bearing in mind sometimes it will have multiple LOGMINS per INQUIRY_ID To finish up as
| INQUIRY_ID | … | TOTAL | … | CATNAME | … | PRODDESC |
| 2 | | 45 | | Server | | SBS 2003 |
| 5 | | 30 | | Workstation | | Windows 7 |
| 6 | | 10 | | Phones | | iPhone |
I'm sorry for the extraordinary amount of detail, the queries I have tried just bomb out, my skill in SQL blows up when I get into nested queries and multiple joins. Any help would be massively appreciated.
Query I am running now:
select
timelog.INQUIRY_ID,
SUM (logmins) AS Total,
catname,
proddesc,
EDATE
from
timelog
inner join inquiry on timelog.inquiry_id=inquiry.inquiry_id
inner join category on inquiry.category_id = category.category_id
inner join prod on inquiry.prod_id = prod.prod_id
where EDATE > '2013-07-01'
group by
timelog.INQUIRY_ID,
catname,
proddesc,
edate
order by timelog.INQUIRY_ID desc