JPA Query with Case condition - sql

(Case a.statusCd When 'ln' then 'loan' else a.status_cd end) as statusCd
Why this jpa query returns an error?
The whole query string is
String qryString = "select"
+ " a.biblioId, " // 0
+ " a.copyId, " // 1
+ " a.copyDescr, "// 2
+ " a.employeeId, "// 3
+ " (Case a.statusCd When 'ln' then 'loan' else a.status_cd end) as statusCd, "// 4
+ " a.statusBeginDt, "// 5
+ " a.libListingCopyId, "// 6
+ " a.barcodeNbr, " // 7
+ " b.title, " // 8
+ " b.author " // 9
+ " from LibListingCopy a, LibListing b " + " where "
+ curStmt + "and " + authStmt
+ "and a.biblioId = b.biblioId and a.siteId=b.siteId and "
+ bibStmt + " and " + cpyStmt + " and " + libCodeStmt
+ " and " + accnNbrStmt;

Please check your query, there are two seperate fieldnames for the same filed i think:
(Case a.statusCd When 'ln' then 'loan' else a.status_cd end) as statusCd
should be
(Case a.statusCd When 'ln' then 'loan' else a.statusCd end) as statusCd
or (for the other possibility)
(Case a.status_cd When 'ln' then 'loan' else a.status_cd end) as statusCd
So the failure may be the spelling of your field a.status_cd / a.statusCd

Related

subquery in FROM must have an alias for sql query

I have this code snippet for a query below.
"SELECT DISTINCT(CODE) FROM (" +
"SELECT TRIM(BOTH ' ' FROM PROJECT_NUMBER) CODE FROM SCHEMA.TABLE_NAME " +
"WHERE PROJECT_NUMBER IS NOT NULL " +
"AND LAST_UPDATE_DATE >= :lastUpdateDate " +
"UNION " +
"SELECT TRIM(BOTH ' ' FROM ANOTHER_CODE) CODE FROM SCHEMA.TABLE_NAME " +
"WHERE ANOTHER_CODE IS NOT NULL " +
"AND LAST_UPDATE_DATE >= :lastUpdateDate " +
") " +
"WHERE CODE IS NOT NULL";
I'm in the process of migrating to postgres from oracle and I'm seeing these errors in our logs:
Caused by: org.postgresql.util.PSQLException: ERROR: subquery in FROM must have an alias
Hint: For example, FROM (SELECT ...) [AS] foo.
Where should the alias go, do I need for all subqueries, and do I need to use the alias in this query? Little confused here.
"SELECT DISTINCT(CODE) FROM (" +
"SELECT TRIM(BOTH ' ' FROM PROJECT_NUMBER) CODE FROM SCHEMA.TABLE_NAME " +
"WHERE PROJECT_NUMBER IS NOT NULL " +
"AND LAST_UPDATE_DATE >= :lastUpdateDate " +
"UNION " +
"SELECT TRIM(BOTH ' ' FROM ANOTHER_CODE) CODE FROM SCHEMA.TABLE_NAME " +
"WHERE ANOTHER_CODE IS NOT NULL " +
"AND LAST_UPDATE_DATE >= :lastUpdateDate " +
") as qry " +
"WHERE CODE IS NOT NULL";

Unknown Sql Exception (Number = 102, Message=Incorrect syntax near ','.)

