Convert to Laravel Query - laravel

I have query for query data. That is document.
"SELECT
zk_z_hako * CASE WHEN zk_n_iri> 0 THEN zk_n_iri ELSE 1 END
+ zk_z_bara
- ifnull(
sum(
ns_hako
* CASE WHEN zk_n_iri> 0 THEN zk_n_iri ELSE 1 END
* CASE WHEN ns_tr_kbn in (0,6) OR ( ns_tr_kbn = 1 AND ns_ns_kbn = 7) THEN 1
WHEN ns_tr_kbn in (1,7) THEN (-1)
ELSE 0
END
+ ns_bara
* CASE WHEN ns_tr_kbn in (0,6) OR ( ns_tr_kbn = 1 AND ns_ns_kbn = 7) THEN 1
WHEN ns_tr_kbn in (1,7) THEN (-1)
ELSE 0
END )
,0 ) AS TOTAL_BARA
FROM t_table1
LEFT JOIN t_table2
ON ns_kno = zk_kno
AND ns_show_flg = 0
AND ns_ymd > 'Date param'
WHERE zk_kno = Value param;
So I am not a master of Laravel. Now I need to convert this query for work with laravel. Anyone can help me?
And i have to try this query.
$squery = 'zk_z_hako * CASE WHEN zk_n_iri> 0 THEN zk_n_iri ELSE 1 END
+ zk_z_bara
ifnull(
sum(
ns_hako
* CASE WHEN zk_n_iri> 0 THEN zk_n_iri ELSE 1 END
* CASE WHEN ns_tr_kbn in (0,6) OR ( ns_tr_kbn = 1 AND ns_ns_kbn = 7) THEN 1
WHEN ns_tr_kbn in (1,7) THEN (-1)
ELSE 0
END
+ ns_bara
* CASE WHEN ns_tr_kbn in (0,6) OR ( ns_tr_kbn = 1 AND ns_ns_kbn = 7) THEN 1
WHEN ns_tr_kbn in (1,7) THEN (-1)
ELSE 0
END )
,0 ) AS TOTAL_BARA ';
$param1= '20160310';
$param2= '1972640100';
$results = DB::table('table1')
->select($squery)
->leftJoin('table2', function($join) use ($param1)
{
$join->on('table1.ns_kno', '=', 'table2.zk_kno');
$join->on('table1.ns_show_flg', '=', DB::raw(0));
$join->on('ns_ymd','>',DB::raw("'".$param1."'"));
})
->where('zk_kno', DB::raw($param2))
->toSql()
But it's return sql
"select `zk_z_hako` as `CASE` from `t_zaikmst` left join `t_nsyutrn` on `t_nsyutrn`.`ns_kno` = `t_zaikmst`.`zk_kno` and `t_nsyutrn`.`ns_show_flg` = 0 and `ns_ymd` > '20160310' where `zk_kno` = 1972640100"
i don't sure it true.

If you want to make custom select than you need to use raw queries as select parameter like this:
->select(\DB::raw($squery))

Related

Not a singlenot a single-group group function ORA-00937: Oracle

