I am trying to work out how to convert the order by code bellow that I am currently using in db_query to db_select.
The query sorts users by how well the match the current logged in user. I am also using some WHERE statements to remove users that are totally incompatible before running the rest through the order by calculation to speed up the query, so I don't want to run it as a select expression.
ORDER BY (10*(s.field_smoker_value * :field_smoker_pref_value) +
10*(sp.field_smoker_pref_value * :field_smoker_value) +
15*(p.field_pet_value * :field_pet_pref_value) +
15*(pp.field_pet_pref_value * :field_pet_value) +
5*(c.field_couple_value * :field_couple_pref_value) +
5*(cp.field_couple_pref_value * :field_couple_value))ASC
Related
I am taking the Google Data Analytics course on Coursera and in the video the instructor executed the following query:
SELECT
Date,
Region,
Small_Bags,
Large_Bags,
XLarge_Bags,
Total_Bags,
Small_Bags + Large_Bags + XLarge_Bags AS Total_Bags_Calc
FROM
`class-5-355317.avocado_data.avocado_prices`
After executing this query they then opened a different editor window on BigQuery and executed the following query referring to an alias in the query above without defineing it:
SELECT
*
FROM
`class-5-355317.avocado_data.avocado_prices`
WHERE
Total_Bags != Total_Bags_Calc
When I executed this query it did not work for me and I received this error: 'Unrecognized name: Total_Bags_Calc; Did you mean Total_Bags?'
This makes sense. Within this query, the alias 'Total_Bags_Calc' hadn't been used within that query and didn't have anything to pull, so I tried a workaround:
SELECT
Date,
Region,
Small_Bags,
Large_Bags,
XLarge_Bags,
Total_Bags,
(SELECT Small_Bags + Large_Bags + XLarge_Bags FROM `class-5355317.avocado_data.avocado_prices`) AS Total_Bags_Calc
FROM `class-5-355317.avocado_data.avocado_prices`
WHERE
Total_Bags != Total_Bags_Calc
From what I understood this should work since the subquery now held the alias 'Total_Bags_Calc' but I still received the error Unrecognized name: Total_Bags_Calc; Did you mean Total_Bags?
How can I make this query work, and is there any way to have a query reference another query in the same manner that theirs did in the example?
You'll want to select FROM the result of your first query, so try moving that subquery into the FROM clause.
For example,
SELECT
*
FROM
{{ your other query goes here }}
WHERE
Total_Bags != Total_Bags_Calc
Which would be:
SELECT
*
FROM
(SELECT
Date,
Region,
Small_Bags,
Large_Bags,
XLarge_Bags,
Total_Bags,
Small_Bags + Large_Bags + XLarge_Bags AS Total_Bags_Calc
FROM
`class-5-355317.avocado_data.avocado_prices`
) as subquery
WHERE
Total_Bags != Total_Bags_Calc
This is a really helpful technique to learn, so definitely learn it. However since you're doing something rather simple, you can actually get away with just coding that logic in your WHERE clause.
SELECT
Date,
Region,
Small_Bags,
Large_Bags,
XLarge_Bags,
Total_Bags,
Small_Bags + Large_Bags + XLarge_Bags AS Total_Bags_Calc
FROM
`class-5-355317.avocado_data.avocado_prices`
WHERE (Small_Bags + Large_Bags + XLarge_Bags) <> Total_Bags
Good Morning Tituslcuster!
I think I have spotted the issue.
Your query is creating an column named Total_Bags_Calc
When your code reaches the Where statement stuff, Total_Bags_Calc is the part that is breaking your code.
This is because it doesn't exist in the From table, but it does exist as a temporary name.
You can do two different things to fix this..
You can subquery this whole query, and do the Where Total_Bags_Calc on the outside query.
Or you can replace the Total_Bags_Calc with the actual formula that you used to calculate Total_Bags_Calc.
Here try this one:
select x2.* from (
SELECT
Date,
Region,
Small_Bags,
Large_Bags,
XLarge_Bags,
Total_Bags,
( Small_Bags + Large_Bags + XLarge_Bags ) AS Total_Bags_Calc
FROM 'class-5-355317.avocado_data.avocado_prices'
) as x2
where x2.Total_Bags != x2.Total_Bags_Calc
I have the following query that I use in Azure data factory(this is on the source of a copy action):
SELECT
{ [Measures].[0INV_QTY],
[Measures].[0NET_VAL_S] }
ON COLUMNS,
NON EMPTY
{ [0CUST_SALES].[LEVEL01].MEMBERS *
[0SALESORG].[LEVEL01].MEMBERS *
[0COMPANY].[LEVEL01].MEMBERS *
[0MATERIAL].[LEVEL01].MEMBERS *
[ZDEBITOR].[LEVEL01].MEMBERS *
[0FISCPER].[LEVEL01].MEMBERS *
[0DEB_CRED].[LEVEL01].MEMBERS *
[0BILLTOPRTY].[LEVEL01].MEMBERS *
[0DOC_CATEG].[LEVEL01].MEMBERS *
[0SHIP_TO].[LEVEL01].MEMBERS }
DIMENSION PROPERTIES
[0SALESORG].[20SALESORG],
[0COMPANY].[20COMPANY],
[0CUST_SALES].[80CUST_SALES],
[0CUST_SALES].[20CUST_GRP1],
[0CUST_SALES].[20PMNTTRMS],
[ZDEBITOR].[20CRED_LIMIT],
[0MATERIAL].[20MATERIAL],
[0DEB_CRED].[20DEB_CRED],
[0BILLTOPRTY].[20BILLTOPRTY],
[0DOC_CATEG].[20DOC_CATEG],
[0SHIP_TO].[20SHIP_TO],
[0FISCPER].[80FISCPER]
ON ROWS
FROM $0SD_C03
WHERE ({[0CALYEAR].[2020], [0CALYEAR].[2021], [0CALYEAR].[2018], [0CALYEAR].[2019]})
In here I would like to replace the WHERE with something like Cast(YEAR(GETDATE())-4 as varchar(10)) Now I am really new to MDX and I keep getting stuck. Could anyone point me in the right direction?
So what i want to achieve is not having to adjust the query every year and be able to only have the last 4 years.
If you are looking for a SQL equivalent as below in MDX
SELECT ... From ... WHERE date > DATEADD(year,-4,GETDATE())
Try using "with member" and function parallelperiod.
CREATE MEMBER CurrentCube.Measures.[Last4Years] AS
ParallelPeriod( [Date].[Date].[Date Yr], 4, StrToMember(“[Date].[Date].&[” + Format(now(), “yyyyMMdd”) + “]”))
: StrToMember(“[Date].[Date].&[” + Format(now(), “yyyyMMdd”) + “]”)
;
I want to build something with an aliases column like below, but I don't know how to make it a Laravel query. The following is my SQL.
SELECT
d.*,
d.damaged_building,
d.total_victim
FROM
(
SELECT
delete_flg,
damage_id,
create_time,
reg_user_name,
municipality_id,
report_time,
district,
village,
disaster_type,
cause_of_disaster,
(
bd_major_damage1 + bd_minor_damage1 + bd_major_damage2 + bd_minor_damage2 + bd_major_damage3 + bd_minor_damage3
)
as damaged_building,
(
hi_hd_deaths + hi_hd_serious_injuries + hi_hd_minor_injuries + hi_hd_missing_persons + hi_hd_sick_persons
)
as total_victim
FROM
d_damage
WHERE
delete_flg = 0
ORDER BY
create_time DESC
)
d
I want to translate this to Eloquent of Laravel Query Builder for use in my controller.
You may try with this
DB::table('d_damage')
->where('delete_flg',0)
->orderBy('create_time', 'desc')
->select('delete_flg', 'damage_id', 'create_time', 'reg_user_name', 'municipality_id', 'report_time', 'district', 'village', 'disaster_type', 'cause_of_disaster',DB::raw('(bd_major_damage1 + bd_minor_damage1 + bd_major_damage2 + bd_minor_damage2 + bd_major_damage3 + bd_minor_damage3) as damaged_building'),DB::raw('(hi_hd_deaths + hi_hd_serious_injuries + hi_hd_minor_injuries + hi_hd_missing_persons + hi_hd_sick_persons) as total_victim'))
->get();
this may be not exactly but almost
If I were you just stick to learning the SQL Queries since you bring it anywhere you can learn alot of frameworks / tools, rather than investing your time in Laravel's. Anyway, you still can use sql queries in laravel using the raw expressions : https://laravel.com/docs/5.8/queries#raw-expressions
I have the following SQL query
SELECT
pmma_vt_feldmarkposition.vtvt_recn AS [li_verträge_recn],
pmma_vt_feldmarkposition.feldmark AS [vt_feldmark_nr_pflanze],
switch (gbs is null, gbs_opti, gbs is not null, gbs) AS [ta_istbeitragssatz_hagel],
NZ(pmma_vt_feldmarkposition.ebs_sturm, 0) + NZ(pmma_vt_feldmarkposition.ebs_frost, 0) + NZ(pmma_vt_feldmarkposition.ebs_wolkenbruch, 0) + NZ(pmma_vt_feldmarkposition.ebs_hochwasser, 0) + NZ(pmma_vt_feldmarkposition.ebs_trockenheit, 0) + NZ(pmma_vt_feldmarkposition.ebs_pauschal, 0) AS [ta_istbeitragssatz_elementar],
switch (gbs is null, gbs_opti, gbs is not null, soll_gbs) AS [ta_sollbeitragssatz_hagel]
INTO
vrt_feldmarkpositionen
FROM
PMMA_VT_FELDMARKPOSITION
WHERE
pmma_vt_feldmarkposition.lfd_nr * 1000000000 + pmma_vt_feldmarkposition.vtvt_recn
IN (
SELECT MIN(pmma_vt_feldmarkposition.lfd_nr * 1000000000 + pmma_vt_feldmarkposition.vtvt_recn) AS minhelper
FROM pmma_vt_feldmarkposition
GROUP BY pmma_vt_feldmarkposition.vtvt_recn
);
pmma_vt_feldmarkposition used to be linked to my ODBC database and the query was running without problems. Now, I converted the link to a local table (also named pmma_vt_feldmarkposition, removed the link to the database) to be able to work offline - but the query keeps running forever without finishing. I do not receive any kind of error message.
What could be the reason for this? Could it be because my .accdb file is 1,8GB large now? (I saved a few more local tables and already used the "compact and repair" function)
Actually, I think the best solution will be to get rid of the calculated expression, and work with both fields in a JOIN instead.
Simplifying the first part of the query, this would be:
SELECT
fields
FROM
PMMA_VT_FELDMARKPOSITION T
INNER JOIN (
SELECT MIN(lfd_nr) AS MinLfdNr, vtvt_recn
FROM pmma_vt_feldmarkposition
GROUP BY vtvt_recn
) AS MinGrp
ON T.lfd_nr = MinGrp.MinLfdNr
AND T.vtvt_recn = MinGrp.vtvt_recn
At least I think this should return the same result as your query.
If necessary, performance can be further improved by storing the subquery result in a temp table.
Make sure both lfd_nr and vtvt_recn are indexed.
When using an order by, conditional projection and SetFirstResult() together in a query I get this error:
Invalid index 4 for this SqlParameterCollection with Count=4
In my experience, this error usually happens when you have one field mapped to two properties, as outlined here. But it is not the case in this situation, the query works fine until, I pass a value greater than 0 to SetFirstResult().
This bug logged with Nhiberbate seems very simular, but it was fixed 2 years ago.
Any suggestions on how to proceed? Here is a sample of the code:
var query = Session.CreateCriteria<KeepItem>(KeepAlias)
.CreateAlias("Resource", ResourceAlias)
.CreateAlias("Memory", MemoryAlias, JoinType.LeftOuterJoin);
// other code
query.AddOrder
(
Order.Asc
(
Projections.Conditional
(
Restrictions.IsNull(MemoryAlias + ".MinDate"),
Projections.Conditional
(
Restrictions.IsNull(ResourceAlias + ".MinDate"),
Projections.Constant(DateTime.MaxValue),
Projections.Property(ResourceAlias + ".MinDate")
),
Projections.Property(MemoryAlias + ".MinDate")
)
)
);
//other code
query.SetFirstResult(skip);
query.SetMaxResults(take);
return query.List<KeepItem>();