Query Builder array_agg Laravel - postgresql

I have a question about Laravel Eloquent ORM, can i use this tool to create Postgresql array_agg?, I have this query:
this query returns me:
[{"id":1, "contact_name":"name", "description":"description", "discharge":0, "latitude":null, "longitude":null, "town":null, "country":null, "province":null,
"numbers":"{77777,888888}", "emails":"{email#email.com, email2#email2.com}"}]
And im trying to create this query with eloquent: i have this code:
This second query returns me all the previous data except the numbers and emails, because i dont have any idea how to put array_agg in this query.
I appreciate any help!

Related

How do I pass results from one Custom Query to another?

I am using Tableau to create custom queries, I need to pass the results from one custom query into another. So for example one custom query might have the following query:
select *
from bime.test_tab
where record_date = '2018-05-21'.
I then create another custom query which uses the results from the query above. Is this possible?
Sure you can by making this custom query as a subquery
select * from (select *
from bime.test_tab
where record_date = '2018-05-21') a
As you might already know that we do not have a leverage to use temp tables in Tableau custom queries, hence we usually create subqueries for that.
Considering your case,
SELECT BIM_TABLE.* FROM
(SELECT * FROM
BIME.TEST_TAB WHERE RECORD_DATE= '2018-05-21')BIM_TABLE
You can post your actual problem with the relevant columns if you need further help.
PS - It's advised to do most of your data manipulations in SQL itself (if you are running your workbook on live connection), as Tableau is not able to properly optimize part of the 'Custom SQL' query.

On laravel using builder query whereRaw ?&

I have problem using query whereraw on laravel 5.5 postgresql, for this case i want to select data by colors. Data example
Source postgresql documentation postgres. I'm success to try on execute sql like this success example execute query. But fail using laravel example source code. Error on laravel
The problem is that your statement contains a question mark. When using the whereRaw method, the question mark is an expected parameter, which aren't providing in your call.
However it seems that there isn't a real solution for this issue. I suggest you take a look at Question mark operator in query, it handles about a similar issue.

How to execute a raw sql query from LINQ to Entities 4.5?

Problem
I need to execute a raw SQL query from LINQ to Entities and retrieve the result. The query returns the current date/time from the SQL Server instance, and looks like this:
SELECT GETDATE()
[Edit]
I'm using a data model that was created database-first.
[/Edit]
What I've Tried
I've researched this issue on the interwebz and been unable to find a technique to do this. I was able to learn how to do this using LINQ to SQL, but since I'm not using that, it's of no help.
Heres what you are after
var time = context.Database.SqlQuery<DateTime>("SELECT GETDATE()").FirstOrDefault();
You can read more about raw sql and EF here http://msdn.microsoft.com/en-us/data/jj592907.aspx

Converting SQL query

I have an SQL query which is fetching me the data correcting from sqlplus. When I try to convert the query such that I can implement in the catalyst controller, I am getting an error message saying there is no relation between the tables Feature and Featurealias.As I am new to catalyst and DBIx::Class, I am not able to figure out what could be the probable mistake. Can any body help me fix this issue.
Below is the SQL query and the code which I a trying to implement in catalyst Controller.
my#sourceid=$c->model('Gene::Featurealias')->search({
'feature.typeid'=>4,
},
{
join=>'feature',
distinct=>1,
columns=>[qw/sourceid/]
});
SQL query:
select distinct FeatureAlias.SourceID From FeatureAlias join Feature on FeatureAlias.FeatureID=Feature.FeatureID where Feature.TypeID=4;
Defined in Feature.pm
__PACKAGE__->has_many( "featurealias",
"...Result::Featurealias");
Defined in Featurealias.pm
__PACKAGE__->belongs_to("featureid",
"...::Result::Feature");
Thanks in advance.
You've created a relationship named 'featureid' in your Featurealias class but asked DBIC to join one named 'feature'.

How to use "DISTINCT ON (field)" in Doctrine 2?

I know how to use "DISTINCT" in Doctrine 2, but I really need to use "DISTINCT ON (field)" and I don't know how to do this with the QueryBuilder.
My SQL query looks like:
SELECT DISTINCT ON (currency) currency, amount FROM payments ORDER BY currency
And this query works perfect, but I can't use it with the QueryBuilder. Maybe I could write this query on some other way?
I would suggest that the SELECT DISTINCT ON (..) construct that PostgreSQL supports is outside the Object Relational Model (ORM) that is central to Doctrine. Or, perhaps put another way, because SELECT DISTINCT ON (..) is rare in SQL implementations Doctrine haven't coded for it.
Regardless of the actual logic for it not working, I would suggest you try Doctrine's "Native SQL". You need to map the results of your query to the ORM.
With NativeQuery you can execute native SELECT SQL statements and map
the results to Doctrine entities or any other result format supported
by Doctrine.
In order to make this mapping possible, you need to describe to
Doctrine what columns in the result map to which entity property. This
description is represented by a ResultSetMapping object.
With this feature you can map arbitrary SQL code to objects, such as
highly vendor-optimized SQL or stored-procedures.
SELECT DISTINCT ON (..) falls into vendor-optimized SQL I think, so using NativeQuery should allow you to access it.
Doctrine QueryBuilder has some limitations. Even if I didn't check if it's was possible with query builder, I do not hesitate to use DQL when I do not know how to write the query with query builder.
Check theses examples at
http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html#dql-select-examples
Hope this help.
INDEX BY can be used in DQL, allowing first result rows indexed by the defined string/int field to be overwritten by following ones with the same index:
SELECT
p.currency,
p.amount
FROM Namespace\To\Payments p INDEX BY p.currency
ORDER BY p.currency ASC
DQL - EBNF - INDEX BY