How to write mysql sub query in CDbCriteria and CActiveDataProvider in yii? - yii

example :
SELECT * FROM (
SELECT *
FROM `servicelist` `t`
WHERE t.is_accept_by_driver = 0 ) as tmp GROUP BY servicenumber

Try this:
\Yii::app()
->db
->createCommand(
"SELECT * FROM (
SELECT *
FROM `servicelist` `t`
WHERE t.is_accept_by_driver = 0 ) as tmp GROUP BY servicenumber"
)
->queryAll()

Related

Expected SELECT statement

WITH 1_hour_trips as (
SELECT *
from bigquery-public-data.new_york.citibike_trips
where tripduration = 60
)
WITH creates a temporary view, and it then expects you to use a select to actually get the result you want. Something like this:
WITH 1_hour_trips as (
SELECT * from bigquery-public-data.new_york.citibike_trips
where tripduration = 60
)
SELECT * from 1_hour_trips;

Can I use With within another with?

I am trying to create something like this but end up in errors. Not sure if this is the right way to do.
WITH SECONDMAIN AS
(
WITH MAIN AS
(
SELECT
....
)
SELECT
*
FROM MAIN M
JOIN ....
)
SELECT * FROM SECONDMAIN SM;
The syntax would be-
WITH MAIN AS
(
SELECT
....
),
SECONDMAIN AS
(
SELECT
*
FROM MAIN M
JOIN ....
)
SELECT * FROM SECONDMAIN SM;

Translating SQL Server 'OUTPUT' to Oracle?

I am having big issues translating my MSSQL (sql-server) query to Oracle (PL-SQL).
The goal is to do an update and a select on the updated field in one threadsafe operation.
My current MSSQL query:
UPDATE PDFCONVERT_G
SET PDF_STATUS = 1, PDF_STARTDATE = GETDATE(), PDF_CONVERTERNAME='inputConverterName'
OUTPUT Inserted.PDF_ACTION as Action,
Inserted.PDF_ARKMERK_VE As ARKMERK,
Inserted.PDF_TYPE_DL as DlTypeDT,
Inserted.PDF_DOKID_VE as DocId,
Inserted.PDF_DOKMALID_VE as DOKMALID,
Inserted.PDF_FILREF_VE as FILREF,
Inserted.PDF_FILTYPE_LF as Filtype,
Inserted.PDF_JPID_JP as JpId,
Inserted.PDF_LOCFILREF_VE as LOCFILREF,
Inserted.PDF_SAID_SA as SaId,
Inserted.PDF_SJEKKETUT_VE as SJEKKETUT,
Inserted.PDF_TGKODE_VE as TGKODE,
Inserted.PDF_VARIANT_VE as Variant,
Inserted.PDF_VERSJON_VE as Version,
Inserted.PDF_CHECKINAFTERCONVERT as CheckinAfterConvert
FROM PDFCONVERT_G t1
INNER JOIN (
SELECT TOP(1) *
FROM PDFCONVERT_G A WHERE (
(A.PDF_LAGRENH_VE = 'PROD' OR EXISTS(SELECT * FROM PDFCONVERT_G B WHERE A.PDF_JOBID=B.PDF_JOBID AND B.PDF_LAGRENH_VE='PROD' AND B.PDF_ACTION='MERGE'))
AND PDF_STATUS = 0 AND NOT EXISTS(
SELECT * FROM PDFCONVERT_G B where a.pdf_jobid = b.pdf_jobid and b.pdf_status > 0 and a.pdf_action != b.pdf_action))
ORDER BY A.PDF_PRIORITY DESC, A.PDF_JOBID, A.PDF_RNR
) t2 ON t2.PDF_JOBID = t1.PDF_JOBID
I can simply do this query in my .net code and the result will be the Output variables.
I know Oracle has the RETURNING INTO syntax but concidering how complex my WHERE clause is I simply get syntax errors all the time.
I would prefer to write a query without creating a function but even if I have to do that, I am having issues.
SELECT TOP(1) *
FROM PDFCONVERT_G A
WHERE (
( A.PDF_LAGRENH_VE = 'PROD'
OR EXISTS(SELECT *
FROM PDFCONVERT_G B
WHERE A.PDF_JOBID=B.PDF_JOBID
AND B.PDF_LAGRENH_VE='PROD'
AND B.PDF_ACTION='MERGE')
)
AND PDF_STATUS = 0
AND NOT EXISTS(
SELECT *
FROM PDFCONVERT_G B
where a.pdf_jobid = b.pdf_jobid
and b.pdf_status > 0
and a.pdf_action != b.pdf_action
)
)
ORDER BY A.PDF_PRIORITY DESC, A.PDF_JOBID, A.PDF_RNR
Can be rewritten (without the correlated sub-queries) as:
SELECT *
FROM (
SELECT a.*,
ROW_NUMBER() OVER ( ORDER BY PDF_PRIORITY DESC, PDF_JOBID, PDF_RNR ) AS rn
FROM (
SELECT a.*,
COUNT(
CASE WHEN DF_LAGRENH_VE = 'PROD'
AND PDF_ACTION = 'MERGE'
THEN 1 END
) OVER ( PARTITION BY pdf_jobid )
AS num_prod_merge,
COUNT(
CASE WHEN pdf_status > 0 THEN 1 END
) OVER ( PARTITION BY pdf_jobid )
AS num_all_actions,
COUNT(
CASE WHEN pdf_status > 0 THEN 1 END
) OVER ( PARTITION BY pdf_jobid, pdf_action )
AS num_same_actions
FROM PDFCONVERT_G a
) a
WHERE ( PDF_LAGRENH_VE = 'PROD' OR num_prod_merge > 0 )
AND PDF_STATUS = 0
AND num_all_actions = num_same_actions
)
WHERE rn = 1;
You can then rewrite your UPDATE to something like:
UPDATE PDFCONVERT_G
SET PDF_STATUS = 1,
PDF_STARTDATE = SYSDATE,
PDF_CONVERTERNAME='inputConverterName'
WHERE ROWID = (
SELECT ROWID
FROM (
-- as above
)
WHERE rn = 1
)
RETURNING PDF_ACTION -- , ...
INTO :Action -- , ...
(Note: Unable to test this at the moment so there may be some small syntax errors but you should get the general idea.)

