Limit return data for 7 days - sql

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");

Related

How to insert if statement in sql query at vb.net?

How to insert if/else in the query?
This is my sqlcommand query :
sqlcommand ("select Machine_Code,Machine_Name,Status from
Machine_Master where '" & combolinecode.Text & "' = Line_Code" )
I wanna change the value of Status from 1 => active and 0 => not active.
You can do this with the ANSI standard case statement in almost all databases:
select Machine_Code, Machine_Name,
(case when Status = 1 then 'active' else 'not active' end) as status
from Machine_Master
where '" & combolinecode.Text & "' = Line_Code";
I fear that if you are using VBA you might be using Access, which has its own way:
select Machine_Code, Machine_Name,
iif(Status = 1, "active", "not active") as status
from Machine_Master
where '" & combolinecode.Text & "' = Line_Code";
You can use case :
select Machine_Code
, Machine_Name
, (case when Status = 1 then 'active' else 'not active' end) as Status
from Machine_Master
where '" & combolinecode.Text & "' = Line_Code

JPA Query with Case condition

(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

Can these two SQL statements be made into one? Changing multiple indices with two constraints

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.

SQL UPDATE doesn't work with foreign languages (Arabic)

the UPDATE gives ???? if the updater field was written in Arabic and this is my query:
UPDATE students
SET first_name = 'الاسم' , last_name = 'الاسم الاخير' ,
father_name = 'الاسم الاخير' , mother_name = '',
birth_date = '1/1/1990 12:00:00 AM' , education_level = '' ,
address = '' , notes = ''
WHERE student_id = 33
And here is the result of the update:
student_id first_name last_name mother_name father_name birth_date
33 ????? ????? ?????? ??????????? 1990-01-01
//the answer is great and thank you people, another question is that I am using this UPDATE syntax in my C# program
command.CommandText = "UPDATE students SET " +
"first_name = " + "'" + first_name + "'" + " , last_name = " + "'" + last_name + "'" +
" , father_name = " + "'" + father_name + "'" + " , mother_name = " +
"'" + mother_name + "'" + ", birth_date = " + "'" + birth_date + "'" +
" , education_level = " + "'" + education_level + "'" +
" , address = " + "'" + address + "'" + " , notes = " + "'" + notes + "'" +
" WHERE student_id = " + id ;
//how to use the character N
You have forgotten the N prefix before your string literals which is required so they will be treated as nvarchar rather than varchar
SET first_name = N'الاسم' etc.
without that the text is coerced into whatever characters the code page of your default collation can deal with.
Create the database with this collation Arabic_CI_AS, you won't need to put N before the Arabic characters.

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.