2013-04-11 43 views
0

我想在我的grails應用程序中繪製一些圖表,但在此之前,我必須從數據庫獲取數據以提供圖表,但我仍然無法弄清楚如何獲取我需要的數據正確的方法。查詢獲取日期或月份等數據等?在grails中

我有一個起始日期和結束日期,限制了期,我想按天或周,月,年等

得到的數據我覺得從開始日期進行一個循環結束日期步驟等於一天/周/月/年等。

您認爲這是正確的做法嗎?如果是的話該怎麼寫在Groovy中,如果沒有請告訴怎麼做這正確的方式

感謝

+0

從數據庫grails檢索數據提供了不同的方法,例如,查看文檔中的動態查找器,createCriteria,executeQuery,並查看哪一個更適合您的需要。如果你在這裏有某種代碼,我相信人們會幫助你。 – Alidad 2013-04-11 11:07:08

回答

1

我喜歡在這種情況下,做的是組和總結在查詢本身的結果,只是就像你會在純sql中使用sum()和group by子句一樣。 Grails基於Hibernate,而Hibernate則作爲這種情況非常適用的select new map()語法。

這裏是Grails的一個例子按月選擇金融總量(如金融交易):

def query = """ 
    select new map(sum(tx.value) as total, month(tx.date) as month, year(tx.date) as year) 
    from Transaction tx 
    where tx.date >= :beginDate and tx.date < :endDate 
    group by month(tx.date), year(tx.date) 
    order by year(tx.date) desc , month(tx.date) desc 
""" 
def result = Transaction.executeQuery(query,[beginDate:someDate, endDate:anotherDate]) 

與該代碼你可以在你的result可變環,並獲得每個結果項目這是地圖。像這樣:

result.each{ item -> 
    println "Financial total for ${item.month}/${item.year} is equal to ${item.total}" 
} 

我認爲這是非常簡單的,並以點解決方案。 Regards ...

相關問題