SQL - Group不在此代码中工作

时间:2011-06-21 11:58:02

标签: sql ms-access

TRANSFORM Count(Details.Customer_ID) AS CountOfCustomer_ID
SELECT
  Switch(
    [Age] < 25,                  "Under 25",
    [Age] >= 25 And [Age] <= 32, "Between 25 And 32",
    [Age] >= 32 And [Age] <= 40, "Between 32 And 40",
    [Age] >= 40 And [Age] <= 45, "Between 40 And 45",
    [Age] > 45,                  "Over 45"
  ) AS Age_Range
FROM Details
  INNER JOIN [Account Status] ON Details.Customer_ID = [Account Status].Customer_ID
WHERE ((([Account Status].Account_Status)='Active'))
GROUP BY Details.Age, [Account Status].Account_Status
PIVOT Details.site;

我的结果是计算这样的活跃帐户:

Age Range          Site 1  Site 2  Site 3
-----------------  ------  ------  ------
Under 25                   1
Under 25                   1
Between 25 And 32                  1
Between 25 And 32                  1
Between 25 And 32                  1
Between 25 And 32                  1

什么时候应该是这样的:

Age Range          Site 1  Site 2  Site 3
-----------------  ------  ------  ------
Under 25                   2
Between 25 And 32                  4

我也尝试过使用Switch语句进行分组,但它不起作用。请帮忙。

4 个答案:

答案 0 :(得分:3)

其他人给出的答案在概念上是正确的。这是你可以使用的SQL

TRANSFORM Count(Details.Customer_ID) AS CountOfCustomer_ID
SELECT Switch(
        [Age] < 25,                  "Under 25",
        [Age] >= 25 And [Age] <= 32, "Between 25 And 32",
        [Age] >= 32 And [Age] <= 40, "Between 32 And 40",
        [Age] >= 40 And [Age] <= 45, "Between 40 And 45",
        [Age] > 45,                  "Over 45"
      ) AS Age_Range
FROM   details 
       INNER JOIN [Account Status] 
         ON details.customer_id = [Account Status].customer_id 
WHERE  (( ( [Account Status].account_status ) = 'Active' )) 
GROUP  BY Switch(
        [Age] < 25,                  "Under 25",
        [Age] >= 25 And [Age] <= 32, "Between 25 And 32",
        [Age] >= 32 And [Age] <= 40, "Between 32 And 40",
        [Age] >= 40 And [Age] <= 45, "Between 40 And 45",
        [Age] > 45,                  "Over 45"
      )
PIVOT Details.site;          

如果您想使用内联查询来简化事情,那么这将起作用

TRANSFORM Count(Customer_ID) AS CountOfCustomer_ID
SELECT 
    Age_range 
 FROM 
(
SELECT Switch(
        [Age] < 25,                  "Under 25",
        [Age] >= 25 And [Age] <= 32, "Between 25 And 32",
        [Age] >= 32 And [Age] <= 40, "Between 32 And 40",
        [Age] >= 40 And [Age] <= 45, "Between 40 And 45",
        [Age] > 45,                  "Over 45"
      ) AS Age_Range, details.customer_id , details.site
FROM   details 
       INNER JOIN [Account Status] 
         ON details.customer_id = [Account Status].customer_id 
WHERE  (( ( [Account Status].account_status ) = 'Active' )) ) t
GROUP BY Age_range

PIVOT site;   

答案 1 :(得分:0)

你试过了吗?

GROUP BY Age_Range 

答案 2 :(得分:0)

当您GROUP BY Details.Age时,结果将在运行SELECT语句之前组合在一起。所以你并没有真正按照自己的想法进行分组。相反,它是在原始时代进行分组。

因此,您希望将18,19和21组合在一起,它们实际上被分组到各自独立的组中。

您必须在GROUP BY之前获取Age_Range ....也许在SELECT的嵌套FROM语句中?

SELECT a.Age_Range, COUNT(*)
FROM
    (TRANSFORM Count(Details.Customer_ID) AS CountOfCustomer_ID
    SELECT
      Switch(
        [Age] < 25,                  "Under 25",
        [Age] >= 25 And [Age] <= 32, "Between 25 And 32",
        [Age] >= 32 And [Age] <= 40, "Between 32 And 40",
        [Age] >= 40 And [Age] <= 45, "Between 40 And 45",
        [Age] > 45,                  "Over 45"
      ) AS Age_Range
    FROM Details
      INNER JOIN [Account Status] ON Details.Customer_ID = [Account Status].Customer_ID
    WHERE ((([Account Status].Account_Status)='Active'))
    PIVOT Details.site) a
