nhibernate hql subquery : convert null to zero - sql

I am trying to update sum of particular amount from one table as a value in another table, but getting problem when the particular id does not exist in another table :
the hql i have looks like beow:
var session = SessionManager.GetCurrentSession();
var queryString = string.Empty;
queryString += #"update CLiner cl set cl.UnbilledDue =
(select sum(cu.UnbilledAmount) as UnbilledAmount from CUnbilled cu
where cu.AccountNo = cl.AccountNo and cu.FileDate = :fileDate)
where cl.FileDate = :fileDate ";
var query = session.CreateQuery(queryString);
query.SetDateTime("fileDate", new DateTime(2014, 10, 7));
query.ExecuteUpdate();
but its giving exception when the particular account no does not exist in child table and the sum is returned as zero.
i tried changing the subquery select to
select isnull(sum(cu.UnbilledAmount),0) as UnbilledAmount
which as expected is not supported by nhibernate hql. so there a way i can convert null to zero in hql select statement...

Try this:
coalesce(sum(cu.UnbilledAccount), 0)
Reason:
Normally when you wirte a SQL query to prevent NULL you must apply an ISNULL function over the SELECT, in this way:
ISNULL(SELECT sum(...., 0) or COALESCE(SELECT sum(...., 0) (return the same result but COALESCE can applied with more parameter, ISNULL has only two parameters)
but in HQL isn't possible, so you can use COALESCE function to prevent that behaviour.

Related

How to use NVL or COALESCE in native query for list of values in Spring Data JPA

I am using native query as follows which fetches data from multiple tables.
I also want to perform filtering on it based on users input. User can select either none, one or multiple checkboxes in filter menu based on which my query should return the record.
I have tried both the approaches like:
src_region.region_id NVL(:regionIds,src_region.region_id )
src_region.region_id in(:regionIds) OR COALESCE (:regionIds)IS NULL)
But for both of them I am getting errors as I am sending List values.
public interface dbRepository
{
public static final String dbQuery = "select * from
(select src_data.*,
trg_data.*
from
(select reg.region_id,reg.region_name,
ctry.country_id,ctry.country_name
from src_region reg,src_ctry ctry
where reg.region_id=ctry.region_id
AND src_region.region_id in(:regionIds) OR COALESCE (:regionIds)IS NULL) src_data,
(select md.module_id,reg.module_name,
ctry.country_id,ctry.country_name
from trg_module md,trg_ctry ctry
where md.module_id=ctry.country_id
AND ctry.country_id in(:countryIds) OR COALESCE (:countryIds)IS NULL) trg_data
ORDER BY src_data.region_id ";
#Query( value = dbQuery, nativeQuery = true )
List<Object[]> findBySorceOrTarget( #Param( "regionIds" ) List<Long> regionIds, #Param( "countryIds" ) List<Long> countryIds );
}
User may on may not send values. Also, rather than sending multiple values, they can also send only one value. What is the better way to pass list of values to parameters in native query?
Generally speaking, that should be
AND (src_region.region_id = :regionIds or :regionIds IS NULL)
However, if you can select multiple values, then = won't be OK - you'll have to switch to in (as code you posted suggests). Though, you can't do it that way as you'll get nothing as a result - you'll have to split those values into rows.
Something like this (presuming that values in :regionIds are comma-separated, such as 1,2,5:
and (src_region.region_id in (select regexp_substr(:regionIds, '[^,]+', 1, level)
from dual
connect by level <= regexp_count(:regionIds, ',') + 1
)
or :regionIds is null)

PostgreSQL conditional where clause

In my Ruby on Rails app I'm using blazer(https://github.com/ankane/blazer) and I have the following sql query:
SELECT *
FROM survey_results sr
LEFT JOIN clients c ON c.id = sr.client_id
WHERE sr.client_id = {client_id}
This query works really well. But I need to add conditional logic to check if client_id variable is present. If yes then I filter by this variable, if not then I not launching this where clause. How can I do it in PostgreSQL?
Check if its null OR your condition like this:
WHERE {client_id} IS NULL OR sr.client_id = {client_id}
The WHERE clause evaluate as follow: If the variable is empty, then the WHERE clause evaluate to true, and therefore - no filter. If not, it continue to the next condition of the OR
If anyone faced with the psql operator does not exist: bigint = bytea issue, here is my workaround:
WHERE ({client_id} < 0 AND sr.client_id > {client_id}) OR sr.client_id = {client_id}
Please consider that, client_id generally cannot be negative so you can use that information for eleminating the operation cast issue.
My solution:
I use spring data jpa, native query.
Here is my repository interface signature.
#Query(... where (case when 0 in :itemIds then true else i.id in :itemIds end) ...)
List<Item> getItems(#Param("itemIds) List<Long> itemIds)
Prior calling this method, I check if itemIds is null. If yes, I set value to 0L:
if(itemIds == null) {
itemIds = new ArrayList<Long>();
itemIds.add(0L);
}
itemRepo.getItems(itemIds);
My IDs starts from 1 so there is no case when ID = 0.

Linq Query in VB

Good Day,
I am querying my database using Linq and I have run into a problem, the query searched a column for a search phrase and based on if the column has the phrase, it then returns the results, The query is below,
Dim pdb = New ProductDataContext()
Dim query =
From a In pdb.tblUSSeries
Join b In pdb.tblSizes_ On a.Series Equals b.Series
Where
a.Series.ToString().Equals(searchString) Or
b.Description.Contains(searchString) Or Not b.Description.Contains(Nothing)
Order By b.Series, b.OrderCode Ascending
Select New CustomSearch With
{
.Series = a.Series,
.SeriesDescription= a.Description,
.Coolant = a.Coolant,
.Material = a.Material,
.Standard = a.Standard,
.Surface = a.Surface,
.Type = a.Type,
.PointAngle = a.PointAngle,
.DiaRange = a.DiaRange,
.Shank = b.Shank,
.Flutes = b.Flutes,
.EDPNum = b.EDPNum,
.SizesDescription = b.Description,
.OrderCode = b.OrderCode
}
Return query
I think the problem is that, in the table certain rows are NULL, so when it is checking the column for the phrase and it encounters a row that is null it, breaks and returns this error,
The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
I have ran this query against another column that has all the rows populated with data and it returns the results ok.
So my question is how can I write it in VB to query the db with the supplied searchstring and return the results, when some of the rows in the columns have null values.
Any help would be great.
The exception occurs when you make the projection (i.e. select new CustomSearch)
And yes your trying to assign Null to some int property
(Not sure which one of your properties that is)
one of 2 choices :
1) Use nullalbe types for your properties (or just that one property).
2) project with an inline If ( ?? in C#) , I don't know VB so don't catch me on the syntax.
Taking Series just as an example i don't know if it's an int or if that's the problematic property
Select New CustomSearch With
{
.Series = If(a.Series Is Nothing,0, CInt(a.Series))
}
In C#
Select new CustomSearch
{
Series = a.Series ?? 0;
}

Nhibernate and like statement on XML field

I have a wrapped fluent nhibernate framework that I'm reusing and have no control over the actual mapping.
In my entity object I have a property mapped as string to an XML column in sql.
Hence when I run a query like:
var myResult = (from myTable in DataManager.Session.Query<Table>()
where myTable.thatXmlFieldWhichIsMappedAsString.Contains(AnXmlSnippet))
select myTable).FirstOrDefault();
It is trying to use the LIKE operator in SQL which is invalid on that column type.
How can I get around this without having to select all the rows and converting to List first?
In case, that we do not need .Query() (LINQ), and we can use Criteria query or QueryOver, we can use conversion:
// the projection of the column with xml
// casted to nvarchar
var projection = Projections
.Cast(NHibernateUtil.StringClob
, Projections.Property("thatXmlFieldWhichIsMappedAsString"));
// criteria filtering with LIKE
var criteria = Restrictions.Like(projection, "searched xml");
// query and result
var query = session.QueryOver<MyEntity>()
.Where(criteria)
;
var result = query
.SingleOrDefault<MyEntity>()
;
From my experience this could lead to conversion into small nvarchar(255) - sql server... Then we can do it like this:
var projection = Projections
.SqlProjection("CAST(thatXmlFieldWhichIsMappedAsString as nvarchar(max)) AS str"
, new string[]{}
, new NHibernate.Type.IType[]{}
);

sql select with one value of two where

This is the only place that I get all answer ;)
I want to select :
SELECT
RTRIM(LTRIM(il.Num_bloc)) AS Bloc,
RTRIM(LTRIM(il.num_colis)) AS Colis,
cd.transporteur AS Coursier,
cd.origine AS Origine,
cd.destination AS Destinataire,
cd.adresse AS [Adresse Destinataire],
cd.poids AS Poids,
il.Signataire, il.num_cin AS CIN, il.date_livraison AS [Date Livraison]
FROM
dbo.cd
INNER JOIN
dbo.il ON cd.bloc = il.Num_bloc AND dbo.cd.colis = dbo.il.num_colis
WHERE
(il.Num_bloc = RTRIM(LTRIM(#ParamBloc)))
AND (il.num_colis = RTRIM(LTRIM(#ParamColis)))
In the way of getting result if the user put ether #ParamBloc or #ParamColis
Try using IsNull() function.
A simple query would go like this
Select * from yourTable
Where
Num_bloc = ISNULL(#ParamBloc, Num_block) AND
num_colis = ISNULL(#ParamColis, num_colis)
The second parameter would make the expression to true if the #parameter Bloc or Colis is null. This query would be useful for all 4 possible combination of these two parameter.