ADO.NET : how to run sql query with parameters? - postgresql

version is PostgreSQL 14.2
I have perfect sql query:
select
details ->> 'id' as vr_id,
details ->> 'lastname' as lastname,
details ->> 'birthdate' as birthday,
jsonb_path_query_first(details,
'$.contacts.phoneList[*] ? (#.fullNumber == "0671234567" && #.verifyStateId == 1)'
) ->> 'businessId' as businessId,
jsonb_path_query_first(details,
'$.contacts.phoneList[*] ? (#.fullNumber == "0671234567" && #.verifyStateId == 1)'
) ->> 'fullNumber' as fullnumber
from public.information
where details #? '$.contacts.phoneList[*] ? (#.fullNumber == "0671234567" && #.verifyStateId == 1)';
I need use in this SQL the parameters for phone number and stateId.
vPhone varchar := '0671234567'; -- fullNumber
vStateId int := 1; -- verifyStateId
something like that
select
details ->> 'id' as vr_id,
details ->> 'lastname' as lastname,
details ->> 'birthdate' as birthday,
jsonb_path_query_first(details,
'$.contacts.phoneList[*] ? (#.fullNumber == "vPhone" && #.verifyStateId == vStateId)'
) ->> 'businessId' as businessId,
jsonb_path_query_first(details,
'$.contacts.phoneList[*] ? (#.fullNumber == "vPhone" && #.verifyStateId == vStateId)'
) ->> 'fullNumber' as fullnumber
from public.information
where details #? '$.contacts.phoneList[*] ? (#.fullNumber == "vPhone" && #.verifyStateId == vStateId)';
I plan use this sql as procedure for .Net Core API.
How I can to give these parameters to sql-query?

Related

pyspark sql with inline case when join condition

This is the original SQL with inline case when condition:
select *
from table_a
LEFT JOIN table_b
ON case when table_a.key not in ('1','2') then '0' else table_a.key end = table_b.key
What is the equivalent pyspark code?
I was trying when().otherwise() function and if() function but neither worked.
table_a=spark.sql('''select 1 as key union select 2 as key''')
table_a.show()
table_b=spark.sql('''select 3 as key union select 0 as key''')
table_b.show()
val join_condition = when(((table_a.key == '1') | (table_a.key == '2')), table_a.key == table_b.key).otherwise(('0' == table_b.key))
df = table_a.join(table_b, join_condition, 'leftouter').select(table_a['*'], table_b['*'])
df = table_a.join(table_b, ((if(((table_a.key == '1') | (table_a.key == '2')), ('0'), (table_a.key))) == table_b.key), 'leftouter')\
.select(table_a['*'], table_b['*'])
Thank you!
Whenever you have a multi conditions like this (table_a.key == '1' | table_a.key == '2'), you'd have to wrap each of them separately like this ((table_a.key == '1') | (table_a.key == '2'))

Filter by Age in EFCore using Postgresql - Npgsql

Is it possible to translate this following Postgresql query to EFCore?
SELECT "applic"."age" FROM (
SELECT EXTRACT(YEAR FROM age(birthdate)) :: int AS "age" FROM public.applicant
) AS "applic"
WHERE "applic"."age" < 50;
I've looked into the documents but I can't find anything helpful.
A solution that works:
var applicants = from s in this.RepositoryContext.Applicants select s;
if (query.AgeStart != null && query.AgeEnd != null)
{
applicants = applicants.Where(c => (DateTime.Today.Year - c.BirthDate.Year) >= query.AgeStart && (DateTime.Today.Year - c.BirthDate.Year) < query.AgeEnd);
}

converting SSRS report to Crystal Report

