Grouping and projections help

Hey everyone, I'm trying to execute this query:
g.V('0x77696bb39917c91a0c3908d577d5e322095425ca')
.outE()
.or(
__.hasLabel('from'),
__.hasLabel('to')
).inV()
.has('blockNumber', inside(0, 10000000000))
.project('label', 'transactionHash', 'value', 'dollarValue', 'blockNumber', 'from', 'to', 'tokenAddress')
.by(label)
.by(id)
.by(values('value'))
.by(values('dollarValue'))
.by(values('blockNumber'))
.by(coalesce(inE('from').outV().values('id'), constant('')))
.by(coalesce(inE('to').outV().values('id'), constant('')))
.by(coalesce(values('tokenAddress'), constant('')))
g.V('0x77696bb39917c91a0c3908d577d5e322095425ca')
.outE()
.or(
__.hasLabel('from'),
__.hasLabel('to')
).inV()
.has('blockNumber', inside(0, 10000000000))
.project('label', 'transactionHash', 'value', 'dollarValue', 'blockNumber', 'from', 'to', 'tokenAddress')
.by(label)
.by(id)
.by(values('value'))
.by(values('dollarValue'))
.by(values('blockNumber'))
.by(coalesce(inE('from').outV().values('id'), constant('')))
.by(coalesce(inE('to').outV().values('id'), constant('')))
.by(coalesce(values('tokenAddress'), constant('')))
Solution:
You can just add a group().by('from') to the end of what you have. as an example with the modern graph: ```gremlin> g.V().project('name','degree').by('name').by(bothE().count()) ==>[name:marko,degree:3] ==>[name:vadas,degree:1] ==>[name:lop,degree:3]...
Jump to solution
3 Replies
PedroR
PedroROP2y ago
And the result I'm getting is this:
[
{
"label": "Transaction",
"transactionHash": "0xd851a5f400b696b3b80f04e7f33f0d98268a1b5a69b5c1f0392c5512109c966c",
"value": 77225790000000000,
"dollarValue": 119.96660686595044,
"blockNumber": 16444832,
"from": "0x77696bb39917c91a0c3908d577d5e322095425ca",
"to": "0x54822fd21897b9264440cd1a8f91458971d59ce7",
"tokenAddress": ""
},
{
"label": "Transaction",
"transactionHash": "0x6e847bc5aed2c5943c819b5145862d4ee5dbdab31dd092dc841d8f0e64295be5",
"value": 1198834000000000,
"dollarValue": 1.8623318346828803,
"blockNumber": 16444827,
"from": "0x77696bb39917c91a0c3908d577d5e322095425ca",
"to": "0x3d36c138d0d527013798716a861513251b7bf1ac",
"tokenAddress": ""
},
{
"label": "Transaction",
"transactionHash": "0xfbf463432326fea8f13ae370a123cfd3eb47c3efab2cc68515c1261075deccee",
"value": 30439240000000000,
"dollarValue": 47.28591754617613,
"blockNumber": 16444822,
"from": "0x77696bb39917c91a0c3908d577d5e322095425ca",
"to": "0xa285d2d22245f1dd8cb9c530be5944b8720b38fd",
"tokenAddress": ""
}]
[
{
"label": "Transaction",
"transactionHash": "0xd851a5f400b696b3b80f04e7f33f0d98268a1b5a69b5c1f0392c5512109c966c",
"value": 77225790000000000,
"dollarValue": 119.96660686595044,
"blockNumber": 16444832,
"from": "0x77696bb39917c91a0c3908d577d5e322095425ca",
"to": "0x54822fd21897b9264440cd1a8f91458971d59ce7",
"tokenAddress": ""
},
{
"label": "Transaction",
"transactionHash": "0x6e847bc5aed2c5943c819b5145862d4ee5dbdab31dd092dc841d8f0e64295be5",
"value": 1198834000000000,
"dollarValue": 1.8623318346828803,
"blockNumber": 16444827,
"from": "0x77696bb39917c91a0c3908d577d5e322095425ca",
"to": "0x3d36c138d0d527013798716a861513251b7bf1ac",
"tokenAddress": ""
},
{
"label": "Transaction",
"transactionHash": "0xfbf463432326fea8f13ae370a123cfd3eb47c3efab2cc68515c1261075deccee",
"value": 30439240000000000,
"dollarValue": 47.28591754617613,
"blockNumber": 16444822,
"from": "0x77696bb39917c91a0c3908d577d5e322095425ca",
"to": "0xa285d2d22245f1dd8cb9c530be5944b8720b38fd",
"tokenAddress": ""
}]
But I'd also like to group these results by 'from'. How should I go about grouping these? Thanks!
Solution
spmallette
spmallette2y ago
You can just add a group().by('from') to the end of what you have. as an example with the modern graph:
gremlin> g.V().project('name','degree').by('name').by(bothE().count())
==>[name:marko,degree:3]
==>[name:vadas,degree:1]
==>[name:lop,degree:3]
==>[name:josh,degree:3]
==>[name:ripple,degree:1]
==>[name:peter,degree:1]
gremlin> g.V().project('name','degree').by('name').by(bothE().count()).
......1> group().by('degree')
==>[1:[[name:vadas,degree:1],[name:ripple,degree:1],[name:peter,degree:1]],3:[[name:marko,degree:3],[name:lop,degree:3],[name:josh,degree:3]]]
gremlin> g.V().project('name','degree').by('name').by(bothE().count())
==>[name:marko,degree:3]
==>[name:vadas,degree:1]
==>[name:lop,degree:3]
==>[name:josh,degree:3]
==>[name:ripple,degree:1]
==>[name:peter,degree:1]
gremlin> g.V().project('name','degree').by('name').by(bothE().count()).
......1> group().by('degree')
==>[1:[[name:vadas,degree:1],[name:ripple,degree:1],[name:peter,degree:1]],3:[[name:marko,degree:3],[name:lop,degree:3],[name:josh,degree:3]]]
the by() on group() will select a key from a Map to group on.
spmallette
spmallette2y ago
as an aside, you might consider simplifying your traversal a bit to:
g.V('0x77696bb39917c91a0c3908d577d5e322095425ca')
.out('from','to')
.has('blockNumber', inside(0, 10000000000))
.project('label', 'transactionHash', 'value', 'dollarValue', 'blockNumber', 'from', 'to', 'tokenAddress')
.by(label)
.by(id)
.by('value')
.by('dollarValue')
.by('blockNumber')
.by(coalesce(in('from').values('id'), constant('')))
.by(coalesce(in('to').values('id'), constant('')))
.by(coalesce(values('tokenAddress'), constant('')))
g.V('0x77696bb39917c91a0c3908d577d5e322095425ca')
.out('from','to')
.has('blockNumber', inside(0, 10000000000))
.project('label', 'transactionHash', 'value', 'dollarValue', 'blockNumber', 'from', 'to', 'tokenAddress')
.by(label)
.by(id)
.by('value')
.by('dollarValue')
.by('blockNumber')
.by(coalesce(in('from').values('id'), constant('')))
.by(coalesce(in('to').values('id'), constant('')))
.by(coalesce(values('tokenAddress'), constant('')))
Want results from more Discord servers?
Add your server