Coder Social home page Coder Social logo

Comments (5)

cleitonmarx avatar cleitonmarx commented on May 28, 2024 3

Hey, @penhauer-xiao and @107295472! My apologies for the delay. Here's a possible code example for your problem:

        type TransactionRecord struct {
		ID        uint64
		AccountID string
		TCode     string
		TDate     string
		TType     uint8
		Amount    float64
	}

	type GroupedRecord struct {
		AccountID string
		TCode     string
		TDate     string
		TType     uint8
		SumAmount float64
	}

	type GroupKey struct {
		AccountID string
		TCode     string
		TDate     string
		TType     uint8
	}

	tds := []TransactionRecord{
		{ID: 1, AccountID: "A001", TCode: "161700", TDate: "2020-08-01", TType: 17, Amount: 23.0},
		{ID: 2, AccountID: "A001", TCode: "161700", TDate: "2020-08-01", TType: 17, Amount: 99.0},
		{ID: 3, AccountID: "A001", TCode: "161700", TDate: "2020-08-01", TType: 17, Amount: 25.0},

		{ID: 1, AccountID: "A001", TCode: "161700", TDate: "2020-08-01", TType: 24, Amount: 13.0},
		{ID: 2, AccountID: "A001", TCode: "161700", TDate: "2020-08-01", TType: 24, Amount: 69.0},
		{ID: 3, AccountID: "A001", TCode: "161700", TDate: "2020-08-01", TType: 24, Amount: 85.0},

		{ID: 4, AccountID: "A001", TCode: "161700", TDate: "2020-08-02", TType: 17, Amount: 11.0},
		{ID: 5, AccountID: "A001", TCode: "161700", TDate: "2020-08-02", TType: 17, Amount: 33.0},
		{ID: 6, AccountID: "A001", TCode: "161700", TDate: "2020-08-02", TType: 17, Amount: 57.0},
	}

	var results []GroupedRecord
	linq.From(tds).
		WhereT(func(r TransactionRecord) bool {
			return r.TType == 17 || r.TType == 24
		}).
		GroupByT(
			func(r TransactionRecord) GroupKey {
				return GroupKey{r.AccountID, r.TCode, r.TDate, r.TType}
			},
			func(r TransactionRecord) TransactionRecord { return r },
		).
		SelectT(func(g linq.Group) GroupedRecord {
			return GroupedRecord{
				AccountID: g.Key.(GroupKey).AccountID,
				TCode:     g.Key.(GroupKey).TCode,
				TDate:     g.Key.(GroupKey).TDate,
				TType:     g.Key.(GroupKey).TType,
				SumAmount: linq.From(g.Group).SelectT(func(r TransactionRecord) float64 { return r.Amount }).SumFloats(),
			}
		}).
		OrderByT(func(g GroupedRecord) string { return g.AccountID }).
		ThenByT(func(g GroupedRecord) string { return g.TCode }).
		ThenByT(func(g GroupedRecord) string { return g.TDate }).
		ThenByT(func(g GroupedRecord) uint8 { return g.TType }).
		ToSlice(&results)

from go-linq.

107295472 avatar 107295472 commented on May 28, 2024

I also want to use it like this, sum is in group by

from go-linq.

107295472 avatar 107295472 commented on May 28, 2024

Thanks, really strong

from go-linq.

wpecker avatar wpecker commented on May 28, 2024

thanks @cleitonmarx

from go-linq.

gatspy avatar gatspy commented on May 28, 2024

fantastic, thanks @cleitonmarx

from go-linq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.