我正在努力为此设置。这是第一个表的设置。第一个查询只需要应用于特定日期之后的项目,例如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 |
然后查询需要使用INQUIRY_ID进入另一个表并获取一些额外的字段(在下一个表中每个INQUIRY_ID只有一个条目:
| Table: INQUIRY |
| INQUIRY_ID | … | CATEGORY_ID | … | PROD_ID |
| 2 | | 45 | | 6 |
| 3 | | 3 | | 50 |
| 4 | | 12 | | 3 |
| 5 | | 15 | | 67 |
| 6 | | 10 | | 2 |
一旦我们拥有CATEGORY_ID和PROD_ID,那些“友好名称”就会出现在他们自己的单独表格中
| 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 |
然后我想总计一下LOGMINS字段,记住有时每个INQUIRY_ID会有多个LOGMINS要完成
| INQUIRY_ID | … | TOTAL | … | CATNAME | … | PRODDESC |
| 2 | | 45 | | Server | | SBS 2003 |
| 5 | | 30 | | Workstation | | Windows 7 |
| 6 | | 10 | | Phones | | iPhone |
我很抱歉非常详细,我尝试过的查询只是炸弹,当我进入嵌套查询和多个连接时,我的SQL技能会爆炸。任何帮助都会受到大力赞赏。
查询我现在正在运行:
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
答案 0 :(得分:1)
加入您的数据所在的表格,然后group
加上您保持不变的数据,sum
您要添加的字段
select
inquiry.inquiry_id,
SUM(logmins),
catname,
proddesc
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
group by
inquiry.inquiry_id,
catname,
proddesc