# SQL vs Ecto optimization/translation help

**URL:** <https://forum.elixirforum.com/t/sql-vs-ecto-optimization-translation-help/5559>\
**Category:** Questions\
**Tags:** ecto, sql\
**Created:** [May 29, 2017, 4:58am UTC](https://forum.elixirforum.com/t/sql-vs-ecto-optimization-translation-help/5559 "2017-05-29T04:58:41Z")\
**Posts on this page:** 1\
**Showing post:** 20

<div class="post-metadata">

**Author:** ![bglusman](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bglusman/32/9459_2.png) [@bglusman](https://forum.elixirforum.com/u/bglusman)\
**Post date:** [May 30, 2017, 10:38pm UTC](https://forum.elixirforum.com/t/sql-vs-ecto-optimization-translation-help/5559/20 "2017-05-30T22:38:03Z")

</div>

Parting thought, as I start to try this simplifying… I suspect I can make the query work, but I also beleive the shape of the data will be exactly the opposite what I want, and it may make for some very ugly code manually constructing structs or something to rearrange data how I want… I hoped the group\_by clause in Ecto would flip things around to hand me back the CreditType structs I want, with the stocks and foods for them loaded up, but I’m pretty sure that was a delusion on my part because I didn’t understand group by correctly or something… is there anyway, given a bunch of sql data, to control how ecto instantiates the structs out of it? I start this query with from stocks in Stock, and so I’m pretty confident what I get out will be a list of Stock structs, but thats not what I want.. I want a list of CreditType, containing the stocks as a relation… I sort of lost focus on that while thinking about how to reproduce this SQL query as Ecto, but without that, I think I have to, in memory in elixir, do all kinds of weird acrobatics with the stocks that come back, shoving them into a map or struct based on which credit type’s they have present… which I can do, but just confirmed there’s not a better way.

---

_[View the full topic](https://forum.elixirforum.com/t/sql-vs-ecto-optimization-translation-help/5559)._
