2016-05-05 86 views
1

對於航程(年,船),如何按標準SQL或MS-ACCESS中的年齡間隔對水手進行分組和計數?按年齡段劃分的組和計數

YEAR SHIP   SAILOR_AGE 
2003 Flying dolphin 33 
2003 Flying dolphin 33 
2003 Flying dolphin 34 
2001 Flying dolphin 23 
2003 Flying dolphin 35 
2001 Flying dolphin 38 
2001 Flying dolphin 31 
2003 Flying dolphin 36 
2003 Columbine  41 
2003 Columbine  42 
2003 Flying dolphin 27 
2003 Flying dolphin 51 
2003 Flying dolphin 46 

我試了一下:

SELECT YEAR, SHIP, SAILOR_AGE, COUNT (*) as `NUMBERS` 
FROM TABLE 
GROUP BY YEARS, SHIP, SAILOR_AGE; 

它給我的水手每年數:

例子:

YEAR |SHIP   |SAILOR_AGE | NUMBERS 
------------------------------------------ 
2003 | Flying dolphin| 33   | 2 

如何能集團水手年齡由intervalles

E xample:

From 20th to 40th year's old 
From 40th to 60th year's old 
+0

你爲什麼不使用'CASE..WHEN'? – MusicLovingIndianGirl

回答

1

您可以使用單個查詢使用CASE EXPRESSION

SELECT t.years,t.ship, 
     CASE WHEN t.Sailor_age between 21 and 40 then 'From 20th to 40th' 
      WHEN t.Sailor_age between 41 and 60 then 'From 40th to 60th' 
      ELSE 'Other Ages' 
     END as Range 
FROM YourTable t 
GROUP BY t.years,t.ship,CASE WHEN t.Sailor_age between 21 and 40 then 'From 20th to 40th' WHEN t.Sailor_age between 41 and 60 then 'From 40th to 60th' ELSE 'Other Ages' END 
ORDER BY t.years,t.ship,Range 

或者,如果您使用條件聚集想把它當作一個單行:

SELECT t.years,t.ship, 
     COUNT(CASE WHEN t.Sailor_age between 21 and 40 then 1 end) as Age_21_to_40, 
     COUNT(CASE WHEN t.Sailor_age between 41 and 60 then 1 end) as Age_41_to_60, 
FROM YourTable t 
GROUP BY t.years,t.ship 
ORDER BY t.years,t.ship 
2

你可以嘗試使用CASE在以下指定年齡範圍:

SELECT YEARS, SHIP, AgeRange, COUNT (*) as `NUMBERS` 
FROM (SELECT YEAR 
      , SHIP 
      , CASE WHEN SAILOR_AGE BETWEEN 21 AND 40 THEN '21-40' 
        WHEN SAILOR_AGE BETWEEN 41 AND 60 THEN '41-60' 
        ELSE 'XX-XX' -- anything goes here 
       END as AgeRange 
     FROM YourTable 
)x 
GROUP BY YEARS, SHIP, AgeRange; 
2

如果你想在列中的值,你可以使用條件彙總:

SELECT YEAR, SHIP, COUNT(*) as NUMSAILORS, 
     SUM(CASE WHEN SAILOR_AGE BETWEEN 21 AND 40 THEN 1 ELSE 0 END) as AGE_21_40, 
     SUM(CASE WHEN SAILOR_AGE BETWEEN 41 AND 60 THEN 1 ELSE 0 END) as AGE_41_60 
FROM TABLE 
GROUP BY YEARS, SHIP; 
1

我想說實現,最好的方法是創建幫手錶:

DECLARE @hlptbl TABLE(DescOfGrp VARCHAR(50), AgeFrom INT, AgeTo INT) 
INSERT INTO @hlptbl (DescOfGrp, AgeFrom, AgeTo) 
VALUES('20 to 40', 20, 40), 
('41 to 60', 40, 60) 

然後,你有加入的數據是這樣的:

SELECT t1.[Year], t1.SHIP, t2.DescOfGrp, COUNT(t1.SHIP) AS Number 
FROM TableA As t1 INNER JOIN @hlptbl AS t2 ON 
      t1.SAILOR_AGE>=t2.AgeFrom AND t1.SAILOR_AGE <=t2.AgeTo 
GROUP BY t1.[Year], t1.SHIP, t2.DescOfGrp 

結果:

Year SHIP DescOfGrp Number 
2001 Flying dolphin 20 to 40 3 
2003 Columbine  41 to 60 2 
2003 Flying dolphin 20 to 40 6 
2003 Flying dolphin 41 to 60 2 
2

由於這 - 定的標籤 - 最有可能是訪問SQL,你可以去這樣的:

SELECT 
    YEAR, 
    SHIP, 
    INT(SAILOR_AGE/20) * 20 AS AgeGroup, 
    COUNT(*) As [NUMBERS] 
FROM 
    TABLE 
GROUP BY 
    YEAR, 
    SHIP, 
    INT(SAILOR_AGE/20) * 20 
+0

它的效果很好,但是對於我來說,這種情況不太清楚。 – Wilcar

+0

也許,但_Case-When_由Access SQL未知。 – Gustav

+0

我想將我的Access數據庫轉換爲sqlite數據庫,但我沒有找到一個免費且快速的工作轉換工具... – Wilcar