sample = {
'13-18':50,
'19-25':60,
'25-40':50,
'50-80':100
}
我想找到屬于上述類別的用戶數,即13-18, 19-25分別從年齡開始,我將如何使用SQLalchemy 進行查詢,我只有出生日期欄位可以使用
dob : 07/10/2005
DOB是上述格式
uj5u.com熱心網友回復:
您應該添加更多詳細資訊。你的資料庫是什么(postres、mysql、其他?)?根據資料庫,有不同的內置函式集。列的型別dob是什么?它是字串還是日期時間。在 postgreSQL 中,您的實作可能是:
select
case
when age < 13 then '0-12'
when age >= 13 and age < 25 then '13-25'
when age >= 25 and age < 40 then '25-40'
when age >= 41 and age < 80 then '41-80'
else '80 '
end as range,
count(*)
from (
SELECT
1 extract('years' from justify_interval(now() - to_date(dob, 'DD/MM/YYYY'))) as age
from table
) x
group by range
您可以將其重寫為 sqlalchemy 語法,但這很棘手。您還可以在 sqlalchemy 中使用原始 sql 執行,例如:
from sqlalchemy.sql import text
statement = text(<sql_above>)
result = session.execute(statement)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/377672.html
標籤:sql 烧瓶 sqlalchemy
上一篇:docker-compose構建失敗,未找到檔案但檔案實際存在
下一篇:在Flask中啟動和停止執行緒
