我是 sql 菜鸟,我不知道如何继续这个查询。
我有这张表 m2AndPrices。它包含资产及其相关的表面和价值。
|---------------------------------------------|
|id | code | m2 | value |
|---------------------------------------------|
|32980 | 143452asd10 | 11.17 | 0 |
|---------------------------------------------|
|25107 | 20152fgg219 | 57.00 | 55304,99 |
|---------------------------------------------|
|5692 | 203hhh75735 | 87.18 | 98334,16 |
|---------------------------------------------|
|31500 | 200788fgfg193 | 240.64 | 164401,75 |
|---------------------------------------------|
按平方米(m2)分组并计算我得到这个输出:
|----------------------------------|
|m2section | number_of_assets |
|----------------------------------|
|<1 | 175 |
|----------------------------------|
|1-5 | 286 |
|----------------------------------|
|5-10 | 374 |
|----------------------------------|
|10-20 | 573 |
|----------------------------------|
|20-40 | 5212 |
|----------------------------------|
|40-80 | 3892 |
|----------------------------------|
|80-120 | 4121 |
|----------------------------------|
|120-180 | 1849 |
|----------------------------------|
|180-250 | 705 |
|----------------------------------|
|250-500 | 430 |
|----------------------------------|
| >500 | 131 |
|----------------------------------|
编码:
SELECT * FROM (
SELECT cast(m2section as varchar), COUNT(*) as number_of_assets FROM (
SELECT
CASE
WHEN m2 <= 1 THEN '<1'
WHEN m2 > 1 AND m2 <= 5 THEN '1-5'
WHEN m2 > 5 AND m2 <= 10 THEN '5-10'
WHEN m2 > 10 AND m2 <= 20 THEN '10-20'
WHEN m2 > 20 AND m2 <= 40 THEN '20-40'
WHEN m2 > 40 AND m2 <= 80 THEN '40-80'
WHEN m2 > 80 AND m2 <= 120 THEN '80-120'
WHEN m2 > 120 AND m2 <= 180 THEN '120-180'
WHEN m2 > 180 AND m2 <= 250 THEN '180-250'
WHEN m2 > 250 AND m2 <= 500 THEN '250-500'
WHEN m2 > 500 THEN '>500' END AS m2section
FROM (select * from m2AndPrices x) as a
) as A
GROUP BY m2section) as B
order by CASE
WHEN m2section = '<1' THEN 1
WHEN m2section = '1-5' THEN 2
WHEN m2section = '5-10' THEN 3
WHEN m2section = '10-20' THEN 4
WHEN m2section = '20-40' THEN 5
WHEN m2section = '40-80' THEN 6
WHEN m2section = '80-120' THEN 7
WHEN m2section = '120-180' THEN 8
WHEN m2section = '180-250' THEN 9
WHEN m2section = '250-500' THEN 10
WHEN m2section = '>500' THEN 11
END
;
很抱歉,如果它没有很好的缩进。
我需要得到另一列来计算每个范围内元素数量的值,如下所示:
|------------------------------------------------------|
|m2section | number_of_assets | total value ($)
|------------------------------------------------------|
|<1 | 175 | 345.436
|------------------------------------------------------|
|1-5 | 286 | 864.364
|------------------------------------------------------|
|5-10 | 374 | 364.334
|------------------------------------------------------|
|10-20 | 573 | 1.364.364
|------------------------------------------------------|
|20-40 | 5212 | 8.364.364
|------------------------------------------------------|
|40-80 | 3892 | 6.364.364
|------------------------------------------------------|
|80-120 | 4121 | .....
|------------------------------------------------------|
|120-180 | 1849 | .....
|------------------------------------------------------|
|180-250 | 705 | .....
|------------------------------------------------------|
|250-500 | 430 | .....
|------------------------------------------------------|
| >500 | 131 | .....
|------------------------------------------------------|
但我不知道在哪里可以计算价值($)。
任何想法都会受到赞赏。
非常感谢