MS Access SQL Case When expression - tsql

I am trying to convert a block of code from SQL Server Management Studio to an Access Database and am having some trouble with my Case When expressions.
What am I missing in the Case When below so it is compatible with MS Access Queries?
Case WHEN E.MemberStartDate <= "2018-01-01 AND E.MemberEndDate >= "2018-12-31"
THEN ""
WHEN "2018-01-01" <= E.MemberEndDate AND "2018-01-31" >=
E.MemberStartDate THEN "X" ELSE "" END AS Cov1Jan

Try with IIf and octothorpes (#):
IIf(E.MemberStartDate <= #2018/01/01 AND E.MemberEndDate >= #2018/12/31#,
Null,
IIf(#2018/01/01# <= E.MemberEndDate AND #2018/01/31# >= E.MemberStartDate,
"X",
Null)) AS Cov1Jan

You are missing that MS-Access doesn't support case. So:
switch(E.MemberStartDate <= "2018-01-01 AND E.MemberEndDate >= "2018-12-31", "",
"2018-01-01" <= E.MemberEndDate AND "2018-01-31" >=
E.MemberStartDate, "X"
1=1, "") AS Cov1Jan

Related

Database migration syntax

I am unsure how to rewrite this. I understand the issue or at least I think I do. I am unable to use instances of t before it is defined by the follow "AS" statement. How can I resolve this so that so that it works the same way without using the instance of t before definition?
The error that I am getting thrown from the converter is "unable to convert .t"
Converted code
INSERT INTO t$trades_long (portfolio_name, fund, cusip, td_num, desc_instmt, trd_price, trd_trade_dt, t.trd_settle_dt, trd_counterparty, trd_td_par, tran_type, sec_type, trd_type, t.trd_trader)
SELECT
p.portfolio_name, t.fund, t.cusip, t.td_num, a.desc_instmt, t.trd_price, t.trd_trade_dt, t.trd_settle_dt, t.trd_counterparty, t.trd_td_par, t.tran_type, a.sec_type, 'long' AS trd_type, trd_trader
FROM pfi_pilot.br_transaction AS t, pfi_pilot.br_portfolio_group AS p, pfi_pilot.br_asset AS a
WHERE t.fund = p.fund AND p.portfolio_group = 'ALL_FUNDS' AND t.cusip = a.cusip AND t.tran_type IN ('BUY', 'SELL', 'ISSUE', 'ALLOC') AND t.trd_status <> 'C' AND a.sm_sec_group NOT IN ('CASH', 'FUTURE', 'FX', 'FUND', 'SWAP', 'OPTION') AND a.sec_type != 'MBS_TBA' AND t.trd_counterparty NOT IN ('IFUND', 'GHOST', 'SPO', 'SPOBO', 'CONV', 'ASSGN') AND t.trd_trade_dt >= par_from_dt AND t.trd_trade_dt <= par_to_dt AND t.cusip NOT IN (SELECT DISTINCT
cusip
FROM pfi_pilot.br_asset
WHERE (COALESCE(risk_country, country) = 'US' AND sm_sec_type IN ('GOVT', 'TBILL'))) AND t.trd_td_par > 0;
Original code
insert into #trades_long(portfolio_name, fund, cusip, td_num, desc_instmt, trd_price, trd_trade_dt, t.trd_settle_dt,
trd_counterparty, trd_td_par, tran_type , sec_type, trd_type, t.trd_trader)
select p.portfolio_name, t.fund, t.cusip, t.td_num, a.desc_instmt, t.trd_price, t.trd_trade_dt, t.trd_settle_dt,
t.trd_counterparty, t.trd_td_par, t.tran_type, a.sec_type,'long' as trd_type, trd_trader
from br_transaction t, br_portfolio_group p, br_asset a
where t.fund = p.fund
and p.portfolio_group = 'ALL_FUNDS'
and t.cusip = a.cusip
and t.tran_type in ('BUY','SELL','ISSUE','ALLOC')
and t.trd_status <> 'C'
and a.sm_sec_group not in ('CASH','FUTURE','FX','FUND','SWAP','OPTION')
and a.sec_type != 'MBS_TBA'
and t.trd_counterparty not in ('IFUND','GHOST','SPO','SPOBO','CONV','ASSGN')
and t.trd_trade_dt >= #from_dt
and t.trd_trade_dt <= #to_dt
and t.cusip not in (select distinct cusip from br_asset where (isnull(risk_country,country) = 'US'
and sm_sec_type in ('GOVT','TBILL')))
and t.trd_td_par > 0

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);
}

