[英]in factorygirl, any way to refer to the value of field1 when initializing field2?
[英]rails 3 + activerecord: is there a single query to count(field1) grouped by field2?
我正在嘗試找到匯總表中數據的最佳方法
我有一個帶有字段的表信息
id
region_number integer (NOT associated with another table)
member_name string
member_active T/F
成員屬於一個地區,有一個名字,並且活躍與否。
我想知道是否有單個查詢將創建一個具有3列的表,並且具有唯一的region_numbers的行數:
For each unique region_number:
region_number
COUNT of members in that region
COUNT of members in that region with active=TRUE
假設我有50個區域,我知道如何使用2x50查詢,但這肯定不是正確的方法!
如果您准備進行一點后處理,則可以始終將幾件事分組:
SELECT region_number, COUNT(*) AS instances, member_active
GROUP BY region_number, member_active
WHERE region_number IN ?
這樣一來,您就可以一次查詢所有區域號。 T
值將有一行, F
則有一行,但前提是存在它們。
如果您看到正在執行很多僅在標識符上有所不同的查詢的情況,那么通常可以像這樣一次執行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.