How to replace value of column in a row with other columns in same row - oracle

How to replace value of column in a row with other columns in same row below are the sample db data and output which is required any help would be appreciated.
Data in DB:
I tried using if else but not able to solve it below is the sample if else code
if ((#adrln1 is null) and (#adrln2 is not null or #adrln3 is not null or #adrstc is not null or #adrpsz is not null or #ctry_name is not null))
{
publish data
where adrnam = #adrnam
and adrln1 = #adrln2
and adrln2 = #adrln3
and adrln3 = #adrstc
and adrstc = #adrpsz
and adrpsz = #ctry_name
and ctry_name = ''
}
Required Result:

Related

Linq request group by

I'd like to group my request by FkOperateur and FkPstStd but I still need information in the select new part. How should I proceed to get IdPoste, FkStandard, IsValidated, Date depending on the group part result
var shortFormations =
(from f in _context.Formations
where f.FkPstStdNavigation.FkPosteNavigation.FkZoneNavigation.FkBatimentNavigation.IdBatiment == Batiment && (f.FkOperateurNavigation.Equipe == Equipe || Equipe == null)
group f by new { f.FkOperateur, f.FkPstStd } into formation
select new shortFormations
{
FkOperateur = formation.Key.FkOperateur,
FkPstStd = formation.Key.FkPstStd,
IdPoste = f.FkPstStdNavigation.FkPosteNavigation.IdPoste,
FkStandard = f.FkPstStdNavigation.FkStandard,
IsValidated = f.IsValidated,
Date = f.DateFormation,
})
.AsNoTracking()
.ToList();
I can't get IdPoste FkStandard IsValidated and Date
return f doesn't exist on current context on these line.
Could you explain me please.
Select
fk_operateur,Fk_Pst_Std, max(date_formation)
FROM
Formations
GROUP by
Fk_Operateur,Fk_Pst_Std
that's a try in SQL but I'm not able to get others column mentionned before
returns
fk_operateur
FkPstStd
date_formation
1
1
Adate
and many other rows
Since its grouped, to access the mentioned fields, you will have to look in the Values and select the correct item. As in, something like
select new shortFormations
{
FkOperateur = formation.Key.FkOperateur,
FkPstStd = formation.Key.FkPstStd,
IdPoste = formation.FirstOrDefault()
.FkPstStdNavigation.FkPosteNavigation.IdPoste,
....
or order it and then select the correct record

Excel PowerQuery: how do I add an IsNotNull column

I have a simple function that I'd like to run on the values in a column, resulting in another column.
let
ThisIsNotNull = (input) => if (input = null) then false else true,
Source = ...
eventually there is a text column with Nulls in it, let's call it TextColumn.
I'd like to add another column alongside it with a value of =ThisIsNotNull(TextColumn).
Add column... Custom column with formula
= ThisIsNotNull([NameOfColumnToTest])
But really you can skip the function and just use
= if [NameOfColumnToTest] = null then false else true

Setting Linq2Db to read and update with CHAR type and NOT NULL constraint in Oracle

Reading value of fixed CHAR string from table, using Linq2db Oracle provider:
CREATE TABLE mytable
(pk NUMBER(15,0) NOT NULL,
fixed_data CHAR(20) DEFAULT ' ' NOT NULL)
Although in database, length of FIXED_DATA filed is 20,
SELECT LENGTH(fixed_data) FROM mytable WHERE pk = 1
-- result is 20
When same field is read using Linq2Db, value gets truncated to empty string:
var row = (from row in database.mytable where row.pk == 1 select row).ToList()[0];
Console.WriteLine(row.fixed_data.Length);
// result is zero
This causes problem when record is updated using Linq2Db, Oracle converts empty string to NULL, and UPDATE fails:
database.Update(row);
// Oracle.ManagedDataAccess.Client.OracleException: 'ORA-01407: cannot update ("MYSCHEMA"."MYTABLE"."FIXED_DATA") to NULL
Is there any setting in Linq2Db for read->update cycle to work with CHAR type and NOT NULL constraint?
Found a solution, thanks to source code openly available. By default, Linq2Db calls expression IDataReader.GetString(int).TrimEnd(' ') on every CHAR and NCHAR column. However this can be easily customized, by implementing custom provider, which overrides field value retrieval expression, with one that does not trim:
class MyOracleProvider : OracleDataProvider
{
public MyOracleProvider(string name)
: base(name)
{
// original is SetCharField("Char", (r,i) => r.GetString(i).TrimEnd(' '));
SetCharField("Char", (r, i) => r.GetString(i));
// original is SetCharField("NChar", (r,i) => r.GetString(i).TrimEnd(' '));
SetCharField("NChar", (r, i) => r.GetString(i));
}
}

How to create a temporary column + when + order by with Criteria Builder

here is the sql statement I am trying to translate in jpa :
select
id,
act_invalidation_id,
last_modification_date,
title,
case when act_invalidation_id is null then 1 else 0 end as test
from act order by test, last_modification_date desc
The actual translation
Root<Act> act = query.from(Act.class);
builder.selectCase()
.when(builder.isNull(actRoot.get("actInvalidation")), 1)
.otherwise(0).as(Integer.class);
Expression<?> actInvalidationPath = actRoot.get("actInvalidation");
Order byInvalidationOrder = builder.asc(actInvalidationPath);
Path<Date> publicationDate = actRoot.get("metadata").get("publicationDate");
Order byLastModificationDate = builder.desc(publicationDate);
query.select(act).orderBy(byInvalidationOrder, byLastModificationDate);
entityManager.createQuery(query).getResultList();
I try to create a temporary column (named test) of Integer type and orderby this column, then orderby lastmodificationdate. The content of this new column is determined by the value of actInvalidation field.
In short: How to create a temp column with integer values, then order by this temp column in jpa ?
Thank you
I didn't test this but it should work like this:
Root<Act> act = query.from(Act.class);
Expression<?> test = builder.selectCase()
.when(builder.isNull(actRoot.get("actInvalidation")), 1)
.otherwise(0).as(Integer.class);
Expression<?> actInvalidationPath = actRoot.get("actInvalidation");
Order byInvalidationOrder = builder.asc(actInvalidationPath);
Path<Date> publicationDate = actRoot.get("metadata").get("publicationDate");
Order byLastModificationDate = builder.desc(publicationDate);
Order byTest = builder.asc(test);
query.select(act).orderBy(byTest, byInvalidationOrder, byLastModificationDate);
entityManager.createQuery(query).getResultList();

Update row in database using LINQ - object copied

var queryResult = this.kindergardenDataContext.Groups.Where(g => g.group_id == groupToAdd.group_id && g.group_enabled==true);
if (queryResult != null && queryResult.Count() != 0)
{
Group groupToEdit = queryResult.First();
groupToEdit.group_name = groupToAdd.group_name;
groupToEdit.group_create_date = groupToAdd.group_create_date;
groupToEdit.Kindergarden = groupToAdd.Kindergarden;
groupToEdit.GroupGuardian = groupToAdd.GroupGuardian;
groupToEdit.Room = groupToAdd.Room;
}
this.kindergardenDataContext.SubmitChanges();
Hi. Im beginner in LINQ. Here is ok, but foreigns key kindergarden, groupguardian, room has been duplicated in database (added new rows in database in kindergarden, groupguardian, room table). How can I set good reference to update row in group table.
Unless you retrieved groupToAdd.Kindergarden, groupToAdd.GroupGuardian, andgroupToAdd.Roomfromthis.kindergardenDataContext` or attached them with the right IDs set, that is the expected behviour. It doesn't know that you mean the same object instances that already resides in the database.

Resources