我需要用SQL计算人口密度。我有一列人口,一列面积,但我不知道如何执行它,我得到除以零误差。
这是我尝试过的:
import React, {useState, useRef, useEffect} from 'react';
import products from './mock.json';
export default function App() {
return (
<div style={{display: 'flex', flexDirection: 'row'}}>
<ShoppingCart products={products} />
</div>
);
}
function List({products, title, addProduct, removeProduct}) {
return (
<div
style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'start-end',
}}
>
{title}
<ul
style={{
listStyle: 'none',
paddingInlineStart: '0px',
width: '500px',
height: '260px',
overflow: 'scroll',
paddingRight: '10px',
}}
>
{products.length === 0 ? (
<div>No products</div>
) : (
products.map(({category, price, stocked, name}, index) => (
<SuspendDisplayIfOverflowing>
{ref => (
<li
style={{
padding: '10px',
border: '1px solid blue',
display: 'flex',
justifyContent: 'space-between',
height: '18px',
overflow: 'scroll',
}}
ref={ref}
key={price}
>
<div style={{color: `${stocked ? 'green' : 'red'}`}}>
{`${name} - ${category} - available: ${stocked} - price: ${price}`}
</div>
{addProduct && stocked ? (
<button
onClick={() =>
addProduct({category, price, stocked, name})
}
>
Add
</button>
) : null}
{removeProduct ? (
<button onClick={() => removeProduct(index)}>Remove</button>
) : null}
</li>
)}
</SuspendDisplayIfOverflowing>
))
)}
</ul>
<Total prices={products.map(({price}) => getCleanPrice(price))} />
</div>
);
}
function ShoppingCart({products}) {
const [productsInCart, manipulateProduct] = useState([]);
const addProduct = product =>
manipulateProduct(productsInCart.concat(product));
const removeProduct = productIndex =>
manipulateProduct(
productsInCart.filter((_, index) => index !== productIndex),
);
return (
<>
<List
title="Available Products"
products={products}
addProduct={addProduct}
/>
<List
title="Shopping Cart"
products={productsInCart}
removeProduct={removeProduct}
/>
</>
);
}
function Total({prices}) {
const total = prices.reduce((accum, item) => (accum += parseFloat(item)), 0);
return <div>Total amount to pay: {total.toFixed(2)}</div>;
}
function getCleanPrice(price) {
return price.slice(1);
}
function SuspendDisplayIfOverflowing(props) {
const overflowingRef = useRef(null);
useEffect(() => {
console.log(
'oveflowing scroll height',
overflowingRef.current.scrollHeight,
);
console.log(
'oveflowing client height',
overflowingRef.current.clientHeight,
);
console.log(
'oveflowing client height',
overflowingRef.current.offsetHeight,
);
console.log(overflowingRef.current);
}, [overflowingRef]);
return props.children(overflowingRef);
}
答案 0 :(得分:1)
您为什么需要group by
?
无需分组。
即使您确实需要group by
,也无法选择非聚集列,例如:
population, gdp, area
。
只需检查null
列中的0
或area
:
select
name, population, gdp, area,
case coalesce(area, 0)
when 0 then null
else population / area
end as pop_density
from country
order by gdp
答案 1 :(得分:0)
使用nullif()
:
select name, population, gdp, area,
population / nullif(area, 0) as pop_density
from country
order by gdp;
group by
似乎没有必要。
nullif()
是一个标准函数,当传入的两个值相等时会返回NULL
。如果您的数据库不支持它,则可以使用case
表达式来做同样的事情。
答案 2 :(得分:0)
只需添加where clause as
select name, population, gdp, area,
[population]/[area] as pop_density
from country where area
NOT IN (NULL,0) group by name
order by gdp
或者,您可以通过Case
语句来处理除数区域的值0 ...
.......Case when Area IN (Null,0)
then 0
else
[population]/[area]
答案 3 :(得分:0)
select name, population, gdp, area, [population]/[area] as pop_density
from country
where area is not null and area is not 0
group by name
order by gdp;