0

第 1 步t1- 将所有存在的行(即field1不为空)计数为Count1并按年和月分组。

SELECT
  Year(t1.date) AS Year,
  Month(t1.date) AS Month,
  Count(t1.field1) As Count1,
FROM t1
GROUP BY Year(t1.date), Month(t1.date)

第 2 步t1- 计算值t1.field1为的所有行,并按年和月分组。 t2.field1Count2

SELECT
  Year(t1.date),
  Month(t1.date),
  Count(*) t1.field1 AS Count2,
  FROM t1 LEFT JOIN t2 ON t1.field1 = t2.field1
  WHERE ISNULL t2.field1
GROUP BY Year(t1.date), Month(t2.date)

第 3 步- 在表格中显示上述两个计数的结果,其中包含Year(t1.date)行标题和Month(t1.date)列标题,每个月的结果Count1Count2嵌套在每个月的下方。

我没有任何代码可以显示第 3 步,因为我不知道该怎么做。

4

1 回答 1

0

试试这个。

SELECT
      STEP1.Total_Rows_S1, STEP1.Year_S1, STEP1.Month_S1, COUNT(*) as Remaining_Rows
FROM (
            SELECT
            Year(t1.date) AS Year_S1,
            Month(t1.date) AS Month_S1,
            Count(t1.field1) As Total_Rows_S1,
            t1 .field1 AS FS1
            FROM t1
            GROUP BY Year(t1.date), Month(t1.date)
) AS STEP1 LEFT JOIN t2 ON (STEP1.FS1 = t2.field1)
WHERE ISNULL t2.field1
GROUP BY STEP1.Year_S1, Month( t2.日期)

于 2012-12-30T05:20:55.733 回答