我有以下表格:
id activity xuser isDone
---------------------------------------
1 abc tom y
2 def tom n
3 hij jeff y
4 klm jeff n
5 nop jeff n
我想有以下結果:
nCol tom jeff
----------------------------
完成 1 1
undone 1 2
我怎樣才能用SQL語法做到這一點?然后我如何將其轉換為linq語法?
謝謝你。
uj5u.com熱心網友回復:
回傳列(xuser, donecount, undonecount)要靈活得多。
做一個GROUP BY,使用case 運算式來做條件聚合:
select xuser,
sum(case when isDone = 'y' then 1 else 0 end) as donecount,
sum(case when isDone = 'n' then 1 else 0 end) as undonecount
from tablename
group by xuser
(必須由其他人來做linq編碼。)
uj5u.com熱心網友回復:
如果你最終做了類似@jarlh 在他的答案中建議的事情,你可以在LINQ中通過一個查詢實作類似的SQL結果,大致是這樣的:
var xUserCounts =
from x in tableName
group x by x.xuser into groupped
選擇 新
{
User = groupped.Key,
DoneCount = groupped.Select(y => y.IsDone).Sum(y => y == "y"/span> ? 1 : 0)。)
UndoneCount = groupped.Select(y => y.IsDone).Sum(y => y == "n"/span> ? 1 : 0)。)
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/310054.html
標籤:
