with the following code I get an error in my sql statement:
Expression<Number> difference = queryBuilder.diff(A, B);
Predicate differenceGEtoZero = queryBuilder.ge(difference, new Long(0));
CriteriaBuilder.Case<Number> when = queryBuilder.<Number>selectCase().when(
differenceGEtoZero, difference
);
queryDefinition.select(
queryBuilder.construct(
State.class,
root.get("object"),
queryBuilder.sum(
when.otherwise(new Long(0))
)
)
);
I get this sql statement containing an error in CASE'S THEN part:
SUM(
CASE WHEN ((t1.A - t1.B) >= 0)
THEN ((t1.A - t1.B) >= 0) //'>= 0' should not appear here!!!
ELSE 0
END )
I expected only the difference appearing ((t1.A - t1.B), not again a condition >=0.
I am using this version of the libs: org.apache.openejb:javaee-api:6.0-5 and eclipseLink 2.3.2 as provider
Could you please tell me what's wrong in the code?
Thank you in advance.
Nic
Seems a bug solved by following versions of eclipseLink.
With 2.5.1 everything is working fine.
Related
I have a task to finish in SQL, and am not very familiar with the language. The normal resource I would use is sick currently, so I'm asking for help here instead. I'm running the following query on Sybase:
SELECT WgArt, SrtNr, Datum, WNetto
FROM M01.Wgs
WHERE (WgArt <> 'f') AND (SrtNr = '170904-01') AND
(Datum BETWEEN to_date('09.02.2017','dd.mm.yyyy') AND
to_date('09.02.2017','dd.mm.yyyy')
) OR
(SrtNr = '170904-02') OR
(SrtNr = '170904-05') OR
(SrtNr = '170904-07') OR
(SrtNr = '150106-03')
The error message I'm receiving is: "Procedure 'to_date' is not found".
Error msg 42S02
I googled and found some workaround articles with CAST/Convert, but also getting same error. Any idea would be welcome.
I would use IN clause rather that strange OR clause :
SELECT WgArt, SrtNr, Datum, WNetto
FROM M01.Wgs
WHERE (WgArt <> 'f') AND
(Datum BETWEEN to_date('09.02.2017','dd.mm.yyyy') AND
to_date('09.02.2017','dd.mm.yyyy')
) AND (SrtNr IN ('170904-01', '170904-02', '170904-05', '170904-07', '150106-03')
);
Assuming your product is Sybase (now SAP) SQLAnywhere, there is no to_date() function.
Here's a list of date-related functions supported by SQLAnywhere.
Of specific interest might be the date(), datetime() and/or dateformat() functions ... ?
I need to pass a parameter into an HQL at execution time. For this we have updated the .param file with the required value. Now I am facing issue in using it.
SELECT * FROM temp_table A
WHERE PRODT_CTGRY_CD = 'CAR' AND
(
(DATE(A.CUST_BRTH_DT) BETWEEN concat(${hiveconf:PRODYEAR} - 13 ,"-02","-28") and concat(${hiveconf:PRODYEAR} - 7 ,"-03","-01")
AND DATE(A.TERM_DT) >= concat(${hiveconf:PRODYEAR} - 1 ,"-03","-31")
)
In the above query if I don't include the condition of term_dt the query is running fine. However when I include that I am getting an error as below:
FAILED: ClassCastException org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableIntObjectInspector cannot be cast to org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector (state=42000,code=40000)
Can someone let me know what needs to be done here?
I have wrote a query so I can view people who are overdue an order based on their average order dates. The query is to be ran on a PostgreSQL database, and will be executed from a Java process.
However in the line :
CASE WHEN
(max(date_trunc('day', dateordered))-
min(date_trunc('day', dateordered)) ) /
count(distinct dateordered) + 5 <
date_trunc('day',now()) -
max(date_trunc('day', dateordered)) THEN 'ORDEROVERDUE' ELSE
null
END
I receive the error message :
Operator does not exist : integer < interval
I have read a lot of questions which have a similar issue, but none which seem to fix my particular issue.
If I alter my query to this :
CASE WHEN
(max(dateordered::date) - min(dateordered::date) )/
count(distinct dateordered) + 5 <
now()::date - max(dateordered::date) THEN
'ORDEROVERDUE' ELSE null
END
Then it runs on the database, however I can't get this syntax to work in my process in eclipse.
My understanding of SQL is letting me down. I understand the general reason behind the error, but I am unable to create a solution.
Is there a way of altering this line in a way which removes the error and I can still get the desired result?
I am using informix and I am unable to use a CASE statement inside an aggregate function. It always throws an error (-201: A syntax error has occurred). The following is the query I am using.
select nvl(count(case when (ccd.contacttype =1 and ccd.contactdisposition =2) then (ccd.sessionid) else 0 end),'ELSED (att)') as calls
from contactcalldetail ccd
inner join agentconnectiondetail acd on ccd.sessionid=acd.sessionid
Can you explain what's wrong?
Informix 11.70 allows CASE inside an aggregate
The following SQL works for me in Informix 11.70.FC6 on Mac OS X 10.7.5 (also 11.70.FC4 on RHEL 5):
CREATE TEMP TABLE contactcalldetail
(
contacttype INTEGER NOT NULL,
contactdisposition INTEGER NOT NULL,
sessionid INTEGER NOT NULL
);
CREATE TEMP TABLE agentconnectiondetail
(
sessionid INTEGER NOT NULL
);
SELECT NVL(COUNT(CASE
WHEN (ccd.contacttype = 1 AND ccd.contactdisposition = 2)
THEN (ccd.sessionid)
ELSE 0 END),
'ELSED (att)') AS calls
FROM contactcalldetail ccd
JOIN agentconnectiondetail acd ON ccd.sessionid = acd.sessionid;
Informix 11.50 or older does not
Since it generates a -201 syntax error for you, we can infer that you are using an older version of Informix. It probably means you need to upgrade to a newer version of Informix. (Based on the information from Copilot's comments, it appears the Informix 11.50 did not support the notation; only 11.70 does.)
If you are using Informix 11.70, you need to document exactly which version you are using, and the platform on which you are running it. If it is 11.70.xC[4-7], then we may have a bug to chase; if it is earlier, then the support may have been added since the version you are using was released. Studying the release notes might help understand this. I've not checked when it was first available.
Does COUNT ever return NULL?
I observe that I don't think there are any circumstances when COUNT returns NULL. Certainly, with the empty tables, the output of the following variant of the query above returns zeros for all the values. Consequently, I think the NVL function call is unnecessary.
SELECT NVL(COUNT(CASE
WHEN (ccd.contacttype = 1 AND ccd.contactdisposition = 2)
THEN (ccd.sessionid)
ELSE 0 END),
'ELSED (att)') AS calls,
COUNT(*) AS count1,
COUNT(CASE
WHEN (ccd.contacttype = 1 AND ccd.contactdisposition = 2)
THEN (ccd.sessionid)
ELSE 0 END) AS count2
FROM contactcalldetail ccd
JOIN agentconnectiondetail acd ON ccd.sessionid = acd.sessionid;
Seems like an IDS bug to me.
I tried similar queries (a case nested in a count function) and this always gives a syntax error on the 'when' keyword.
However, according to the official documentation, this should work.
I have a Linq to SQL query that was working just fine with SQL Server 2005 but, I have to deploy the web app with a SQL Server 2000 and, when executing that query, I get his error:
"System.Data.SqlClient.SqlException: The column prefix 't0' does not match with a table name or alias name used in the query."
I have more queries but it doesn't seems to have problems with those.
Now, this is the query:
from warningNotices in DBContext_Analyze.FARs
where warningNotices.FAR_Area_ID == filter.WarningAreaID &&
warningNotices.FAR_Seq == filter.WarningSeq &&
warningNotices.FAR_Year == filter.WarningYear
orderby warningNotices.FAR_Seq ascending
select new Search_Result
{
FAR_Area_ID = warningNotices.FAR_Area_ID,
FAR_Seq = warningNotices.FAR_Seq,
FAR_Year = warningNotices.FAR_Year,
DateTime_Entered = (DateTime)warningNotices.DateTime_Entered == null ? DateTime.MaxValue : (DateTime)warningNotices.DateTime_Entered,
Time_Entered = warningNotices.Time_Entered,
OrigDept = warningNotices.OrigDept,
Part_No = warningNotices.Part_No,
DateTime_Analyzed = (DateTime)warningNotices.DateTime_Analyzed == null ? DateTime.MaxValue : (DateTime)warningNotices.DateTime_Analyzed,
Analyzed_By = warningNotices.Analyzed_By,
MDR_Required = (Char)warningNotices.MDR_Required == null ? Char.MinValue : (Char)warningNotices.MDR_Required,
Resp_Dept = (from FARSympt in DBContext_Analyze.FAR_Symptoms
where FARSympt.FAR_Area_ID == warningNotices.FAR_Area_ID &&
FARSympt.FAR_Year == warningNotices.FAR_Year &&
FARSympt.FAR_Seq == warningNotices.FAR_Seq
select new { FARSympt.Resp_Dept}).FirstOrDefault().Resp_Dept,
Sympt_Desc = (from SymptomsCatalog in DBContext_Analyze.Symptoms
where SymptomsCatalog.symptom_ID == filter.Status_ID
select new {
SymptomsCatalog.Sympt_Desc
}).FirstOrDefault().Sympt_Desc,
Status_ID = warningNotices.Status.HasValue ? warningNotices.Status.Value : 0
};
Previously I had a "Distinc" in the subquery for the Resp_Dept field, but I removed it.
Any ideas? Thanks in advance for your comments =)
This is query I get from the SQL Server profiler:
exec sp_executesql N'SELECT [t0].[FAR_Seq], [t0].[FAR_Year],
(CASE
WHEN ([t0].[DateTime_Entered]) IS NULL THEN #p3
ELSE [t0].[DateTime_Entered]
END) AS [DateTime_Entered], [t0].[Time_Entered], [t0].[OrigDept], [t0].[Part_No],
(CASE
WHEN ([t0].[DateTime_Analyzed]) IS NULL THEN #p4
ELSE [t0].[DateTime_Analyzed]
END) AS [DateTime_Analyzed], [t0].[Analyzed_By],
(CASE
WHEN (UNICODE([t0].[MDR_Required])) IS NULL THEN #p5
ELSE CONVERT(NChar(1),[t0].[MDR_Required])
END) AS [MDR_Required], (
SELECT [t2].[Resp_Dept]
FROM (
**SELECT TOP (1)** [t1].[Resp_Dept]
FROM [dbo].[FAR_Symptoms] AS [t1]
WHERE (UNICODE([t1].[FAR_Area_ID]) = UNICODE([t0].[FAR_Area_ID])) AND ([t1].[FAR_Year] = [t0].[FAR_Year]) AND ([t1].[FAR_Seq]
= [t0].[FAR_Seq])
) AS [t2]
) AS [Resp_Dept], (
SELECT [t4].[Sympt_Desc]
FROM (
**SELECT TOP (1)** [t3].[Sympt_Desc]
FROM [dbo].[Symptoms] AS [t3]
WHERE [t3].[symptom_ID] = #p6
) AS [t4]
) AS [Sympt_Desc], [t0].[FAR_Area_ID],
(CASE
WHEN [t0].[Status] IS NOT NULL THEN [t0].[Status]
ELSE #p7
END) AS [Status_ID]
FROM [dbo].[FARs] AS [t0]
WHERE (UNICODE([t0].[FAR_Area_ID]) = #p0) AND ([t0].[FAR_Seq] = #p1) AND ([t0].[FAR_Year] = #p2)
ORDER BY [t0].[FAR_Seq]',N'#p0 int,#p1 int,#p2 varchar(2),#p3 datetime,#p4 datetime,#p5 nchar(1),#p6 int,#p7
int',#p0=76,#p1=7204,#p2='08',#p3=''9999-12-31 23:59:59:997'',#p4=''9999-12-31 23:59:59:997'',#p5=N' ',#p6=0,#p7=0
The only think that I see there that may not in SQL Server 2000 is the '()' in the "Select top..." but I'm not sure if that is what is causing the problem and, also, I don't know how that could be fixed =S
Thanks again =)
My Linq statement worked on SQL2008 but broke with the exact same error message on SQL2000.
Had a very similar Linq query that worked on both, the only real difference was that before calling .ToList() I called the .OrderBy() clause.
Ex:
var query = from t1 in table1 ...
...;
list = query.OrderBy(o => o.Field).ToList()
Tried the same OrderBy clause on the broken Linq query and it worked!
Has to be a bug?
Do you have the latest Service Pack for Visual Studio and the framework?
I just checked some of my Linq generated SQL and it is using "Top 1" correctly against a SQL Server 2000 database.
after several testing and review the DB, I found that the problem was a legacy table I was working on: that table has "text" type fields. Also, I had to remove some "Distinct" instructions in a nested query I had.
I found this and, after review that, I found that I have to change my queries and that the "Distinct" instruction does not work correctly. As a side note, let me say that the nested queries can also generate unexpected behavior.
So, the real lesson here is that if you need to deploy this against a SQL Server 2000, set an instance of the server and test against it!!! XD
Thanks a lot of your help =)