Snowflake Query/task gets canceled cause of timelimit - performance

I set up a code, to load Data from Google Analytics from a raw GA table to an adapted table, that offers more insights.
Somehow the Task is not running inside the defined time limit of 3600 seconds.
So the task is getting canceled and no data is loaded.
"Statement reached its statement or warehouse timeout of 3,600 second(s) and was canceled."
Then I load it in a manual way by using a fixed Clause: WHERE gae."DAY"='2020-03-31' instead of
WHERE gae."DAY">=CuRRENT_DATE-1. It takes still much time but works in the end.
How can I make this query faster? Or anyway, solve my problem.
CREATE OR REPLACE TASK dm."Website"."x009_002_all_GA_events"
WAREHOUSE = marketing_wh
SCHEDULE = 'USING CRON 26 5 * * * Europe/Berlin'
AS
merge into DM."Website".ALL_GA_EVENTS_CLEAN target --DM."Website".ALL_GA_EVENTS target
using (
SELECT
GAEVENTACTION AS GAEVENTACTION,
GAEVENTCATEGORY AS GAEVENTCATEGORY,
"DAY" AS Datum,
DEVICE_TYPE AS Device,
EVENT_COUNT AS EventCount,
GAUNIQUEEVENTS AS Uniqueevents,
EVENT_VALUE AS eventvalue,
LABELS AS labelz,
URL AS urlz,
--split_part(LABELS,'/_',2) AS "HITSTAMP",
CASE WHEN split_part(LABELS,'/_',2) IS NOT NULL THEN TRY_CAST(split_part(LABELS,'/_',2) AS timestamp) ELSE NULL END AS "HITSTAMP",
split_part(LABELS,'/_',3) AS EVENT_INFO,
split_part(LABELS,'/_',1) AS "SESSIONID",
CASE
WHEN CONTAINS (URL, '/checkout/')=TRUE THEN split_part(URL,'/',3)
WHEN CONTAINS (URL, '/auto/')=TRUE THEN split_part(split_part(URL,'/',3),'?',1)
WHEN CONTAINS (URL, '/angebote/')=TRUE THEN split_part(URL,'/',3)
ELSE 'no vehicle'
END AS vehicleID,
rank() over (partition BY "SESSIONID" order by "HITSTAMP") as "RANK",
CASE
WHEN (GAEVENTACTION= ('pdp_flash_offer_request' )) THEN
CASE WHEN split_part(LABELS,'/_',2)=(SELECT min(split_part(t2.LABELS,'/_',2))
FROM "DL_Datatap"."PUBLIC"."GA_all_events" t2
WHERE split_part(t2.LABELS,'/_',1)=split_part(gae.LABELS,'/_',1) AND gae.GAEVENTACTION=t2.GAEVENTACTION)
THEN TRUE
ELSE FALSE
END
WHEN (GAEVENTACTION= ('chat_started')) THEN
CASE WHEN split_part(LABELS,'/_',2)=(SELECT min(split_part(t2.LABELS,'/_',2))
FROM "DL_Datatap"."PUBLIC"."GA_all_events" t2
WHERE split_part(t2.LABELS,'/_',1)=split_part(gae.LABELS,'/_',1) AND gae.GAEVENTACTION=t2.GAEVENTACTION)
THEN TRUE
ELSE FALSE
END
WHEN (GAEVENTACTION= ('Direct_checkout_send')) THEN
CASE WHEN split_part(LABELS,'/_',2)=(SELECT min(split_part(t2.LABELS,'/_',2))
FROM "DL_Datatap"."PUBLIC"."GA_all_events" t2
WHERE split_part(t2.LABELS,'/_',1)=split_part(gae.LABELS,'/_',1) AND gae.GAEVENTACTION=t2.GAEVENTACTION)
THEN TRUE
ELSE FALSE
END
WHEN (GAEVENTACTION= ('pdp_offer_request')) THEN
CASE WHEN split_part(LABELS,'/_',2)=(SELECT min(split_part(t2.LABELS,'/_',2))
FROM "DL_Datatap"."PUBLIC"."GA_all_events" t2
WHERE split_part(t2.LABELS,'/_',1)=split_part(gae.LABELS,'/_',1) AND gae.GAEVENTACTION=t2.GAEVENTACTION)
THEN TRUE
ELSE FALSE
END
WHEN (GAEVENTACTION= ('agent-requested')) THEN
CASE WHEN split_part(LABELS,'/_',2)=(SELECT min(split_part(t2.LABELS,'/_',2))
FROM "DL_Datatap"."PUBLIC"."GA_all_events" t2
WHERE split_part(t2.LABELS,'/_',1)=split_part(gae.LABELS,'/_',1) AND gae.GAEVENTACTION=t2.GAEVENTACTION)
THEN TRUE
ELSE FALSE
END
WHEN (GAEVENTACTION= ('SERP_softlead_send')) THEN
CASE WHEN split_part(LABELS,'/_',2)=(SELECT min(split_part(t2.LABELS,'/_',2))
FROM "DL_Datatap"."PUBLIC"."GA_all_events" t2
WHERE split_part(t2.LABELS,'/_',1)=split_part(gae.LABELS,'/_',1) AND gae.GAEVENTACTION=t2.GAEVENTACTION)
THEN TRUE
ELSE FALSE
END
ELSE False
END AS "GOAL_EVENT"
FROM "DL_Datatap"."PUBLIC"."GA_all_events" gae
---WHERE gae."DAY"='2020-03-31'
WHERE gae."DAY">=CuRRENT_DATE-1
) SOURCE
ON target.SESSIONID=SOURCE."SESSIONID" AND target.HITSTAMP=SOURCE."HITSTAMP" AND target.EVENT_ACTION=SOURCE.GAEVENTACTION AND target."Date"=SOURCE.Datum
when NOT matched then INSERT (EVENT_ACTION, EVENT_CATEGORY, "Date", DEVICE, TOTAL_EVENTS, UNIQUE_EVENTS, EVENT_VALUE, EVENT_LABEL, URL, HITSTAMP, EVENT_INFO, SESSIONID, VEHICLEID, EVENT_SEQUENCE, GOAL_EVENT)
VALUES (SOURCE.GAEVENTACTION, SOURCE.GAEVENTCATEGORY, SOURCE.Datum, SOURCE.Device, SOURCE.eventcount, SOURCE.Uniqueevents, SOURCE. eventvalue, SOURCE.labelz, SOURCE.urlz, SOURCE."HITSTAMP", SOURCE.EVENT_INFO, SOURCE."SESSIONID", SOURCE.vehicleid, SOURCE."RANK", SOURCE."GOAL_EVENT"
)

