2013-05-07 118 views
0

朋友。我對以下查詢感到困惑。從sqlite數據庫中選擇月份

c = db.rawQuery("SELECT strftime('%W', tm.txn_date) AS week, 
       sum(case when cm.master_id = 1 then (tm.amount - tm.pre_amount) else 0 end) AS Income, 
       sum(case when cm.master_id = 2 then (tm.pre_amount - tm.amount) else 0 end) AS Expense 
       FROM transmaster tm 
       INNER JOIN subcatmasters scm ON tm.scmaster_id = scm.id 
       INNER JOIN catmasters cm ON scm.catmaster_id = cm.id 
       WHERE strftime('%m', tm.txn_date) = '05' 
       GROUP BY week ORDER BY week DESC", null); 

這一個完美的工作,但。

c = db.rawQuery("SELECT strftime('%W', tm.txn_date) AS week, 
       sum(case when cm.master_id = 1 then (tm.amount - tm.pre_amount) else 0 end) AS Income, 
       sum(case when cm.master_id = 2 then (tm.pre_amount - tm.amount) else 0 end) AS Expense 
       FROM transmaster tm 
       INNER JOIN subcatmasters scm ON tm.scmaster_id = scm.id 
       INNER JOIN catmasters cm ON scm.catmaster_id = cm.id 
       WHERE strftime('%m', tm.txn_date) = '"+05+"' 
       GROUP BY week ORDER BY week DESC", null); 

這一個沒有工作。什麼是朋友的問題。

我只是改變WHERE strftime('%m', tm.txn_date) = '05'這一行到WHERE strftime('%m', tm.txn_date) = '"+05+"'爲什麼,因爲我想明確給月份編號。

請幫幫我。 在此先感謝。

回答

1

試試這個:

c = db.rawQuery("SELECT strftime('%W', tm.txn_date) AS week, 
       sum(case when cm.master_id = 1 then (tm.amount - tm.pre_amount) else 0 end) AS Income, 
       sum(case when cm.master_id = 2 then (tm.pre_amount - tm.amount) else 0 end) AS Expense 
       FROM transmaster tm 
       INNER JOIN subcatmasters scm ON tm.scmaster_id = scm.id 
       INNER JOIN catmasters cm ON scm.catmaster_id = cm.id 
       WHERE strftime('%m', tm.txn_date) = '0"+5+"' 
       GROUP BY week ORDER BY week DESC", null); 

怎麼樣?

+0

怎麼樣呢? – 2013-05-07 14:42:16

+0

啊!我的偉大。謝謝你的朋友 – Gunaseelan 2013-05-07 14:49:59

+0

不客氣,但要小心,對於像10,11和12這樣的月份,別忘了不要加零。 – user2358253 2013-05-07 14:53:59

1

你應該做的事情更清潔,聲明一個變量爲您的月數:

String monthNumber = "05"; 
String request = "SELECT strftime('%W', tm.txn_date) AS week, " + 
       "sum(case when cm.master_id = 1 then (tm.amount - tm.pre_amount) else 0 end) AS Income, " + 
       "sum(case when cm.master_id = 2 then (tm.pre_amount - tm.amount) else 0 end) AS Expense " + 
       "FROM transmaster tm " + 
       "INNER JOIN subcatmasters scm ON tm.scmaster_id = scm.id " + 
       "INNER JOIN catmasters cm ON scm.catmaster_id = cm.id " + 
       "WHERE strftime('%m', tm.txn_date) = '" + monthNumber + "' " + 
       "GROUP BY week ORDER BY week DESC"; 
c = db.rawQuery(request, null); 
相關問題