Assume the following table with 3 numeric fields:
Images (table)
--------------
Width
Height
Amount
Width and Height are image sizes in millimeters. Now I want to create a report about the amount of images grouped by their dimensions in centimeters. This means that I have to GROUP BY 2 non existing columns.
I can do:
SELECT ROUND(Width/10,0) AS W
, ROUND(Height/10,0) AS H
, SUM(Amount) AS A
FROM IMAGES
GROUP BY Width
, Height
ORDER BY W
, H
, A
but this will do the mm to cm conversion only on the view level and will result in more than one row for same dimensions.
eg
W H A
--- --- -
150 159 2
150 160 1
will not result in 1 category
W H A
-- -- -
15 16 3
but in
W H A
-- -- -
15 16 2
15 16 1
The targeted engine is actually a FileMaker database, that unfortunatly does not seem to support aggregate functions within the GROUP BY clause.
Simply GROUP BY
your calculated columns:
SELECT
ROUND(Width/10,0) as W
,ROUND(Height/10,0) as H
,COUNT(*) as A -- You may replace this with SUM(Amount) too
FROM
IMAGES
GROUP BY
ROUND(Width/10,0)
,ROUND(Height/10,0)
ORDER BY
W
,H
,A
EDIT: Also, from what I understand of your question you want the COUNT
not the SUM
of the rows..., right?
Following query uses Common Table Expression (CTE)
to convert width and height from mm to cm along with rounding and then produces a derived table output. This CTE output is then used to group the width and height values to calculate the SUM of the amount column.
I am not sure if you were looking for SUM
of the amount column or the COUNT
of the amount column. I assumed SUM based on your query. If you only want COUNT , please change SUM in the query to COUNT .
Click here to view the demo in SQL Fiddle
Script :
CREATE TABLE images
(
width INT NOT NULL
, height INT NOT NULL
, amount FLOAT NOT NULL
);
INSERT INTO images (width, height, amount) VALUES
(150, 159, 1),
(150, 159, 2),
(150, 158, 1),
(150, 159, 3),
(150, 158, 2),
(160, 158, 4),
(160, 158, 1);
;WITH imagesincm AS
(
SELECT ROUND(width / 10,0) AS W
, ROUND(height / 10,0) AS H
, amount
FROM images
)
SELECT W
, H
, COUNT(amount) AS A
FROM imagesincm
GROUP BY W
, H
ORDER BY W
, H
Output :
W H A
-- -- -
15 15 9
16 15 5
根据官方 FileMaker SQL 参考 ( https://help.claris.com/en/sql-reference.pdf ) 的第 11 页,Group By 子句仅适用于字段。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.