I am trying to create a table in Dbeaver and I have encountered an issue. I have posted my code below. My code works fine if a well has completion types of both 'Frac' of 'MSFRAC'.However, this well with a uwi= '100042204410W500' has only 'MSFRAC' as completion type. When I run my code, it shows nothing in output (image 2).I wrote a small part of code at the top to check this well's information and you can see the output shows that all well completion types are MSFRAC.
What cause this empty output issue? As you can see here, I am using common expression tables for key words "FRAC","MSFRAC",and"%PERF". So my assumption is if FRAC and %PERF% has no record for this well, even tho MSFRAC has data, it will cause the output to be empty???
Please help!!!! Much Appreciated1
WITH frac as (
SELECT
c.uwi,
case
when AVG(c.base_depth - c.top_depth) =0
then null
else AVG(c.base_depth - c.top_depth)
end AS Avg_Frac_Stage_Spacing,
COUNT(c.completion_type) AS Frac_Stage_Count,
min(c.completion_date) as Min_Completion_Date,
max(c.completion_date) as Max_Completion_Date
FROM
well_completion AS c
WHERE
c.completion_type = 'FRAC'
--or c.completion_type='MSFRAC'
GROUP BY
c.uwi
),
msfrac as (
SELECT
c.uwi,
case
when max(c.base_depth - c.top_depth) >500
then (sum(c.base_depth)-sum(c.top_depth)-max(c.base_depth - c.top_depth))/nullif((count(c.completion_type)-1),0)
else AVG(c.base_depth - c.top_depth)
end AS Avg_Msfrac_Stage_Spacing,
case
when max(c.base_depth - c.top_depth) >500
then count(c.completion_type)-1
else count(c.completion_type)
end AS Msfrac_Stage_Count
FROM
well_completion AS c
WHERE
c.completion_type = 'MSFRAC'
--or c.completion_type='MSFRAC'
GROUP BY
c.uwi
),
jetperf as (
SELECT
c.uwi,
COUNT(c.completion_type) AS Perf_Count,
avg(c.base_depth-c.top_depth ) as Avg_cluster_interval
--Avg(c.x_perf_shots) as SPM
--Avg((c.base_depth - c.top_depth)*(c.x_perf_shots)) as Avg_counts_per_cluster
FROM
well_completion AS c
WHERE
c.completion_type like '%PERF%'
--and (c.base_depth - c.top_depth) <= 2
GROUP BY
c.uwi
)
select
f.uwi,
f.Frac_Stage_Count+m.Msfrac_Stage_Count as Stage_Count,
f.Min_Completion_Date,
f.Max_Completion_Date,
(m.Avg_Msfrac_Stage_Spacing*m.Msfrac_Stage_Count+f.Avg_Frac_Stage_Spacing*f.Frac_Stage_Count)/(m.Msfrac_Stage_Count+f.Frac_Stage_Count) as Stage_Spacing,
--f.completion_strat_unit_id,
round(j.Perf_count/(f.Frac_Stage_Count+m.Msfrac_Stage_Count),2) as Avg_number_of_clusters,
j.Avg_cluster_interval,
j.perf_Count,
--j.SPM,
--j.Avg_counts_per_cluster,
j.Avg_cluster_interval/f.Avg_frac_stage_spacing as completion_multipledividebysingle_ratio,
(f.Avg_Frac_Stage_Spacing+m.Avg_Msfrac_Stage_Spacing)/j.Perf_Count *(f.Frac_Stage_Count+m.Msfrac_Stage_Count) AS Avg_cluster_spacing,
m.Avg_Msfrac_Stage_Spacing,
ihs_ppdm3_own1.well.profile_type as Profile_type,
ihs_ppdm3_own1.well."operator" as Operator,
ihs_ppdm3_own1.well.province_state as Province_state,
ihs_ppdm3_own1.well.x_td_tvd as x_td_tvd,
ihs_ppdm3_own1.well.max_tvd as max_tvd,
ihs_ppdm3_own1.well.td_strat_unit_id as td_strat_unit
FROM
frac as f
inner join
jetperf as j
ON
f.uwi = j.uwi
inner join
ihs_ppdm3_own1.well
ON
ihs_ppdm3_own1.well.uwi=f.uwi
inner join
msfrac as m
ON
m.uwi=f.uwi
where ihs_ppdm3_own1.well.profile_type ='H'and f.uwi ='100042204410W500'
Related
I'm using a sql query to export a database from my company's program.
Everything seems to be fine till I change the date on the "where" statement with a previous one.
Please find below the code:
SELECT p."Index", p."PSN" || CAST(p."PNR"as int) AS ID,
p."PSN" AS Serie, cast(p."PNR"as int) AS Numar,
pr."PINDate" AS r_gdate,
CASE WHEN pr."AsigEID"='10' THEN pr."PrimSUM" ELSE
pr."PrimSUM"*valuta1."EXCValue" END AS r_prima_lei,
CASE WHEN pr."AsigEID"='2'
THEN pr."PrimSUM"
ELSE CASE WHEN pr."AsigEID"='10' THEN pr."PrimSUM"/valuta2."EXCValue"
ELSE pr."PrimSUM"*valuta1."EXCValue"/valuta2."EXCValue"
END
END AS r_prima_eur,
CASE WHEN pr."AsigEID"='10' THEN pr."AsigSUM" ELSE
pr."AsigSUM"*valuta1."EXCValue" END as r_sa_lei,
CASE WHEN pr."AsigEID"='2'
THEN pr."AsigSUM"
ELSE CASE WHEN pr."AsigEID"='10' THEN pr."AsigSUM"/valuta2."EXCValue"
ELSE pr."AsigSUM"*valuta1."EXCValue"/valuta2."EXCValue"
END
END AS r_sa_eur,
pr."AsigStart", pr."AsigEnd", risc."Code", plink."Index"
FROM "PolsRisc" AS pr
LEFT JOIN "Pols" as p ON p."Index" = pr."PID"
LEFT JOIN "Riscs" as risc ON pr."RID" = risc."Index"
LEFT JOIN "PRLNK" plink ON plink."PTID" = p."PTID" AND plink."RID" = risc."Index"
LEFT JOIN "EXCValues" valuta1 ON valuta1."AtDate" = pr."AsigStart" AND valuta1."EID" = pr."AsigEID"
LEFT JOIN "EXCValues" valuta2 ON valuta2."AtDate" = pr."AsigStart" AND valuta2."EID"='2'
WHERE pr."PINDate" > '2020-08-01' AND pr."IsRezil" = 'false';
When I'm using '2020-08-01' the query works well. When I try to change it to a previous one eg. '2010-01-01' a get an error:
ERROR: invalid input syntax for integer: ""
SQL state: 22P02
I was looking for a solution on the previous posts but I didn't manage to solve this issue.
It looks like it is returning "" or a null value into one of the columns you are using integer logic for. The date change is just filtering out the data that would crash it.
You may need to use coalesce to reassign the nulls as 0 and then cast it back into being an int
select
cast(coalesce(table.column, 0) as int) as result
from table
I would advice to read the chapter http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS
. It's a brief and informative read.The cause for the error message is that '' is an empty string that has no representation in a numeric type like integer
I have a SQL query where I am trying to replace null results with zero. My code is producing an error
[1]: ORA-00923: FROM keyword not found where expected
I am using an Oracle Database.
Select service_sub_type_descr,
nvl('Single-occupancy',0) as 'Single-occupancy',
nvl('Multi-occupancy',0) as 'Multi-occupancy'
From
(select s.service_sub_type_descr as service_sub_type_descr, ch.claim_id,nvl(ci.item_paid_amt,0) as item_paid_amt
from table_1 ch, table_" ci, table_3 s, table_4 ppd
where ch.claim_id = ci.claim_id and ci.service_type_id = s.service_type_id
and ci.service_sub_type_id = s.service_sub_type_id and ch.policy_no = ppd.policy_no)
Pivot (
count(distinct claim_id), sum(item_paid_amt) as paid_amount For service_sub_type_descr IN ('Single-occupancy', 'Multi-occupancy')
)
This expression:
nvl('Single-occupancy',0) as 'Single-occupancy',
is using an Oracle bespoke function to say: If the value of the string Single-occupancy' is not null then return the number 0.
That logic doesn't really make sense. The string value is never null. And, the return value is sometimes a string and sometimes a number. This should generate a type-conversion error, because the first value cannot be converted to a number.
I think you intend:
coalesce("Single-occupancy", 0) as "Single-occupancy",
The double quotes are used to quote identifiers, so this refers to the column called Single-occupancy.
All that said, fix your data model. Don't have identifiers that need to be quoted. You might not have control in the source data but you definitely have control within your query:
coalesce("Single-occupancy", 0) as Single_occupancy,
EDIT:
Just write the query using conditional aggregation and proper JOINs:
select s.service_sub_type_descr, ch.claim_id,
sum(case when service_sub_type_descr = 'Single-occupancy' then item_paid_amt else 0 end) as single_occupancy,
sum(case when service_sub_type_descr = 'Multi-occupancy' then item_paid_amt else 0 end) as multi_occupancy
from table_1 ch join
table_" ci
on ch.claim_id = ci.claim_id join
table_3 s
on ci.service_type_id = s.service_type_id join
table_4 ppd
on ch.policy_no = ppd.policy_no
group by s.service_sub_type_descr, ch.claim_id;
Much simpler in my opinion.
for column aliases, you have to use double quotes !
don't use
as 'Single-occupancy'
but :
as "Single-occupancy",
SELECT p.pnum, p.pname
FROM professor p, class c
WHERE p.pnum = c.pnum AND c.cnum = CS245 AND (SELECT COUNT(*) FROM (SELECT MAX(m.grade), MAX(m.grade) - MIN(m.grade) AS diff
FROM mark m WHERE m.cnum = c.cnum AND m.term = c.term AND m.section = c.section AND diff <= 20)) = 3
Incorrect syntax near ')'. Expecting AS, FOR_PATH, ID, or QUOTED_ID.
Consider the following example:
SELECT COUNT(1)
FROM SYSCAT.TABLES T
WHERE
(
-- SELECT COUNT(1)
-- FROM
-- (
SELECT COUNT(1)
FROM SYSCAT.COLUMNS C
WHERE C.TABSCHEMA=T.TABSCHEMA AND C.TABNAME=T.TABNAME
-- )
) > 50;
The query above works as is. But the problem is, that if you uncomment the commented out lines, you get the following error message: "T.TABNAME" is an undefined name. and least in Db2 for Linux, Unix and Windows.
You can't push external to the sub-select column references too deeply.
So, your query is incorrect.
It's hard to correct it, until you provide the task description with data sample and the result expected.
I can see a potential syntax errors: It seems that CS245 refers to a value c.cnum may take and not a column name. If that is the case, it should be enclosed in single quotes.
I'm having a Sql code like as follows
Select a.ItemCode, a.ItemDesc
From fn_BOM_Material_Master('A', #AsOnDate, #RptDate, #BranchID, #CompID)a
Left Outer Join fn_INV_AsOnDate_Stock(#StockDate, #AsOnDate, #RptDate, #BranchID, #CompID, #Finyear)b
On a.ItemCode=b.ItemCode and b.WarehouseCode<>'WAP'
and a.BranchID=b.BranchID and a.CompID=b.COmpID
Where a.ItemNatureCode = 'F' and a.BranchID = #BranchID and a.CompID = #CompID
Group by a.ItemCode, a.ItemDesc
Having sum(b.CBQty)<=0
Here the problem is that im passing an "#ShowZeroStock" value as as bit if the "#ShowZeroStock" value is '1' then Having should not be validated or (i.e: All values from the table should be returned including zero)
So How to change the query based on passed bit value "#ShowZeroStock"
I can Use "If else " condition at the top and remove having in else part, but for a lengthy query i can't do the same.
Is this the logic you want?
Having sum(b.CBQty) <= 0 or #ShowZeroStock = 1
Problem:
A Database collumn has a Tristate (0,1,2).
Each of the values are used serversidely.
The Clientcode (which cant be changed anymore) is only able to understand '0,1'.
In the Clients view '1' is identic with '2'. So I want to change the SQL Query in the Database to return '1', if the specific value is > 0.
My current Solution is combining 2 Selects (using UNION SELECT) with different WHERE-Clauses and returning '1' or '0' as static values. Now I'm looking for a solution to 'translate' the value within only ONE SELECT statement.
This is my current Solution:
SELECT
dbo.Nachricht.NachrichtID, dbo.Nachricht.Bezeichnung, '1' AS BetrifftKontoeinrichtung,
FROM dbo.Nachricht INNER JOIN dbo.AdditionalData
ON dbo.Nachricht.NachrichtID = dbo.AdditionalData.NachrichtID
WHERE (dbo.Nachricht.NachrichtID in ( 450,439 ))
AND dbo.AdditionalData.BetrifftKontoeinrichtung > 0
UNION SELECT
dbo.Nachricht.NachrichtID, dbo.Nachricht.Bezeichnung, '0' AS BetrifftKontoeinrichtung,
FROM dbo.Nachricht INNER JOIN dbo.AdditionalData
ON dbo.Nachricht.NachrichtID = dbo.AdditionalData.NachrichtID
WHERE (dbo.Nachricht.NachrichtID in ( 450,439 ))
AND dbo.AdditionalData.BetrifftKontoeinrichtung = 0
You can use a case statement, like this:
SELECT
dbo.Nachricht.NachrichtID, dbo.Nachricht.Bezeichnung,
CASE WHEN dbo.AdditionalData.BetrifftKontoeinrichtung = 0
THEN '0' ELSE '1'
END AS BetrifftKontoeinrichtung,
FROM dbo.Nachricht
INNER JOIN dbo.AdditionalData
ON dbo.Nachricht.NachrichtID = dbo.AdditionalData.NachrichtID
WHERE (dbo.Nachricht.NachrichtID in ( 450,439 ))
Looks like you need to use CASE. A decent tutorial here
http://www.databasejournal.com/features/mssql/article.php/3288921/T-SQL-Programming-Part-5---Using-the-CASE-Function.htm
See the worked example
If you just CAST(CAST(val AS BIT) AS INT) you will get integer 0 for 0 and integer 1 for everything else.