2014-07-12 65 views
1

我有以下Postgres的查詢:格式的Postgres row_to_json響應查詢

"SELECT \"responses\".\"index\", \"responses\".\"created_at\", 
ROUND(AVG(\"responses\".\"numeric\")) AS numeric 
FROM \"responses\" 
WHERE \"responses\".\"time\" = '#{time}' 
GROUP BY \"responses\".\"index\", \"responses\".\"created_at\"" 

我試圖輸出JSON使用row_to_json的響應。我可以使用:

"select row_to_json(row) 
from (
    SELECT \"responses\".\"index\", \"responses\".\"created_at\", 
    ROUND(AVG(\"responses\".\"numeric\")) AS numeric 
    FROM \"responses\" 
    WHERE \"responses\".\"time\" = '#{time}' 
    GROUP BY \"responses\".\"index\", \"responses\".\"created_at\" 
) row" 

哪位能給我:

{"row_to_json"=>"{\"index\":1,\"created_at\":\"2014-07-12 03:51:00\",\"numeric\":3}"} 

但是我真的不希望嵌套在row_to_json哈希的響應。有沒有一種簡單的方法來去除,所以我剛剛返回:

"{\"index\":1,\"created_at\":\"2014-07-12 03:51:00\",\"numeric\":3}" 
+2

的PostgreSQL d不要像這樣返回json。您可能正在查看您的訪問語言(PHP?)或抽象層(PDO?) –

+0

@ MikeSherrill'CatRecall有趣的工件。我正在使用Ruby。我會更多地考慮它。 – tvalent2

回答