What can I do at the highest level to change this error
ORA-00937: not a single-group group function
00937. 00000 - "not a single-group group function"
*Cause:
*Action:
Error at Line: 3 Column: 5
select
year,
Net_TWRR_PERIOD,
round(((CASE WHEN MOD(SUM(CASE WHEN
( Net_TWRR_PERIOD ) <0 then 1 else 0 end ), 2 )=1 THEN -1 ELSE 1 END * EXP(SUM(LN(ABS(Net_TWRR_PERIOD)))))-1)*100,2)
from (select
year,
round(((CASE WHEN MOD(SUM(CASE WHEN (Net_TWRR ) <0 then 1 else 0 end ), 2 )=1 THEN -1 ELSE 1 END * EXP(SUM(LN(ABS(Net_TWRR)))))-1)*100,2) as Net_TWRR_PERIOD
from
(select ( net_rate_of_return / 100 + 1) as Net_TWRR,
year
from eom
WHERE id = '2'
and start_date < '09-September-2022'
) group by year order by year)
you are using the SUM functiion and the GROUP BY is missing in the outermost SQL.
create table eom(year number(4), start_date date, net_rate_of_return number (10,4), id number(4))
SELECT year,
Net_TWRR_PERIOD,
ROUND (
( ( CASE
WHEN MOD (
SUM (
CASE
WHEN (Net_TWRR_PERIOD) < 0 THEN 1
ELSE 0
END),
2) = 1
THEN
-1
ELSE
1
END
* EXP (SUM (LN (ABS (Net_TWRR_PERIOD)))))
- 1)
* 100,
2)
FROM ( SELECT year,
ROUND (
( ( CASE
WHEN MOD (
SUM (
CASE
WHEN (Net_TWRR) < 0 THEN 1
ELSE 0
END),
2) = 1
THEN
-1
ELSE
1
END
* EXP (SUM (LN (ABS (Net_TWRR)))))
- 1)
* 100,
2)
AS Net_TWRR_PERIOD
FROM (SELECT (net_rate_of_return / 100 + 1) AS Net_TWRR, year
FROM eom
WHERE id = '2' AND start_date < '09-September-2022')
GROUP BY year
ORDER BY year, Net_TWRR_PERIOD)
GROUP BY year, Net_TWRR_PERIOD
You have the outer query:
select year,
Net_TWRR_PERIOD,
round(
(
CASE
WHEN MOD(SUM(CASE WHEN Net_TWRR_PERIOD < 0 then 1 else 0 end ), 2)=1
THEN -1
ELSE 1
END
* EXP(SUM(LN(ABS(Net_TWRR_PERIOD))))
-1
) * 100,
2
)
from ( ... )
Which has a mix of aggregated columns and non-aggregated columns and you do not have a GROUP BY clause (in that outer query). You need to make sure all columns are either aggregated or contained in a GROUP BY.
So, change the outer query to:
select year,
Net_TWRR_PERIOD,
round(
(
CASE
WHEN MOD(SUM(CASE WHEN Net_TWRR_PERIOD < 0 then 1 else 0 end ), 2)=1
THEN -1
ELSE 1
END
* EXP(SUM(LN(ABS(Net_TWRR_PERIOD))))
-1
) * 100,
2
)
from ( ... )
GROUP BY year, Net_TWRR_PERIOD

Not able to access the INIT variable in a secondary FOR loop

I have the following code snippet:
TYPES: BEGIN OF ty_table_line,
idx TYPE i,
posnr TYPE i,
quan TYPE i,
END OF ty_table_line,
ty_internal_table TYPE SORTED TABLE OF ty_table_line WITH UNIQUE KEY idx.
DATA(lt_set) = VALUE ty_internal_table( ( idx = 1 posnr = 1 quan = 2 )
( idx = 2 posnr = 1 quan = 40 )
( idx = 3 posnr = 1 quan = 10 )
( idx = 4 posnr = 1 quan = 88 ) ).
DATA(lt_set_2) = VALUE ty_internal_table(
FOR i = 0 WHILE i <= 4
( LINES OF VALUE #( FOR <line> IN lt_set[]
WHERE ( idx = i + 1 )
( <line> ) ) ) ).
Here, my loop starts with i=0 and I am trying to use the value in i to filter out some values from an internal table LT_SET. What is the right approach for this error?
I tried with WHERE ( idx = i + 1 ) and WHERE ( idx <> i ), but I get this error:
The variable "I" cannot be used here.
I can't explain the reason (nothing found in the ABAP documentation) but I could reproduce and find a workaround with an additional auxiliary variable declared with LET. Tested with ABAP 7.52.
TYPES: BEGIN OF ty_table_line,
idx TYPE i,
posnr TYPE i,
quan TYPE i,
END OF ty_table_line,
ty_internal_table TYPE SORTED TABLE OF ty_table_line WITH UNIQUE KEY idx.
DATA(lt_set) = VALUE ty_internal_table( ( idx = 1 posnr = 1 quan = 2 )
( idx = 2 posnr = 1 quan = 40 )
( idx = 3 posnr = 1 quan = 10 )
( idx = 4 posnr = 1 quan = 88 ) ).
DATA(lt_set_2) = VALUE ty_internal_table(
FOR i = 0 WHILE i <= 4
LET j = i IN
( LINES OF VALUE #( FOR <line> IN lt_set[]
WHERE ( idx = j + 1 )
( <line> ) ) ) ).
ASSERT lt_set_2 = lt_set.

Calculate % based on a combination of count and stored values - Power BI

