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 "-".
Related
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";
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)
(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
I have a form that users can use to edit data in my database. The database is structured like this:
If a user wants to edit both a FAVE_COLOR and a FAVE_FOOD, how would I go about doing that in my SQL statement? I can think of this, but is there a way to do this in one statement?
string sql1 = "UPDATE MYTABLE " +
"SET PROP_VALUE = '" + form["color"] + "' " +
"WHERE ID = " + form["id"] + " " +
"AND PROP_NAME = 'FAVE_COLOR'"
string sql2 = "UPDATE MYTABLE " +
"SET PROP_VALUE = '" + form["food"] + "' " +
"WHERE ID = " + form["id"] + " " +
"AND PROP_NAME = 'FAVE_FOOD'"
string sql = "UPDATE MYTABLE " +
"SET PROP_VALUE = CASE " +
"WHEN PROP_NAME = 'FAVE_COLOR' THEN '" + form["color"] + "' " +
"WHEN PROP_NAME = 'FAVE_FOOD' THEN '" + form["food"] + "' " +
"END " +
"WHERE ID = " + form["id"] + " " +
"AND PROP_NAME IN ('FAVE_COLOR', 'FAVE_FOOD')"
But beware of SQL injection! You really should be using prepared statements, into which you pass your variables as parameters that do not get evaluated for SQL. If you don't know what I'm talking about, or how to fix it, read the story of Bobby Tables.
You can use case statements:
UPDATE MYTABLE
SET PROP_VALUE = (
CASE favefood
WHEN PROP_NAME = 'FAVE_FOOD'
THEN 'PIZZA'
CASE favecolor
WHEN PROP_NAME = 'FAVE_COLOR'
THEN 'BLUE'
WHERE ID = #myIdValue
For MS SQL Server you can use an UPDATE FROM which will update two properties at the same time, like this:
CREATE TABLE MYTABLE (
ID INT,
PROP_NAME VARCHAR(20),
PROP_VALUE VARCHAR(20));
go
INSERT INTO MYTABLE VALUES (1, 'A','B')
go
INSERT INTO MYTABLE VALUES (1, 'C', 'D')
go
UPDATE MYTABLE
SET PROP_VALUE = X.PROP_VALUE
FROM MYTABLE MT JOIN (
SELECT 'A' AS PROP_NAME, 'F' AS PROP_VALUE
UNION
SELECT 'C' AS PROP_NAME, 'G' AS PROP_VALUE) AS X ON MT.PROP_NAME = X.PROP_NAME
WHERE ID = 1
For other SQL DB server the solution should be similar if not identical.
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.