How to use multiple SUM in Eloquent laravel if condition like this - laravel-5.3

How to make the format query on laravel if I have a sql query like this?
SELECT category,
SUM(IF(group='core',weight,0))/SUM(IF(group='core',1,0)),
SUM(IF(group='secondary',weight,0))/SUM(IF(group='secondary',1,0))
FROM view
JOIN sub_category USING (id_sub_category)
JOIN category USING (id_category)
JOIN gap ON difference = (result-result_sub_category)
Thanks before

You have to use DB::raw for the sum, since Laravel doesn't have built-in function for this. Using isn't provided either. So use your model like so:
View::join('sub_category', 'sub_category.id_sub_category', '=', 'view.id_sub_category')
->get([DB::raw("SUM(IF(group='core',weight,0))/SUM(IF(group='core',1,0))")]);

Related

Use "where in" for LiteDB

I enjoy working with LiteDB in Xamarin.Forms. What is the best practice for getting list of objects from a table using list of ids or indexes in condition?
Currently, it looks for me like this:
_db.GetCollection<T>().FindAll().Where(q => listValues.Contains(Convert.ToInt32(q.GetProperty(idColumnName))));
listValues - list of searched ids.
idColumnName - column with indexes.
But FindAll retrives all records from LiteDB. Is there any more efficint options without full scanning?
Use the query in the Find method instead which will only retrieve entries based on your query rather than the full data set. This, according to the LiteDB's guidance, is more efficient than Linq queries on the full data returned: https://github.com/mbdavid/LiteDB/wiki/Queries
So your query would look like as follows
_d.GetCollection<T>().Find(q => listValues.Contains(Convert.ToInt32(q.GetProperty(idColumnName))))
I think you can just use collection.Query().Where("Column", 1,2,3) for this

Doctrine 1.2: how to get the count of related table rows ina a query?

I have a table of users, plus a related table called UserWorkplaces, where one user could have any number of related rows on the UserWorkplaces table.
I'm trying to build a query to find all of the users with only one related row in UserWorkplaces. What kind of query should I use to get this?
Here's what I've tried so far, but it gave me the count of everyone's workplaces and only a single user:
$query = Doctrine_Query::create()
->from('Users', 'u')
->innerJoin('u.userWorkplaces uw')
->having('COUNT(uw.id) = 1');
I believe your problem is not Doctrine but the SQL you wanna achieve.
Make the story short, following code should work you.
$query = Doctrine_Query::create()
->from('Users u')
->innerJoin('u.userWorkplaces uw')
->having('COUNT(uw.id) = ?', 1)
->groupBy('u.id');
COUNT() is aggregate function and therefore you need to specify the GROUP BY parameter for the main table you want to list in.

Order by Sum Field

I try to write a SQL-Statement in X++. It should do something like this:
SELECT
Table.field1, SUM(Table.field2) AS SumField
FROM
Table
WHERE
Table.FieldX = Something
GROUP BY
Table.Field1
ORDER BY
SumField;
The problem i have in X++ is that it orders the records before calculating the sum of them. I know that i could make a While Select in X++ and order them by code, but that is not the way i want to do it.
Can someone tell me how i could handle this?
Sorry, but you cannot both do a sort by and group by in a X++ select or query.
The solution is to make a view (without the sort), then do a select on the view with the order by.

Complex datasource issue in Dynamics AX

I have a grid that displays lines from a table. Now, I have these two requirements:
Show only the lines that contains values in the "hour" fields. The "hour" field is an array type.
Show the lines from a project and its subproject
My problem is this: to meet requirement #1, I need to use a select statement in my datasource since I cannot access array value using QueryBuildDataSource (this is a known limitation in Dynamics AX).
But to meet requirements #2 I need to have two "exists join", and this is not possible in Dynamics AX. For example the following code would not work:
select from table where
exists join tableChild where projectId = MyProjectId OR
exists join tableChild where parentProjectId = MyProjectId
How would someone address this issue?
From the select statement example you provided, it appears it may not be necessary to have two exist joins. A single exists join with the "OR" for the two possible conditions in its where clauses may be sufficient, of course, you should still have some relationship between table and tableChild for the join to make logical sense.
select from table
exists join tableChild
where (tableChild.projectId = MyProjectId || tableChild.parentProjectId = MyProjectId)
You can use array values in query ranges providing field IDs using fieldID2Ext function.
You can build view and apply confitions on top of it.

MDX Except function in where clause

I'm having problem restricting a query in mdx, using except function at where clause. i need to retrieved a set of data but which not in an specific set. Then i created the next query:
select {[Measures].[Amount], [Measures].[Transaction Cost], [Measures].[Transaction Number]} ON COLUMNS,{[ManualProcessing].[All ManualProcessings].[MAGNETICSTRIPE], ManualProcessing].[All ManualProcessings].[MANUAL]} ON ROWS
FROM [Transactions]
where except([Product].[All Products].Children,{[Product].[All Products].[Debit})
apparently this works fine, but when I try to add another restriction to slicer, I got this error: No function matches signature (Set,Member).
I'm currently working on mondrian 3.1
Is it possible to add multiple restriction to slicer when im sing the except function ?
are there any other way to get this ?
The Except function only works with sets.
But you can use n dimensions on your where:
select {[Measures].[Amount], [Measures].[Transaction Cost], [Measures].[Transaction Number]} ON COLUMNS,{[ManualProcessing].[All ManualProcessings].[MAGNETICSTRIPE], ManualProcessing].[All ManualProcessings].[MANUAL]} ON ROWS
FROM [Transactions]
where
(
except([Product].[All Products].Children,{[Product].[All Products].[Debit}),
except([Set],[Set to exclude])
)
This works in Analysis Services 2005, it should work in Mondrian
Apparently this can't be done on Mondrian, because there is a bug that doesn't allow compound slicers. I found a nice solutions using Mondrian OLAP Server. It's creating a member which excludes the set that I didn't want. It looks like this.
WITH member [Product].[Except] as ([Product].[All Products]) -
([Product].[All Products].[Debit])
SELECT {[Measures].[Amount],[Measures].[Transaction Cost], [Measures].[Transaction Number]} ON COLUMNS,
{[ManualProcessing].[All ManualProcessings].[MAGNETICSTRIPE],[ManualProcessing].[All ManualProcessings].[MANUAL]} ON ROWS
FROM [Transactions]
WHERE[Product].[Except]

Resources