How do I create a conditional SQL query

I am trying to create an Oracle Sql query using IF/Else statements
IF EXISTS
(
SELECT *
FROM baninst1.an_employee_position
WHERE baninst1.an_employee_position.person_uid = 593791
AND baninst1.an_employee_position.position_end_date IS NULL) THEN
SELECT *
FROM baninst1.an_employee_position
WHERE baninst1.an_employee_position.person_uid = 593791
AND (
baninst1.an_employee_position.position_end_date IS NULL
OR baninst1.an_employee_position.position_end_date > SYSDATE)
AND baninst1.an_employee_position.effective_start_date <= SYSDATE;ELSE
SELECT *
FROM (
SELECT *
FROM baninst1.an_employee_position
WHERE baninst1.an_employee_position.person_uid = 593791 )
WHERE ROWNUM = 1;END IF;
However I receive an "Unknown Command" error when I run it. No more error information
This may provide what you are looking for:
SELECT a.*
FROM employee_position a
where person_uid = 593791
and (
(a.position_end_date is null)
or
(
a.position_end_date =
(select max(position_end_date)
from employee_position b
where b.person_uid = a.person_uid
and b.position_end_date is not null
)
)
)
Another way
SELECT a.*
FROM employee_position a
where person_uid = 593791
and (
nvl(a.position_end_date, trunc(sysdate+100)) >=
(select max(position_end_date)
from employee_position b
where b.person_uid = a.person_uid
and b.position_end_date is not null
)
)

How to do an IN with a subquery returning 2 columns?

I have this Query
SELECT *
FROM GUITARS.FENDER
WHERE FENDER.GUITARTYPE IN (
SELECT GUITARTYPE,GUITARSUBTYPE
FROM GUITARS.GUITAR_TYPE
WHERE GuitarColor = 'RED')
I can get it to run when I'm only doing GUITARTYPE in the subquery but not both GUITARTYPE,GUITARSUBTYPE.
Don't use in, use exists:
SELECT *
FROM GUITARS.FENDER
WHERE EXISTS (SELECT 1
FROM GUITARS.GUITAR_TYPE
WHERE GuitarColor = 'RED' AND
FENDER.GUITARTYPE = GUITARTYPE.GUITARTYPE AND
FENDER.GUITARSUBTYPE = GUITARTYPE.GUITARSUBTYPE
);