0

我正在为此进行设置。这是第一个表的设置。第一个查询只需要适用于某个日期之后的项目,比如 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
4

1 回答 1

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
于 2013-08-02T15:13:49.703 回答