GROUP BY a.Age_Range

抱歉,我从未使用过TRANSFORMPIVOT所以我不确定这是怎么回事。但你可以看到这个想法。嵌套SELECT抓取原始结果,然后外部SELECT执行GROUP BY

答案 3 :(得分:0)

首先,我想指出你的范围条件不正确,它们是重叠的。

您定义的范围将导致32或40岁的某个人进入多个范围:

[Age] >= 25 And [Age] <= 32, "Between 25 And 32",
[Age] >= 32 And [Age] <= 40, "Between 32 And 40",
[Age] >= 40 And [Age] <= 45, "Between 40 And 45",

范围必须是:

[Age] >= 25 And [Age] <= 32, "Between 25 And 32",
[Age] > 32 And [Age] <= 40, "Between 33 And 40",
[Age] > 40 And [Age] <= 45, "Between 41 And 45",

免责声明:我没有MS Access,只有SQL Server,这意味着我无法使用TRANSFORM操作。我发布这个答案是为了演示在没有SWITCH语句的情况下获得所需结果的另一种方法。

创建年龄范围查找表,而不是使用SWITCH语句;如果您的年龄范围发生变化,这也有利于更新。

CREATE TABLE Age_Ranges
(
    age_range_name VARCHAR(20) NOT NULL PRIMARY KEY,
    age_lower TINYINT NOT NULL,
    age_upper TINYINT NOT NULL
)

INSERT Age_Ranges(age_range_name, age_lower, age_upper) VALUES ('Under 25', 0, 24)
INSERT Age_Ranges(age_range_name, age_lower, age_upper) VALUES ('Between 25 And 32', 25, 32)
INSERT Age_Ranges(age_range_name, age_lower, age_upper) VALUES ('Between 33 And 40', 33, 40)
INSERT Age_Ranges(age_range_name, age_lower, age_upper) VALUES ('Between 41 And 45', 41, 45)
INSERT Age_Ranges(age_range_name, age_lower, age_upper) VALUES ('Over 45', 46, 120)
INSERT Age_Ranges(age_range_name, age_lower, age_upper) VALUES ('Error! Bad age data', 121, 255)

由于我无法使用TRANSFORM语句,因此我创建了一个矩阵表来执行转换。

CREATE TABLE Site_Matrix
(
    site_nbr TINYINT NOT NULL PRIMARY KEY ,
    site_1 TINYINT DEFAULT 0 NOT NULL,
    site_2 TINYINT DEFAULT 0 NOT NULL,
    site_3 TINYINT DEFAULT 0 NOT NULL
)

INSERT Site_Matrix(site_nbr,site_1) VALUES(1,1)
INSERT Site_Matrix(site_nbr,site_2) VALUES(2,1)
INSERT Site_Matrix(site_nbr,site_3) VALUES(3,1)

最后,一些客户测试数据。

CREATE TABLE Customer_Details
(
    customer_id INT NOT NULL PRIMARY KEY,
    customer_age TINYINT DEFAULT 255 NOT NULL,
    customer_site tinyint DEFAULT 0 NOT NULL
)

INSERT Customer_Details(customer_id, customer_age, customer_site) VALUES(1, 18, 2)
INSERT Customer_Details(customer_id, customer_age, customer_site) VALUES(2, 19, 2)
INSERT Customer_Details(customer_id, customer_age, customer_site) VALUES(3, 25, 3)
INSERT Customer_Details(customer_id, customer_age, customer_site) VALUES(4, 27, 3)
INSERT Customer_Details(customer_id, customer_age, customer_site) VALUES(5, 29, 3)
INSERT Customer_Details(customer_id, customer_age, customer_site) VALUES(6, 32, 3)

使用这些表格可以获得此查询所需的结果。由于没有必要演示此方法,我已将联接遗漏到帐户状态:

SELECT age_range_name, 
       SUM(site_1) AS site_1,
       SUM(site_2) AS site_2,
       SUM(site_3) AS site_3
FROM Customer_Details INNER JOIN Age_Ranges
    ON customer_age BETWEEN age_lower AND age_upper, Site_Matrix
WHERE customer_site = site_nbr
GROUP BY age_range_name

查询输出:

age_range_name     site_1  site_2  site_3
Between 25 And 32  0       0       4
Under 25           0       2       0

我确信SQL需要更改才能在Access中运行,对不起,我希望这能为您提供一些解决问题的方法。