我只需要知道如何获得明智的年度总数即可。 这是我的代码,需要获得以下预期结果:
SELECT /*+parallel(v,32) +parallel(mm,32) +parallel(c,32) */
'BUS' VEH_TYPE,
MM.MAKE_MO,
SUM (CASE WHEN SUBSTR (V.YEAR_Val, 1, 4) = '1990' THEN 1 ELSE 0 END)
AS FY1990,
SUM (CASE WHEN SUBSTR (V.YEAR_VAL, 1, 4) = '1991' THEN 1 ELSE 0 END)
AS FY1991,
SUM (CASE WHEN SUBSTR (V.YEAR_VAL, 1, 4) = '1992' THEN 1 ELSE 0 END)
AS FY1992,
SUM (CASE WHEN SUBSTR (V.YEAR_VAL, 1, 4) = '1993' THEN 1 ELSE 0 END)
AS FY1993,
SUM (CASE WHEN SUBSTR (V.YEAR_VAL, 1, 4) = '1994' THEN 1 ELSE 0 END)
AS FY1994,
SUM (CASE WHEN SUBSTR (V.YEAR_VAL, 1, 4) = '1995' THEN 1 ELSE 0 END)
AS FY1995,
COUNT (MM.MAKE_MO) Total_Count
FROM VEHICLE V, MAKE mm, CATEGORY c
WHERE C.MAKE_MO = mm.MAKE_MO
AND c.CATEGORY_CD = v.CATEGORY_CD
AND V.p_MONTH BETWEEN '199001' AND '199501' --using this as data is huge
AND C.TYPE_CODE=01
AND MM.VEH_CODE='B'
GROUP BY 'BUS,'MM.MAKE_MO, V.YEAR_Val
ORDER BY V.YEAR_Val;
这是预期的结果:
VEH TYPE YEAR MODEL TOTAL
------- ----- --------
Bus 1990 7,808,658
Bus 1991 5,474,809
Bus 1992 54,839,221
Bus 1993 54,680,000
Bus 1994 15,000,000
Bus 1995 17,899,668
答案 0 :(得分:0)
评论太久了。展开此查询后,您似乎只是在尝试按车型年份来计算“公共汽车”的数量(<div *ngIf="array['data']">
//business logic
</div>
表中的所有车辆都被标记为“公共汽车”)。如果是这样,您可以大大简化查询。
VEHICLE
如果您的车龄只有1990-1995,则可能不需要SELECT VEH_TYPE
, theYear AS YEAR_MODEL
, COUNT(*) AS TOTAL
FROM (
SELECT 'BUS' AS VEH_TYPE
, SUBSTR(V.YEAR_Val,1,4) AS theYear
FROM VEHICLE V
INNER JOIN CATEGORY c ON v.CATEGORY_CD = c.CATEGORY_CD
AND C.TYPE_CODE='01'
INNER JOIN MAKE mm ON c.MAKE_MO = mm.MAKE_MO
AND MM.VEH_CODE='B'
WHERE V.p_MONTH BETWEEN '199001' AND '199501'
) s1
WHERE theYear IN ('1990','1991','1992','1993','1994','1995')
GROUP BY theYear
子句。如果WHERE
和VEHICLE
中没有CATEGORY_CD
匹配项,并且如果没有VEHICLE
匹配项,那么JOINed表除了限制CATEGORY
表外似乎没有做任何其他事情MAKE_MO
和MAKE
中的CATEGORY
个匹配项。如果您要过滤CATEGORY
或MAKE
中的特定列,或者您在VEHICLE
中有孤立的记录,那么您想将这些JOIN添加回去。否则,我不会认为他们真的在做什么。
请注意,SUBSTR(V.YEAR_Val,1,4)
会排除您在V.YEAR_Val
上拥有的任何索引,如果您经常需要该列,或者如果有很多VEHICLE
记录,则可能需要将该值分成自己的列。