I got this syntax error near ','
I already tried some solutions as this question is already asked in this forum before, but to no avail.
string sqlSelect = "SELECT"
+ " 'RI' AS DocType, DebtorCode, DocNo, DocDate, [Description]"
+ ",SalesAgent, LocalNetTotal AS LocalAmount,"
+ "(Outstanding * CurrencyRate) AS LocalBalance"
+ " FROM ARINVOICE"
+ " WHERE Cancelled = 'F'"
+ " UNION"
+ " SELECT 'RD' AS DocType, DebtorCode, DocNo, DocDate, [Description]"
+ ",SalesAgent, LocalNetTotal AS LocalAmount"
+ ",Outstanding AS LocalBalance"
+ " FROM ARDN"
+ " WHERE Cancelled = 'F'"
+ " UNION"
+ " SELECT 'RC' AS DocType, DebtorCode, DocNo, DocDate, [Description]"
+ ",'' AS SalesAgent, LocalNetTotal AS LocalAmount"
+ ",(NetTotal - KnockOffAmt + RefundAmt) * CurrencyRate AS LocalBalance"
+ " FROM ARCN"
+ " WHERE Cancelled = 'F'"
+ " UNION"
+ " SELECT 'RP' AS 'DocType', 'DebtorCode', 'DocNo', 'DocDate',
[Description]"
+ ",'' AS SalesAgent, LocalPaymentAmt AS LocalAmount"
+ ",LocalUnappliedAmount AS LocalBalance"
+ " FROM ARPAYMENT"
+ " WHERE Cancelled = 'F'";
Is their anything wrong with my query? Because i already check it for 2 days and could not find any mistakes, not that i known.
Hope anyone could help.
have u tried to unquote the DocType ?
string sqlSelect = "SELECT"
+ " 'RI' AS DocType, DebtorCode, DocNo, DocDate, [Description]"
+ ",SalesAgent, LocalNetTotal AS LocalAmount,"
+ "(Outstanding * CurrencyRate) AS LocalBalance"
+ " FROM ARINVOICE"
+ " WHERE Cancelled = 'F'"
+ " UNION"
+ " SELECT 'RD' AS DocType, DebtorCode, DocNo, DocDate, [Description]"
+ ",SalesAgent, LocalNetTotal AS LocalAmount"
+ ",Outstanding AS LocalBalance"
+ " FROM ARDN"
+ " WHERE Cancelled = 'F'"
+ " UNION"
+ " SELECT 'RC' AS DocType, DebtorCode, DocNo, DocDate, [Description]"
+ ",'' AS SalesAgent, LocalNetTotal AS LocalAmount"
+ ",(NetTotal - KnockOffAmt + RefundAmt) * CurrencyRate AS LocalBalance"
+ " FROM ARCN"
+ " WHERE Cancelled = 'F'"
+ " UNION"
+ " SELECT 'RP' AS DocType, DebtorCode, DocNo, DocDate,
[Description]"
+ ",'' AS SalesAgent, LocalPaymentAmt AS LocalAmount"
+ ",LocalUnappliedAmount AS LocalBalance"
+ " FROM ARPAYMENT"
+ " WHERE Cancelled = 'F'";
In SQL SERVER, check below script-
declare #sqlSelect nvarchar(max)
select #sqlSelect = N'
SELECT ''RI'' AS DocType,DebtorCode,DocNo,DocDate,[Description],SalesAgent,LocalNetTotal AS LocalAmount,
(Outstanding * CurrencyRate) AS LocalBalance
FROM ARINVOICE WHERE Cancelled = ''F''
UNION
SELECT ''RD'' AS DocType,DebtorCode,DocNo,DocDate,[Description],SalesAgent,LocalNetTotal AS LocalAmount,Outstanding AS LocalBalance
FROM ARDN WHERE Cancelled = ''F''
UNION
SELECT ''RC'' AS DocType,DebtorCode,DocNo,DocDate,[Description],'''' AS SalesAgent,LocalNetTotal AS LocalAmount,
((NetTotal - KnockOffAmt + RefundAmt) * CurrencyRate) AS LocalBalance
FROM ARCN WHERE Cancelled = ''F''
UNION
SELECT ''RP'' AS DocType,DebtorCode,DocNo,DocDate,[Description],'''' AS SalesAgent,LocalPaymentAmt AS LocalAmount,
LocalUnappliedAmount AS LocalBalance
FROM ARPAYMENT WHERE Cancelled = ''F'' '
print (#sqlSelect)
--exec (#sqlSelect)

SQL cmd.command include an OR statement

Below is my SQL cmd.CommandText query:
cmd.CommandText = "SELECT CONVERT(date, [DateTime]) 'Date of Download', ActionBy AS 'User Full Name', COUNT(DISTINCT CAST(Identifier AS NVARCHAR(10)) + Remarks + Link) 'Number of Document Downloads' " +
" FROM [dbo].[AuditLog] "+
" WHERE ActionTaken = 'Clicked' "+
" and Type = 'Attachment Link'" +
" and CONVERT(date, [DateTime]) BETWEEN CONVERT(date,'" + scanStartDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') and CONVERT(date,'" + scanEndDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') "+
" GROUP BY CONVERT(date, [DateTime]), ActionBy "+
" HAVING COUNT(DISTINCT Identifier) > " + limit +
" ORDER BY COUNT(DISTINCT Identifier) DESC";
I would like to include an extra parameter OR for Type, like as follows:
" and Type = 'Attachment Link' OR 'Attachment Link - Search'" +
May I know how can it be done?
Thank you.
Edit: Apologies, did not add in the error message, as follow.
An expression of non-boolean type specified in a context where a condition is expected, near 'and'.
specify the column after your OR operator
cmd.CommandText = "SELECT CONVERT(date, [DateTime]) 'Date of Download', ActionBy AS 'User Full Name', COUNT(DISTINCT CAST(Identifier AS NVARCHAR(10)) + Remarks + Link) 'Number of Document Downloads' " +
" FROM [dbo].[AuditLog] "+
" WHERE ActionTaken = 'Clicked' "+
" and (Type = 'Attachment Link' OR Type = 'Attachment Link - Search') " +
" and CONVERT(date, [DateTime]) BETWEEN CONVERT(date,'" + scanStartDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') and CONVERT(date,'" + scanEndDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') "+
" GROUP BY CONVERT(date, [DateTime]), ActionBy "+
" HAVING COUNT(DISTINCT Identifier) > " + limit +
" ORDER BY COUNT(DISTINCT Identifier) DESC";
I assume I am over-simplifying but do you mean:
cmd.CommandText = "SELECT CONVERT(date, [DateTime]) 'Date of Download', ActionBy AS 'User Full Name', COUNT(DISTINCT CAST(Identifier AS NVARCHAR(10)) + Remarks + Link) 'Number of Document Downloads' " +
" FROM [dbo].[AuditLog] "+
" WHERE ActionTaken = 'Clicked' "+
" and (Type = 'Attachment Link' OR Type = 'Attachment Link - Search') " +
" and CONVERT(date, [DateTime]) BETWEEN CONVERT(date,'" + scanStartDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') and CONVERT(date,'" + scanEndDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') "+
" GROUP BY CONVERT(date, [DateTime]), ActionBy "+
" HAVING COUNT(DISTINCT Identifier) > " + limit +
" ORDER BY COUNT(DISTINCT Identifier) DESC";
You might need ( ) for order of operations and I think you mean "=" instead of "-".

Limit return data for 7 days

I have this table which I use to store events
CREATE TABLE EVENT(
ID INTEGER NOT NULL,
SOURCE VARCHAR2(50 ),
TYPE VARCHAR2(50 ),
EVENT_DATE DATE,
DESCRIPTION VARCHAR2(100 )
)
/
I use this query to get the results for all types of events
dbRequest.setPreparedStatement("SELECT "
+ " COUNT(*) \"ALL\","
+ " COUNT(CASE WHEN TYPE = 'Critical' THEN 1 END) \"CRITICAL\", "
+ " COUNT(CASE WHEN TYPE = 'Info' THEN 1 END) \"INFO\", "
+ " COUNT(CASE WHEN TYPE = 'Warning' THEN 1 END) \"WARN\", "
+ " COUNT(CASE WHEN TYPE = 'Error' THEN 1 END) \"ERROR\" "
+ " FROM "
+ " EVENT");
How I can modify the query to get the rows only for last 7 days?
Also for some reason the query is not working with low capital data.
dbRequest.setPreparedStatement("SELECT "
+ " COUNT(*) \"ALL\","
+ " COUNT(CASE WHEN TYPE = 'Critical' THEN 1 END) \"CRITICAL\", "
+ " COUNT(CASE WHEN TYPE = 'Info' THEN 1 END) \"INFO\", "
+ " COUNT(CASE WHEN TYPE = 'Warning' THEN 1 END) \"WARN\", "
+ " COUNT(CASE WHEN TYPE = 'Error' THEN 1 END) \"ERROR\" "
+ " FROM "
+ " EVENT"
+ " WHERE EVENT_DATE >= TRUNC(SYSDATE)-7");

How to fetch distinct values from multiple rows of select query?

I have used query as
string getPlayers = " Select Player1,Player2,Player3,Player4,Player5 from tbl_game where Player1=" + userid + " OR Player2=" + userid + " OR Player3=" + userid + " OR Player4=" + userid + " OR Player5=" + userid + " AND Complete = 'No' ";
It gives result as multiple rows that fulfills the condition.
But I want to store values of distinct Player1,Player2,Player3,Player4,Player5 in variables returned from select query, so that i can use these values for further case.
How can i do this ?
Please help me. I am very confused.
EDITED :
I got the result of select query as -
(1,2,2,4,5)
(2,3,1,4,5)
(4,3,5,1,2)
Where 1,2,3,4,5 are userids(players).
I want store these userids in variables as -
p1=1 , p2=2 , p3=3 ,p4=4, p5=5
string getPlayer1 = (" select var1 from (" Select distinct Player1 as var1 ,Player2,Player3,Player4,Player5 from tbl_game where Player1=" + userid + " OR Player2=" + userid + " OR Player3=" + userid + " OR Player4=" + userid + " OR Player5=" + userid + " AND Complete = 'No' "));
var1 will have a value of player1. Repeat this for all other players. Hope this is what you want.