You can increase the task timeout limit as a workaround:
CREATE OR REPLACE TASK dm."Website"."x009_002_all_GA_events"
WAREHOUSE = marketing_wh
SCHEDULE = 'USING CRON 26 5 * * * Europe/Berlin'
USER_TASK_TIMEOUT_MS = 86400000
AS
...
https://docs.snowflake.com/en/sql-reference/sql/create-task.html#optional-parameters
If you need help to tune your SQL, please submit a case to support. As they can see metadata of your tables, and execution plans of your previous runs, they can guide you to tune the query, cluster your target table etc..

Related

How to use Laravel Query Builder to create a left Join inside a Right Join

I have a query that works and it includes
RIGHT JOIN (tblMap LEFT JOIN tlkpMapProfTyp ON tblMap.MapProfTyp = tlkpMapProfTyp.MapProfTyp) ON tlkpMapView.MapViewCode = tblMap.MapViewCode
I am trying to use the Laravel Query Builder but I can't seem to get this (right join at the end) to work.
DB::table('tlkpMapView')
->select('tblMap.Site',
DB::raw(
"CASE
WHEN tblmap.mapelec = 'TRUE' THEN 'Digital Maps'
WHEN tblmap.mappaper = 'TRUE' THEN 'Paper Maps'
ELSE 'Other Maps'
END AS MapType"),
'tblMap.MapScopeCode', 'tblMap.MapScopeNum',
DB::raw(
"CASE
WHEN tblmap.mapplan = 'TRUE' THEN 'Plan View'
END AS MapPlan"),
'tlkpMapProfTyp.MapProfTypDesc', 'tlkpMapView.MapView',
'tblMap.MapNum', 'tblMap.MapTitle',
DB::raw(
"CASE
WHEN tblmap.MapPl = 'TRUE' THEN 'Map includes point-located artifacts/samples'
END AS PL"),
DB::raw(
"CASE
WHEN tblmap.MapCompos = 'TRUE' THEN 'Map is a composite of multiple source maps'
END AS Composite"),
'tblMap.MapComm')
->rightJoin('tblMap', DB::raw(
"leftJoin tlkpMapProfTyp ON tblMap.MapProfTyp = tlkpMapProfTyp.MapProfTyp)ON tlkpMapView.MapViewCode = tblMap.MapViewCode"))
->where('tblMap.Site','=', '5mt11842');
I get this error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tlkpMapProfTyp ON tblMap.MapProfTyp = tlkpMapProfTyp.MapProfTyp)...
Here is the original query:
SELECT
tblMap.Site,
CASE
WHEN tblmap.mapelec = 'TRUE' THEN 'Digital Maps'
WHEN tblmap.mappaper = 'TRUE' THEN 'Paper Maps'
ELSE 'Other Maps'
END AS MapType,
tblMap.MapScopeCode, tblMap.MapScopeNum,
CASE
WHEN tblmap.mapplan = 'TRUE' THEN 'Plan View'
END AS MapPlan,
tlkpMapProfTyp.MapProfTypDesc, tlkpMapView.MapView,
tblMap.MapNum, tblMap.MapTitle,
CASE
WHEN tblmap.MapPl = 'TRUE' THEN 'Map includes point-located artifacts/samples'
END AS PL,
CASE
WHEN tblmap.MapCompos = 'TRUE' THEN 'Map is a composite of multiple source maps'
END AS Composite,
tblMap.MapComm
FROM tlkpMapView
RIGHT JOIN (tblMap LEFT JOIN tlkpMapProfTyp ON tblMap.MapProfTyp = tlkpMapProfTyp.MapProfTyp)
ON tlkpMapView.MapViewCode = tblMap.MapViewCode
WHERE tblMap.mapnum <> '514' AND tblMap.Site = '5mt11842'
I tried to use a leftSubJoin but I got lost so I thought DB::raw would be easier. How can I get this to work?
UPDATE:
I used toSql() instead of get which shows why:
->rightJoin('tblMap', DB::raw(
"leftJoin tlkpMapProfTyp ON tblMap.MapProfTyp = tlkpMapProfTyp.MapProfTyp)
ON tlkpMapView.MapViewCode = tblMap.MapViewCode”))
Using toSql() it translates as this:
right join `tblMap` on leftJoin tlkpMapProfTyp ON tblMap.MapProfTyp
ON tlkpMapView.MapViewCode = tblMap.MapViewCode = ``
It has ‘on’ after tblMap and an extra =‘’ at the end.
What would be the way to get this to work using query builder?

How to query jsonb?

I am storing settings into a config field as:
schema "teams" do
field :owner_id, :integer
field :is_base_team, :boolean
field :config, :map
has_many :team_users, {"team_user", App.TeamUser}
end
So, I need to build query that reads parameter see_owner within config as:
teams_users =
from(t in Team, where: t.owner_id == ^user_id and fragment("?->>'see_owner' == ?", t.config, true))
|> Repo.all()
|> Repo.preload(:team_users)
However, I got error:
** (Postgrex.Error) ERROR (undefined_function): operator does not exist: text == boolean
(ecto) lib/ecto/adapters/sql.ex:395: Ecto.Adapters.SQL.execute_and_cache/7
(ecto) lib/ecto/repo/queryable.ex:127: Ecto.Repo.Queryable.execute/5
(ecto) lib/ecto/repo/queryable.ex:40: Ecto.Repo.Queryable.all/4
(app) web/channels/user_socket.ex:67: App.UserSocket.get_team_users_ids/1
(app) web/channels/user_socket.ex:40: App.UserSocket.connect/2
(phoenix) lib/phoenix/socket/transport.ex:167: Phoenix.Socket.Transport.connect_vsn/6
(phoenix) lib/phoenix/transports/websocket.ex:73: Phoenix.Transports.WebSocket.init/2
(phoenix) lib/phoenix/endpoint/cowboy_websocket.ex:12: Phoenix.Endpoint.CowboyWebSocket.init/3
(cowboy) src/cowboy_handler.erl:64: :cowboy_handler.handler_init/4
(cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
So, how would I add to my query a checking of config.see_owner where config is a jsonb where see_owner is a boolean property ?
I am using ecto 2.0.0-rc.5
EDIT
I have fixed the postgres query as:
from(t in Team, where: t.owner_id == ^user_id and fragment("?->> 'see_owner' = '?'", t.config, true))
in the log I, it was translated as:
SELECT t0."id", t0."owner_id", t0."is_base_team", t0."config" FROM "teams" AS t0 WHERE ((t0."owner_id" = $1) AND t0."config"->> 'see_owner' = 'TRUE') [3]
but the result is empty, however, there are teams with config equals to {"see_owner": true}
EDIT2
Checking with owner_id, I got error:
sql> SELECT t0."id", t0."owner_id", t0."is_base_team", t0."config" FROM "teams" AS t0 WHERE ((t0."owner_id" = $1) AND t0."config"->> 'see_owner' = 'TRUE') [3]
[2016-06-01 17:43:33] [42804] ERROR: cannot subscript type boolean because it is not an array
I removed the owner_id check, but I got empty result:
SELECT t0."id", t0."owner_id", t0."is_base_team", t0."config" FROM "teams" AS t0 WHERE (t0."config"->> 'see_owner' = 'TRUE')
[2016-06-01 17:44:09] 0 rows retrieved in 9ms (execution: 4ms, fetching: 5ms)
I removed the owner_id check, and I used small letters for 'see_owner' = 'TRUE' as 'see_owner' = 'true', I got the results without errors:
sql> SELECT t0."id", t0."owner_id", t0."is_base_team", t0."config" FROM "teams" AS t0 WHERE (t0."config"->> 'see_owner' = 'true')
[2016-06-01 17:46:40] 16 rows retrieved starting from 1 in 7ms (execution: 3ms, fetching: 4ms)
so, I think that Ecto needs to translate boolean type to postrgres native query without converting it to capital letters like TRUE or FALSE as it currently does.
also, I don't know why native query with owner_id got error, as I copied the resulted native query from logs, and it should be working correctly..
The problem here is your conditional, == is not valid in PostgreSQL.
Try this:
from(t in Team, where: t.owner_id == ^user_id and fragment("?->>'see_owner' = ?", t.config, true))

case statement with in case statement

I am not sure what kind of statement I need in order for what i need to do. I would like to add a case statement after the case statement i currently have. Bellow is the case statement.
CASE
WHEN LOCOFF.EXT_SRV_POLYGON IN ('BOF', 'CDA', 'COL', 'DAC', 'GOS', 'KEL', 'KLF', 'LAG', 'LEC', 'MED', 'PUM', 'RIT', 'ROS', 'SAN', 'SPO')
THEN 'GAS'
WHEN LOCOFF.EXT_SRV_POLYGON IN ('CDC', 'COC', 'DAV', 'DPC', 'GRC', 'KEC', 'LCC', 'OTC', 'PAC', 'SAC', 'SPC')
THEN 'ELECTRIC'
ELSE 'MISSING'
END AS TYPE,
I would like to say if EXT_Distworktype is GC or GT and the TYPE is ELECTRIC,
GIVE ME TYPE THAT SAYS WRONG POLYGON OR IF THE EXT_DISTWORKTYPE is EC, ES, or ET and Type is GAS then give me a TYPE that say WRONG POLYGON.
here is my full query:
SELECT WO.WONUM AS "Work Order",
WO.LOCATION AS "Location",
WO.STATUS AS "Status",
WO.DESCRIPTION,
WO.ACTFINISH AS "Actual Finish",
WO.PARENT AS "Parent WO",
WO.WORKTYPE AS "Work Type",
CASE
WHEN WO.REPORTDATE IS NULL
THEN TO_DATE('29-JAN-15 00:00:00', 'DD-MON-YY HH24:MI:SS')
ELSE WO.REPORTDATE
END AS "Reported Date",
WO.SITEID AS "Site",
CASE
WHEN WO.ACTFINISH IS NULL
THEN GREATEST(TRUNC(TO_DATE(SysDate, 'DD-MON-YY HH24:MI:SS') - TO_DATE(WO.REPORTDATE, 'DD-MON-YY HH24:MI:SS'), 6), 0)
WHEN WO.STATUS NOT IN ('COMP')
THEN GREATEST(TRUNC(TO_DATE(SysDate, 'DD-MON-YY HH24:MI:SS') - TO_DATE(WO.REPORTDATE, 'DD-MON-YY HH24:MI:SS'), 6), 0)
WHEN WO.STATUS IN ('COMP')
THEN GREATEST(TRUNC(TO_DATE(WO.ACTFINISH, 'DD-MON-YY HH24:MI:SS') - TO_DATE(WO.REPORTDATE, 'DD-MON-YY HH24:MI:SS'), 6), 0)
END AS "Age",
WO.EXT_DISTWORKTYPE,
LOCOFF.EXT_OFFICE,
CASE
WHEN LOCOFF.EXT_SRV_POLYGON IN ('BOF', 'CDA', 'COL', 'DAC', 'GOS', 'KEL', 'KLF', 'LAG', 'LEC', 'MED', 'PUM', 'RIT', 'ROS', 'SAN', 'SPO')
THEN 'GAS'
WHEN LOCOFF.EXT_SRV_POLYGON IN ('CDC', 'COC', 'DAV', 'DPC', 'GRC', 'KEC', 'LCC', 'OTC', 'PAC', 'SAC', 'SPC')
THEN 'ELECTRIC'
ELSE 'MISSING'
END AS TYPE,
LOCOFF.EXT_STATECODE,
WO.OWNERGROUP,
WO.EXT_JOBCODE
FROM LOCATIONS LOCOFF
RIGHT JOIN WORKORDER WO
ON WO.LOCATION = LOCOFF.LOCATION
WHERE WO.STATUS NOT IN ('CLOSE', 'WAIV', 'CAN', 'REJ', 'REVOKED')
AND LOCOFF.SITEID = 'OPS'
AND WO.EXT_JOBCODE NOT LIKE 'A%' AND WO.E`enter code here`XT_JOBCODE NOT LIKE 'B%'
AND WO.EXT_JOBCODE NOT IN ('K008','K009','I006','I007','I008');
You can nest case expressions if you only want to modify the existing column value (rather than adding a separate column with the 'wrong polygon' message):
CASE
WHEN LOCOFF.EXT_SRV_POLYGON IN ('BOF', 'CDA', 'COL', 'DAC', 'GOS', 'KEL', 'KLF', 'LAG', 'LEC', 'MED', 'PUM', 'RIT', 'ROS', 'SAN', 'SPO')
THEN
CASE
WHEN WO.EXT_DISTWORKTYPE IN ('EC', 'ES', 'ET')
THEN 'WRONG POLYGON'
ELSE 'GAS'
END
WHEN LOCOFF.EXT_SRV_POLYGON IN ('CDC', 'COC', 'DAV', 'DPC', 'GRC', 'KEC', 'LCC', 'OTC', 'PAC', 'SAC', 'SPC')
THEN
CASE
WHEN WO.EXT_DISTWORKTYPE IN ('GC', 'GT')
THEN 'WRONG POLYGON'
ELSE 'ELECTRIC'
END
ELSE 'MISSING'
END AS TYPE,
Quick SQL Fiddle demos with the same made-up data; your original case and this nested case, which gives:
EXT EX TYPE
--- -- -------------
BOF GC GAS
CDA GT GAS
SPO ES WRONG POLYGON
CDC EC ELECTRIC
COC ET ELECTRIC
SPC GC WRONG POLYGON
This is a bit simpler than trying to do a second step that interprets the TYPE value, because you can't refer to a column alias in the same level of query; you would need to make your existing query into an inline view.
Will an extra case not do it?
CASE
WHEN WO.EXT_DISTWORKTYPE IN ('GC', 'GT')
AND LOCOFF.EXT_SRV_POLYGON IN ('CDC', 'COC', 'DAV', 'DPC', 'GRC', 'KEC', 'LCC', 'OTC', 'PAC', 'SAC', 'SPC') --electronic
THEN 'WRONG POLYGON'
CASE
WHEN WO.EXT_DISTWORKTYPE IN ('EC', 'ES', 'ET' )
AND LOCOFF.EXT_SRV_POLYGON IN ('BOF', 'CDA', 'COL', 'DAC', 'GOS', 'KEL', 'KLF', 'LAG', 'LEC', 'MED', 'PUM', 'RIT', 'ROS', 'SAN', 'SPO') --gas
THEN 'WRONG POLYGON'
ELSE 'MSSING'
END AS YOURANSWER,
You could do an case within a case
CASE
WHEN (TRUE) THEN
CASE WHEN (true) THEN 'a' ELSE 'b' END
ELSE
'A'
END

Select a random value from a table based on another random value

I'm taking all my friend's weapon stats list for an RP he's running and turning it into a generator, but I'm having issues excluding certain values based on others. For example, I don't want a secondary auto rifle. Is there a way I can exclude certain values based on another output?
local myclasses = { 'Primary', 'Secondary', 'Heavy' }
local myprimaries = { 'Auto Rifle', 'Scout Rifle', 'Pulse Rifle', 'Sniper Rifle', 'Hand Cannon' }
local mysecondaries = { 'Shotgun', 'Sidearm', 'SMG/SEG' }
print( myclasses[ math.random(#myclasses) ] )
if 'Primary' then
print( myprimaries[ math.random(#myprimaries) ] )
elseif 'Secondary' then
print( mysecondaries[ math.random(#mysecondaries) ] )
end
The problem is with the condition:
if 'Primary' then
will always evaluated as true, because any value evaluated as true except false and nil.
What you want is:
local rand_class = myclasses[math.random(#myclasses)]
print(rand_class)
if rand_class == 'Primary' then
print( myprimaries[math.random(#myprimaries)] )
elseif rand_class == 'Secondary' then
print( mysecondaries[math.random(#mysecondaries)] )
end
And don't forget to seed the random.

Problem in oracle query

Hai guys,
I've a query in which i need to interchange the values of two fields.
The query is as follows:
SELECT TO_DATE(A.G_LEDGER_DATE,'dd/mm/YYY')as G_LEDGER_DATE,C.ACC_MASTER_NAME,
A.G_LEDGER_REF_NO ,
NVL(CASE WHEN B.G_LEDGER_SECTION = 1 THEN
CASE WHEN
(SELECT COUNT(*)FROM SOSTRANS.ACC_GEN_LEDGER WHERE G_LEDGER_SECTION = B.G_LEDGER_SECTION AND G_LEDGER_ID = B.G_LEDGER_ID)> 1 THEN
B.G_LEDGER_VALUE ELSE A.G_LEDGER_VALUE END END,0) AS G_LEDGER_DR_VALUE,
NVL(CASE WHEN B.G_LEDGER_SECTION = -1 THEN
CASE WHEN
(SELECT COUNT(*) FROM SOSTRANS.ACC_GEN_LEDGER WHERE G_LEDGER_SECTION = B.G_LEDGER_SECTION AND G_LEDGER_ID = B.G_LEDGER_ID)> 1 THEN
B.G_LEDGER_VALUE ELSE A.G_LEDGER_VALUE END END,0) AS G_LEDGER_CR_VALUE,
B.G_LEDGER_SECTION,C.ACC_MASTER_ID,SUBSTR(A.G_LEDGER_REF_NO,0,3) AS Types,'Z' as OrderChar ,
CASE WHEN A.G_LEDGER_REMARK IS NULL THEN B.G_LEDGER_REMARK ELSE A.G_LEDGER_REMARK END AS Narration
FROM SOSTRANS.ACC_GEN_LEDGER A
LEFT OUTER JOIN SOSTRANS.ACC_GEN_LEDGER B ON A.G_LEDGER_ID = B.G_LEDGER_ID
LEFT OUTER JOIN SOSMASTER.ACC_ACCOUNT_MASTER C ON A.ACC_MASTER_ID = C.ACC_MASTER_ID WHERE A.G_LEDGER_CANCEL='N' AND
B.ACC_MASTER_ID = 'MSOS000001' AND
A.ACC_MASTER_ID <> 'MSOS000001' AND
A.G_LEDGER_SECTION <> B.G_LEDGER_SECTION AND
A.G_LEDGER_DATE >= '25/sep/2009' AND
A.G_LEDGER_DATE<='26/sep/2009'
ORDER BY OrderChar,G_LEDGER_DATE
Now i get the output as
... G_LEDGER_DR_VALUE G_LEDGER_CR_VALUE .....
... 2000 0 .....
... 3000 0 .....
... -1000 0 .....
I need to get the negetive value of the G_LEDGER_DR_VALUE side in G_LEDGER_CR_VALUE and if negetive value exists in G_LEDGER_CR_VALUE then it should be in the G_LEDGER_DR_VALUE field
Can anyone help me to solve this?
If I understood your question well, you select a value (that I will call g_ledger_value) that you want to appear in a different column depending on its sign.
This is how I would do it :
SELECT
CASE WHEN t.g_ledger_value>0 THEN t.g_ledger_value ELSE 0 END AS g_ledger_dr_value,
CASE WHEN t.g_ledger_value<0 THEN t.g_ledger_value ELSE 0 END AS g_ledger_cr_value
FROM
(SELECT g_ledger_value FROM mytable) t;
It sounds like a combination of SIGN() and CASE is what you need ...
CASE WHEN SIGN(G_LEDGER_DR_VALUE) = -1 then ...
ELSE ...
END
etc
SELECT G_LEDGER_DR_VALUE,
CASE WHEN G_LEDGER_DR_VALUE < 0
THEN G_LEDGER_CR_VALUE
ELSE G_LEDGER_DR_VALUE
END
FROM (...)
Is it that you mean? I suggest calculate values of CR___VALUE and DR_VALUE in subquery, and then in wrapping query make CASE which returns you correct value.

Resources