i'm Using subsonic 2.0 and i need to execute the below query
Select * from User where User ID = 1 and (Status = 2 Or Status = 3)
Your help will be highly appreciated.
This is how I did it using and In statement:
Select * from User Where UserID = 1 And Status In (2,3)
TblUserCollection vacReqColl = new Select().From(TblUser.Schema).Where(TblUser.Columns.UserI).IsEqualTo(userID). And(TblUser.Columns.Status).In(Status1, Status2).OrderAsc(new string[] { "CreationDate" }).ExecuteAsCollection<TblUserCollection>();
Related
Is there any way I could re-write this SQL query using Prisma Client.
SELECT TOP 1 * FROM [myTable]
WHERE Name = 'Test' and Size = 2 and PType = 'p'
ORDER BY ABS( Area - #input )
You can use queryRaw and directly pass the raw SQL to execute it.
It would look like this:
const result = await prisma.$queryRaw`SELECT TOP 1 * FROM [myTable]
WHERE Name = 'Test' and Size = 2 and PType = 'p'
ORDER BY ABS( Area - #input )`
I want to write this query using all, but it is not valid SQL:
SELECT *
FROM OrderCustPlankLoad ocl
WHERE ocl.PlankLoadStatusId = 2 and all
(Select PlankLoadStatusId from OrderAccountPlankLoad oal where oal.PlankLoadStatusId = 2
and ocl.PlankClientId = oal.PlankClientId
)
What I want to ensure is that all related records in the related table have a status of 2.
But I don't think I am writing this correctly - SSMS does not like the "All" as well as "ocl.PlankClientId" in the query.
What I am doing is ensuring that all the records are valid before I start processing them. I don't want to process the rows in ocl if there are related rows in oal that are not valid.
How do I write this correctly?
select *
FROM OrderCustPlankLoad ocl
WHERE ocl.PlankLoadStatusId = 2 and not exists
(Select 1 from OrderAccountPlankLoad oal where oal.PlankLoadStatusId <>2
and ocl.PlankClientId = oal.PlankClientId
)
I think this is what you mean:
SELECT *
FROM OrderCustPlankLoad ocl
WHERE ocl.PlankLoadStatusId = 2
AND ocl.PlankClientId IN
(SELECT oal.PlankClientId
FROM OrderAccountPlankLoad oal
WHERE oal.PlankLoadStatusId = 2
)
I came up with this which seems to be working:
SELECT *
FROM OrderCustPlankLoad ocl
WHERE ocl.PlankLoadStatusId = 2
AND NOT EXISTS (
SELECT 1
FROM OrderAccountPlankLoad oal
WHERE oal.PlankLoadStatusId = 5
AND ocl.PlankClientId = oal.PlankClientId
)
I need two combine two queries to get one result only. Here's the first code.
SELECT FKLTI_KTRGN, COUNT(DISTINCT VIEW_PELAJAR_ENROLL.MB_NAMA) AS BIL_UG
FROM VIEW_PELAJAR_ENROLL, KOD_BANGSA, KOD_NEGERI, JANTINA, KOD_AGAMA, KOD_CACAT, TARAF_KAHWIN
WHERE KOD_KTRGN_PROGRAM = 'SARJANA MUDA'
AND MS_SESI = '2015/2016'
AND MB_BANGSA = KOD_BANGSA
AND KOD_NEGERI = MB_ASAL
AND MB_JANTINA = JAN_KOD
AND KOD_AGAMA = MB_AGAMA
AND KOD_CACAT = MB_CACAT
AND KOD_TARAF = MB_TARAF_KAHWIN
AND MS_STATUS_SEMASA IN
(SELECT SP_KOD
FROM STATUS_PELAJAR
WHERE STATUS_ENROLL = 'Y'
AND SP_TAMAT IS NULL
AND SP_KOD = MS_STATUS_SEMASA)
GROUP BY FKLTI_KTRGN
And this is the second code.
SELECT FKLTI_KTRGN, COUNT(DISTINCT PELAJAR_BIODATA.MBUT_NAMA) AS BIL_PG
FROM PELAJAR_BIODATA,KOD_NEGERI,FAKULTI,KOD_IJAZAH,PELAJAR_BUTIR_PENGAJIAN,STATUS_PENGAJIAN A,
KOD_STATUS_PELAJAR_IPS, KOD_CACAT, KOD_BANGSA, E_KOD_PENGAJIAN, JANTINA, KOD_PROGRAM
WHERE A.STAT_STATUS IN (SELECT KOD_STATUS
FROM KOD_STATUS_PELAJAR_IPS
WHERE STATUS_ENROLL='Y'
AND KOD_STATUS=STAT_STATUS)
AND A.STAT_NOMKPB=MBUT_NOMKPB
AND PBP_PROGRAM IN ('5','6')
AND MBUT_ASAL=KOD_NEGERI(+)
AND SUBSTR(A.STAT_KOD_IJAZAH,1,1)=FKLTI_KOD
AND PBP_PROGRAM=KOD_PROGRAM.KOD_PROGRAM
AND A.STAT_KOD_IJAZAH=KOD_IJAZAH_UM
AND A.STAT_NODAFTAR=PBP_NODAFTAR
AND MBUT_CACAT=KOD_CACAT
AND KOD_BANGSA=MBUT_BANGSA
AND PBP_JENIS_PENGAJIAN=KOD_JNS_PENGAJIAN
AND MBUT_JANTINA=JAN_KOD
AND MBUT_WARGA IS NOT NULL
AND MBUT_BANGSA IS NOT NULL
AND MBUT_JANTINA IS NOT NULL
AND MBUT_NEGERI IS NOT NULL
AND PBP_PROGRAM IS NOT NULL
AND A.STAT_STATUS=KOD_STATUS
AND PBP_KOD_IJAZAH IS NOT NULL
AND A.STAT_SESI||A.STAT_SEMESTER IN (SELECT MAX(B.STAT_SESI||B.STAT_SEMESTER) FROM STATUS_PENGAJIAN B
WHERE B.STAT_NODAFTAR=A.STAT_NODAFTAR
AND B.STAT_NOMKPB=A.STAT_NOMKPB
AND A.STAT_SESI||A.STAT_SEMESTER=B.STAT_SESI||B.STAT_SEMESTER)
AND A.STAT_SESI = '2015/2016'
group by FKLTI_KTRGN
This is the expected result:
This is what i get:
I've tried to use UNION but it doesn't work. Anyone can help me? I really need your help. Thank you in advance!
In order to use union, you need to have the same number of fields and the same field name of both query.
as in both queries of yours, the second field has different name: BIL_PG, BIL_UG
Try this
select * from
(SELECT FKLTI_KTRGN, COUNT(DISTINCT VIEW_PELAJAR_ENROLL.MB_NAMA) AS BIL_UG
FROM VIEW_PELAJAR_ENROLL, KOD_BANGSA, KOD_NEGERI, JANTINA, KOD_AGAMA, KOD_CACAT, TARAF_KAHWIN
WHERE KOD_KTRGN_PROGRAM = 'SARJANA MUDA'
AND MS_SESI = '2015/2016'
AND MB_BANGSA = KOD_BANGSA
AND KOD_NEGERI = MB_ASAL
AND MB_JANTINA = JAN_KOD
AND KOD_AGAMA = MB_AGAMA
AND KOD_CACAT = MB_CACAT
AND KOD_TARAF = MB_TARAF_KAHWIN
AND MS_STATUS_SEMASA IN
(SELECT SP_KOD
FROM STATUS_PELAJAR
WHERE STATUS_ENROLL = 'Y'
AND SP_TAMAT IS NULL
AND SP_KOD = MS_STATUS_SEMASA)
GROUP BY FKLTI_KTRGN)
union
select * from (SELECT FKLTI_KTRGN, COUNT(DISTINCT PELAJAR_BIODATA.MBUT_NAMA) AS BIL_PG
FROM PELAJAR_BIODATA,KOD_NEGERI,FAKULTI,KOD_IJAZAH,PELAJAR_BUTIR_PENGAJIAN,STATUS_PENGAJIAN A,
KOD_STATUS_PELAJAR_IPS, KOD_CACAT, KOD_BANGSA, E_KOD_PENGAJIAN, JANTINA, KOD_PROGRAM
WHERE A.STAT_STATUS IN (SELECT KOD_STATUS
FROM KOD_STATUS_PELAJAR_IPS
WHERE STATUS_ENROLL='Y'
AND KOD_STATUS=STAT_STATUS)
AND A.STAT_NOMKPB=MBUT_NOMKPB
AND PBP_PROGRAM IN ('5','6')
AND MBUT_ASAL=KOD_NEGERI(+)
AND SUBSTR(A.STAT_KOD_IJAZAH,1,1)=FKLTI_KOD
AND PBP_PROGRAM=KOD_PROGRAM.KOD_PROGRAM
AND A.STAT_KOD_IJAZAH=KOD_IJAZAH_UM
AND A.STAT_NODAFTAR=PBP_NODAFTAR
AND MBUT_CACAT=KOD_CACAT
AND KOD_BANGSA=MBUT_BANGSA
AND PBP_JENIS_PENGAJIAN=KOD_JNS_PENGAJIAN
AND MBUT_JANTINA=JAN_KOD
AND MBUT_WARGA IS NOT NULL
AND MBUT_BANGSA IS NOT NULL
AND MBUT_JANTINA IS NOT NULL
AND MBUT_NEGERI IS NOT NULL
AND PBP_PROGRAM IS NOT NULL
AND A.STAT_STATUS=KOD_STATUS
AND PBP_KOD_IJAZAH IS NOT NULL
AND A.STAT_SESI||A.STAT_SEMESTER IN (SELECT MAX(B.STAT_SESI||B.STAT_SEMESTER) FROM STATUS_PENGAJIAN B
WHERE B.STAT_NODAFTAR=A.STAT_NODAFTAR
AND B.STAT_NOMKPB=A.STAT_NOMKPB
AND A.STAT_SESI||A.STAT_SEMESTER=B.STAT_SESI||B.STAT_SEMESTER)
AND A.STAT_SESI = '2015/2016'
group by FKLTI_KTRGN)
I have a DB table
id (INT, NOT NULL)
username (NVARCHAR, NOT NULL)
holiday_replacement (INT, NULL)
The holiday_replacement is either NULL or it references a value in the id field.
Now, it would be trivial to determine for whom a specified id was the holiday_replacement, but to complicate this query the holiday_replacement may be chained.
In simpler terms, a user may have a holiday_replacement who themselves has a holiday_replacement.
In such a case the query should return a list of all users in this 'holiday replacement graph'.
Currently I have
WITH user_graph AS (
SELECT
*
FROM
table_name
WHERE
holiday_replacement = #current_user_id
UNION ALL
SELECT
tn.*
FROM
table_name tn
JOIN user_graph ug ON ug.holiday_replacement = tn.id
)
SELECT
id
FROM
user_graph
However, this query only returns those users who have directly referenced #current_user_id as their holiday_replacement and doesn't consider any other users who should also be returned on account of this chaining.
For example, if I have the following 3 users:
id = 1, username = 'user_1', holiday_replacement = NULL
id = 2, username = 'user_2', holiday_replacement = 1
id = 3, username = 'user_3', holiday_replacement = 2
then for #current_user_id = 1, the query should return
id
---
2
3
but presently it only considers that directly referenced user and returns
id
---
2
I just can't wrap my head around what I need to change. Can anyone help?
use below code
WITH user_graph AS (
SELECT
*
FROM
table_name
WHERE
holiday_replacement = #current_user_id
UNION ALL
SELECT
tn.*
FROM
table_name tn
JOIN user_graph ug ON ug.id =tn.holiday_replacement
)
SELECT
id FROM
user_graph
how to limit an integer query result to 1. a return of 2 to be 1, a return 1 to be 1, and a return of 0.5 to be 0.5 because it is <= 1. i don't want to modify the tables, i just want to modify the results.
This is my exact query.
select ((select "V01" from sports where "UID" = '1') * 1.0 ) /
(select "V01" from master where "BALL" = 'REQUIREMENT') ;
I'm using postgres.
To limit, you'd do something like this:
select
case
when yourNumber >= 1 then 1
else yourNumber
end
...
Then you just apply this concept to your query.
As noted by Wiseguy, you could also do:
select LEAST(yourNumber, 1)
, since this is postgresql.
The first solution will work with any ANSI SQL compatible database.
Update
Applied to your query, I think (if I understood what you want correctly) it would be like this:
select LEAST(1,
((select "V01" from sports where "UID" = '1') * 1.0 ) /
(select "V01" from master where "BALL" = 'REQUIREMENT')
);
use the LEAST function , docs: http://www.postgresql.org/docs/8.3/static/functions-conditional.html. Also, check out GREATEST too
SELECT LEAST(1, <your value>)
EDIT replaced GREATEST with LEAST
try this:
select CASE
WHEN ((select V01 from sports where UID = '1') * 1.0 ) /
(select V01 from master where BALL = 'REQUIREMENT') >= 1
THEN 1
ELSE ((select V01 from sports where UID = '1') * 1.0 ) /
(select V01 from master where BALL = 'REQUIREMENT')
END;