How to filter rows before and after a certain period (date)?

My objective is to select dates before/after a certain period. I have a start period and an end period. I want to filter rows where close_time is included between two periods (and some other filters, like category and origin): start period <= close_time >= end period.
I have tried using:
var StartTime == '2017-03-14'
var EndTime == '2017-03-14'
val df1 = df.withColumn(
"X_Field",
when($"category" === "incident" and $"origin" === "phone" and StartTime <== $"close_time" >== EndTime, 1).otherwise(0)
)
I have errors. What is the right syntax to do this ? Thx !
First - unlike with equality, the right operators to use for greater-or-equal and little-or-equal are <= and >= and not <== and >==.
Second, the expression StartTime <= $"close_time" >= EndTime is not valid - the first part (StartTime <= $"close_time") evaluates into a Boolean condition, which you then try to compare to another String (>= EndTime).
Instead, you can use between:
val df1 = df.withColumn("X_Field", when(
$"category" === "incident" and
$"origin" === "phone" and
($"close_time" between (StartTime, EndTime)), 1).otherwise(0)
)
Which is simply shorthand for:
val df1 = df.withColumn("X_Field", when(
$"category" === "incident" and
$"origin" === "phone" and
($"close_time" >= StartTime and $"close_time" <= EndTime), 1).otherwise(0)
)

How to create an Update statement from a subquery in TSQL

I need to update all records that match my criteria. But the Sql below is giving this error:
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
-- Set MasterAccountId = NULL where there is no Receivable with equivalent BillingAccountId and TaskAccountId
UPDATE R
SET R.MasterAccountId = NULL
FROM Receivable R
WHERE EXISTS ( SELECT * FROM MasterAccount M
WHERE (ISNULL(M.BillingAccountId, 0) > 0 AND M.BillingAccountId = R.BillingAccountId) OR
(ISNULL(M.TaskAccountId, 0) > 0 AND M.TaskAccountId = R.TaskAccountId))
Basically, I need to update all records that return in that subquery.
Does any one know how to fix it?
Can you give a try on this. This is base from the repond of https://stackoverflow.com/users/40655/robin-day on this link How do I UPDATE from a SELECT in SQL Server?.
UPDATE
R
SET
R.MasterAccountId = NULL
FROM
Receivable R
INNER JOIN
MasterAccount M
ON
(ISNULL(M.BillingAccountId, 0) > 0 AND M.BillingAccountId = R.BillingAccountId) OR
(ISNULL(M.TaskAccountId, 0) > 0 AND M.TaskAccountId = R.TaskAccountId))
I don't think you are getting the said error in posted query May be somewhere else. Again in your EXISTS subquery, instead of saying select * ... it's always better to say WHERE EXISTS ( SELECT 1 FROM MasterAccount M
Also try using the JOIN version of this query instead like
UPDATE R
SET R.MasterAccountId = NULL
FROM Receivable R
JOIN MasterAccount M ON M.BillingAccountId = R.BillingAccountId
OR M.TaskAccountId = R.TaskAccountId
WHERE ISNULL(M.BillingAccountId, 0) > 0
OR ISNULL(M.TaskAccountId, 0) > 0;

What's Squeryl syntax for exclusion (i.e. != )?

Doing a simple Squeryl database lookup, but trying to exclude a value. I've tried:
j.id not jobExclude and j.id != jobExclude
however the first triggers a compiler error and the second triggers a runtime error.
The whole transaction:
from(DB.jobs)(j =>
where((j.startTime >= todayStart)
and (j.startTime <= todayEnd)
and (j.userId === userId)
and (j.teamId === teamId)
and (j.startOrder >= index)
and (j.id not jobExclude))
select (j)).toList
Thanks!
Courtesy of the Squeryl Groups:
Not equals is <>
so in the bigger picture:
(job.id <> jobExclude)
See http://squeryl.org/functions.html
Credit https://groups.google.com/forum/?fromgroups#!topic/squeryl/Hw7iVyvLLNM