F # Query, Group Values ​​for Multiple Columns

I have a F # sql query that should sum two columns in each group.

let financials = query{
        for data in dbData do
        groupValBy (data.earning, data.losses) data.store into group
        select (group.Key, group.Sum(fun (group) -> fst group), group.Sum(fun (group) -> snd group))
    }

I could not find examples of how to do this, however, this code compiles fine, and for every sign it should work. However, when I execute, I always get an exception with this message: "Failed to format node" New "for execution as SQL." If I delete data.losses everything works fine, but adding a second value causes an error. Is there any other way to do this? Suggestions will be appreciated.

Note. This question is similar, but handles the opposite situation in which multiple columns are grouped : group multiple columns in a F # 3.0 query

UPDATE:

I also tried the following:

(NewAnonymousObjectHelper(T(data.earning, data.losses)))

where T:

type T(earning : decimal, losses : decimal) =
    member val Earning=earning with get,set
    member val Losses=losses with get,set

Sum, T. MutableTuple F # PowerPack, MutableTuple ( /dll ). , :

let t = MutableTuple<_,_>(item1=data.earning,item2=data.losses)
groupValBy t data.store into group

, - , .

:

, , - . , group.Sum() . (: , , sql.)

+4
2

, - , :

let financials =
    query {
        for data in dbData do
        groupValBy (data.earning, data.losses) data.store into group
        let earnings = query { for (e,_) in group do sumBy e }
        let losses = query { for (_,l) in group do sumBy l }
        select (group.Key, earnings, losses) }

. sumByNullable.

+2

, groupBy, linq .

type Data =
    {Store : string; Earning : float; Losses : float }

let dbData =
    [| { Store = "Store A"; Earning = 5.0; Losses = 2.0 }
       { Store = "Store A"; Earning = 10.0; Losses = 3.0 }
       { Store = "Store B"; Earning = 3.0; Losses = 1.0 } |]

let financials =
    query {
        for data in dbData do
        groupBy data.Store into group
        let earnings = query { for g in group do sumBy g.Earning }
        let losses = query { for g in group do sumBy g.Losses }
        select (group.Key, earnings, losses) } |> Seq.toArray
+2

Source: https://habr.com/ru/post/1531405/


All Articles