协助SQL查询

时间:2012-12-04 13:22:06

标签: sql ms-access

现在我有两个单独的查询来实现结果,但我想要一个查询。我正在使用MS Access。

tblJournal          tblArticle      tblStatus
----------          ----------      ---------
JournalID           ArticleID       ID
JournalTitle        JournalCode     StatusName
JournalCode         StatusID       
Cancelled(yes/no)   Date
                    ArticleTitle
                    SignedDate

查询应该有:

tblJournal.JournalTitle, 
tblJournal.JournalCode, 
Year([tblArticle].[Date]), 
Count of articles per year which have a tblArticle.SignedDate, 
Count of articles per year with tblStatus.StatusName 'Published' and tblJournal.Cancelled NO.

1 个答案:

答案 0 :(得分:1)

我认为这应该是你想要的:

SELECT
  tblJournal.JournalTitle, 
  tblJournal.JournalCode, 
  Year([tblArticle].[Date]),
  Count(tblArticle.SignedDate) as Tot_Articles_Year,
  Sum(IIF((not tblJournal.Cancelled) and (tblStatus.StatusName='Published'),1,0)) as Tot_Published
FROM
  (tblJournal LEFT JOIN tblArticle
   ON tblJournal.JournalCode = tblArticle.JournalCode)
  LEFT JOIN tblStatus
  ON tblArticle.StatusID = tblStatus.ID
GROUP BY 
  tblJournal.JournalTitle, 
  tblJournal.JournalCode, 
  Year([tblArticle].[Date])