I have this code to convert over to CR. I am having the most issue with converting #ReportType over to a formula and parameter to CR. Any suggestions of what to do? I am stumped and no support at work.
select i.IncidentTypeDescription,
isnull(sum(datediff(second, i.IncidentStartedDateTime, i.IncidentDate)), 0) as avgrectoinit,
isnull(sum(datediff(second, i.IncidentDate, i.FirstUnitDispatchedTime)), 0) as avginittodisp,
isnull(abs(sum(datediff(second, i.FirstUnitDispatchedTime, i.FirstUnitArrivedTime))), 0) as avgdisptoarrive,
isnull(sum(datediff(second, i.FirstUnitArrivedTime, i.RouteClosedTime)), 0) as avgarrivetoclose,
count(*) as reccount
FROM MV_Incident i
WHERE i.IncidentNumber LIKE N'bvpd%'
AND i.ReportNumber LIKE N'bvpd%'
and i.IncidentStartedDateTime >= #StartDate
and i.IncidentStartedDateTime < dateadd(day, 1, #EndDate)
and (
(#ReportType = 'All') and (i.CallSource = i.CallSource or i.CallSource is null) or
(#ReportType = 'Officer' and (i.CallSource in ('Officer', 'Mobile', 'Field Initiated', 'Mobile Initiated'))) or
(#ReportType = 'NonOfficer' and (i.CallSource not in ('Officer', 'Mobile', 'Field Initiated', 'Mobile Initiated') or i.CallSource is NULL))
)
group by i.IncidentTypeDescription
order by i.IncidentTypeDescription
You can use the query as it is in crystal reports using the command option while creating a database connection.
Second option would be, USe the below query and create parameters and write your conditions in record selection formula which will add where clause to crystal reports.
select i.IncidentTypeDescription, i.IncidentNumber ,i.ReportNumber ,i.CallSource ,
isnull(sum(datediff(second, i.IncidentStartedDateTime, i.IncidentDate)), 0) as avgrectoinit,
isnull(sum(datediff(second, i.IncidentDate, i.FirstUnitDispatchedTime)), 0) as avginittodisp,
isnull(abs(sum(datediff(second, i.FirstUnitDispatchedTime, i.FirstUnitArrivedTime))), 0) as avgdisptoarrive,
isnull(sum(datediff(second, i.FirstUnitArrivedTime, i.RouteClosedTime)), 0) as avgarrivetoclose,
count(*) as reccount
FROM MV_Incident i
group by i.IncidentTypeDescription,i.IncidentNumber ,i.ReportNumber ,i.CallSource
order by i.IncidentTypeDescription,i.IncidentNumber ,i.ReportNumber ,i.CallSource
Now to crystal report design and create two parameters Start date and End Date and write your where condition as it is in record selection formula as:
i.IncidentNumber LIKE N'bvpd%'
AND i.ReportNumber LIKE N'bvpd%'
and i.IncidentStartedDateTime >= #StartDate
and i.IncidentStartedDateTime < dateadd(day, 1, #EndDate)
and (
(#ReportType = 'All') and (i.CallSource = i.CallSource or i.CallSource is null) or
(#ReportType = 'Officer' and (i.CallSource in ('Officer', 'Mobile', 'Field Initiated', 'Mobile Initiated'))) or
(#ReportType = 'NonOfficer' and (i.CallSource not in ('Officer', 'Mobile', 'Field Initiated', 'Mobile Initiated') or i.CallSource is NULL))
)
Don't forget to create reporttype formula.
Let me know incase any issue

EF 4.4 freeze on large table query

I m using EF 4.4 on ASP.NET MVC project with SQL server and my below query freeze every run.
PSSAL has 426.000 rows and PU_CustProduct has 1600 rows
Have any idea or offer for query?
var model = (from p in dbCont.PU_CustProduct
where !(from pt in dbCont.PSSAL
where pt.PSSAL_DATE > d1
&& pt.PSSAL_PSPRP_ID == p.CustProductID
select pt.PSSAL_PSPRP_ID).Contains(p.CustProductID)
&& p.IsActive == true
&& (!p.IsSleepMode == true)
&& p.ProductType == intId
&& p.CustomerID != null
&& p.IsSychSuspend != true
select new WarningViewModel
{
PrpName = p.LicenceName,
CrmID = p.CustomerID.ToString(),
PsprpId = p.CustProductID.ToString(),
}).Distinct().OrderBy(o => o.PrpName).ToList();

ERROR: invalid input syntax for type date: ""

I have the following psql query and can't understand why I get error ERROR: invalid input syntax for type date: "".
My query looks as follows:
SELECT count(*) FROM campaigns
WHERE
dstart >= '2010-09-02' AND
dend <= '2010-09-02' AND
status != 'S' AND
status != 'C' AND
status != 'E' AND
(dsignoff <> '' AND dsignoff is not null) AND
(dstart <> '' AND dstart is not null) AND
(dend <> '' AND dend is not null) AND
clientid=20005294;
dstart,dend and dsignoff are all defined as date types.
Since dstart,dend and dsignoff are defined as date, they can not be compared to string that represents invalid date (''). Try this:
SELECT count(*) FROM campaigns
WHERE
dstart >= '2010-09-02' AND
dend <= '2010-09-02' AND
status != 'S' AND
status != 'C' AND
status != 'E' AND
(dsignoff is not null) AND
(dstart is not null) AND
(dend is not null) AND
clientid=20005294;