I have a basic table that looks like this:
DayNo. Customer AgentsInvolved CallID
0 AAA 1 1858
0 AAA 3 1859
2 AAA 1 1860
0 BBB 2 1862
0 CCC 1 1863
0 DDD 3 1864
9 DDD 1 1865
9 DDD 4 1866
I need to be able to find the % of customers who only contacted only once, and spoke to 1 agent only. So from the above example, out of 4 distinct customers only customer CCC falls into this category (1 call, 1 AgentInvolved)
So the Desired result would be: 1/4 or 25%
How can I create a Power BI measure to do this calc?
Try this measure:
Desired Result =
VAR summarizetable =
SUMMARIZECOLUMNS (
'table'[Customer],
"Calls", COUNT ( 'table'[CallID] ),
"Agents", SUM ( 'table'[AgentsInvolved] ),
"Day", SUM ( 'table'[DayNo.] )
)
RETURN
COUNTROWS (
FILTER ( summarizetable, [Calls] = 1 && [Agents] = 1 && [Day] = 0 )
)
/ COUNTROWS ( summarizetable )
The summarized table created on the fly in VAR summarizetable looks like this:
Here's another approach:
Measure =
SUMX(
VALUES(Table2[Customer]),
CALCULATE(
IF(
DISTINCTCOUNT(Table2[CallID]) = 1 &&
SUM(Table2[AgentsInvolved]) = 1,
1,
0
),
Table2[DayNo.] = 0
)
) /
DISTINCTCOUNT(Table2[Customer])
If you want to exclude the 0 day rows in the denominator as well, replace the last line with
CALCULATE(DISTINCTCOUNT(Table2[Customer]), Table2[DayNo.] = 0)
Desired Result =
VAR summarizetable =
SUMMARIZECOLUMNS (
'table'[AgentsInvolved],
'table'[DayNo.],
'table'[Customer],
"Calls", COUNT ( 'table'[CallID] )
)
)
RETURN
COUNTROWS (
FILTER ( summarizetable, [Calls] = 1 && 'table'[AgentsInvolved] = 1 && 'table'[DayNo.] = 0 )
) / DISTINCTCOUNT ( 'table'[Customer])

Missing some parentehtesis

Working on the case statement below and keep getting a missing parenthesis error. any suggestions?
( CASE
WHEN XBAND = 4 AND TBAND = 0 AND YBAND >= 2
THEN 'A'
END
ELSE
CASE
WHEN XBAND = 4 AND TBAND = 0 AND YBAND >= 3
THEN 'B'
END
END ) XYT_BAND
There should only be one END per CASE expression. This should work:
CASE
WHEN XBAND = 4 AND TBAND = 0 AND YBAND >= 2
THEN 'A'
WHEN XBAND = 4 AND TBAND = 0 AND YBAND >= 3
THEN 'B'
END XYT_BAND
If you need to nest CASE expressions then:
CASE
WHEN XBAND = 4 AND TBAND = 0 AND YBAND >= 2
THEN 'A'
ELSE
CASE
WHEN XBAND = 4 AND TBAND = 0 AND YBAND >= 3
THEN 'B'
END
END XYT_BAND
In order to generate multiple rows for each entry you could use union or more flexible unpivot transposing columns to rows:
demo
select id, xyt_band
from (select id,
case when xband = 4 and yband = 0 and tband >= 2 then 'A' end c1,
case when xband = 4 and yband = 0 and tband >= 3 then 'B' end c2
from t)
unpivot (xyt_band for cx in (C1, C2))

All these projectiles going in 1 direction

Guys so basicaly i have this pieces of code in different locations(didnt want to post all code here) in Lua . I want to make a game on love2d. and my problem is: my projectiles are always going in 1 direction. i even make projectile.animnumber but still it gets to go in 1 direction. is there any way to split this massive. (I'm a newbie so don't flame me too much)
projectile = {}
projectile.width = 30
projectile.height = 32
projectile.animNumber = 1
function love.keyreleased(key)
if (key == "space") then
shoot()
love.audio.play(magic_shotSND)
if player.animNumber == 1 then
projectile.animNumber = 1
elseif player.animNumber == 2 then
projectile.animNumber = 2
elseif player.animNumber == 3 then
projectile.animNumber = 3
else
projectile.animNumber = 4
end
end
end
-----
for i,v in ipairs(player.shots) do
if projectile.animNumber == 1 then
v.x = v.x + 300 * dt
elseif projectile.animNumber == 2 then
v.x = v.x - 300 * dt
elseif projectile.animNumber == 3 then
v.y = v.y + 300 * dt
else
v.y = v.y - 300 * dt
end
end
----
function shoot()
local shot = {}
shot.x = player.x - 16
shot.y = player.y - 8
table.insert(player.shots, shot)
end
for i,v in ipairs(player.shots) do
love.graphics.draw(skull, v.x, v.y)
end
Replace all of your "projectile.animNumber" to "v.animNumber"
I think the problem is this:
for i,v in ipairs(player.shots) do
if projectile.animNumber == 1 then
v.x = v.x + 300 * dt
elseif projectile.animNumber == 2 then
v.x = v.x - 300 * dt
elseif projectile.animNumber == 3 then
v.y = v.y + 300 * dt
else
v.y = v.y - 300 * dt
end
end
In your for loop you are checking for projectile.animNumber which doesn't appear previously in your code. Therefore, making the else statement true and causing all projectiles to travel in 1 direction.
Sorry if it was confusing; I'm not that good at explaining things

Resources