Currently I have a criteria as shown below which finds average of column1 in my base table while joining with table1 and table2.
$criteria= new CDbCriteria;
$criteria->select = 'AVG(t.column1) as cc';
$criteria->group = 'table1.id';
$criteria->with = array('table1', 'table2');
How do I group by tabel1.id as well as table2.id in the above criteria?
seperate them by ,
$criteria->group = 'table1.id , table2.id';
Related
I need to pick the dynamic column based on some condition and it has to be used in where condition.
AuditLog::select('audit_log.id')
->join('columns_list', 'columns_list.id', '=', 'audit_log.interim_column_table_id')
->join('transactions', 'transactions.id', '=', 'audit_log.interim_transaction_id')
->whereRaw(columns_list.column_name = ?', ["p_gross"])
->latest('audit_log_transaction_level.created_at')
In the above query, I need to replace the ["p_gross"] with the following code
CASE
WHEN transactions.transaction_type_id = 1 THEN 'p_gross'
WHEN transactions.transaction_type_id = 2 THEN 's_gross'
END
here is the raw SQL for a better understanding
select audit_log.id
from audit_log
join columns_list on columns_list.id = audit_log.interim_column_table_id
join transactions on audit_log.interim_transaction_id = transactions.id
where
columns_list.column_name = CASE
WHEN transactions.tbi_transaction_type_id = 1 THEN 'p_gross'
WHEN transactions.tbi_transaction_type_id = 2 THEN 's_gross'
END
order by audit_log.created_at desc
My Query
SELECT
stoMast.sStockistCode,
stoMast.sStateName,
stoMast.sDivision,
stateMap.sRMCode
FROM
tblRSM_State_Mapping stateMap
INNER JOIN
tblStockistMaster stoMast ON
stateMap.sStateName = stoMast.sStateName
WHERE
stateMap.sRMCode = 'MCNE04001'
and
stoMast.sDivision = 'CIDIS'
except
select
sStockistCode,
sStateName,
sDivision,
sRMCode
From
tblEntry
Again I would like to compare the query result columns
sStockistCode
sStateName
sDivision
with tblStockistMaster with the same fields
sStockistCode
sStateName
sDivision
and retrieve the STOCKIST NAME.
Don't know how to compare the above query result with the table.
Maybe you can use following SQL code used with CTE expression
;with cte as (
SELECT
stoMast.sStockistCode,
stoMast.sStateName,
stoMast.sDivision,
stateMap.sRMCode
FROM
tblRSM_State_Mapping stateMap
INNER JOIN
tblStockistMaster stoMast ON
stateMap.sStateName = stoMast.sStateName
WHERE
stateMap.sRMCode = 'MCNE04001'
and
stoMast.sDivision = 'CIDIS'
except
select
sStockistCode,
sStateName,
sDivision,
sRMCode
From
tblEntry
)
select
cte.*,
sm.sStockistName
from cte
left join tblStockistMaster as sm
on sm.sStockistCode = cte.sStockistCode and
sm.sStateName = cte.sStateName and
sm.sDivision = cte.sDivision
-- I retrieve the stockist Name
SELECT
stoMast.sStockistName,
FROM
tblRSM_State_Mapping stateMap
INNER JOIN
tblStockistMaster stoMast
ON stateMap.sStateName = stoMast.sStateName
-- I'm joining table entry If I can match
LEFT JOIN
tblEntry tbl
on tbl.sStockistCode = stoMast.sStockistName
AND tbl.sStateName = stoMast.sStateName
AND tbl.sDivision = stoMast.sDivision
AND tbl.sRMCode = stateMap.sRMCode
WHERE
stateMap.sRMCode = 'MCNE04001'
and stoMast.sDivision = 'CIDIS'
-- And The the exept thing, I don't want that got a match with the tableentry
AND COALESCE(tbl.sStockistCode, tbl.sStateName, tbl.sDivision, tbl.sRMCode) is null
I expect it is what you wanted to get. On another way please help us understand wht you come from (tables and idea of what data can be in) and the result you want. Will be easier to create a query.
basically i have a table1 table and tabl2 table.
relation one to many
So i'm trying to make a select like
$em->createQueryBuilder()
->select('t1')
->from('AcmeAppBundle:Table1', 't1')
->orderBy('t1.id', 'DESC')
->getQuery()->getResult();
so i need the same query but i need the order to be by number of records from table2
the relation is set in the entity something like
* #ORM\OneToMany(targetEntity="Table2", mappedBy="something")
in raw query it looks like
SELECT table1.* FROM table1 left join table2 on table1.id = table2.table1_id
group by (table2.table1_id)
$qb = $em->createQueryBuilder();
$qb
->select('t1')
->from('AcmeAppBundle:Table1', 't1')
->leftJoin('BLABLABundle:BlaEntity', 't2', 'WITH', 't1.id = t2.table1_id')
->orderBy('t1.id', 'DESC')
->groupBy('t2.table1_id');
$result = $qb->getQuery()->getResult();
$qb = $em->createQueryBuilder();
$qb
->select('count(t2.id) count')
->from('AcmeAppBundle:Table1', 't1')
->join('AcmeAppBundle:Table1', 't2', 'WITH', 't1.id = t2.table1_id')
->orderBy('count', 'DESC')
->groupBy('t1.id');
Somethins like this. But not tested.
This would order the result by number of records from table2:
$queryBuilder = $entityManager->createQueryBuilder()
->select('t1, COUNT(t2.id) AS myCount')
->from('AcmeAppBundle:Table1', 't1')
->leftJoin('t1.fields', 't2') //fields is the one to many field name for the target entity AcmeAppBundle:Table2
->groupBy('t1.id')
->orderBy('myCount', 'DESC');
$result = $queryBuilder
->getQuery()->getResult();
I have one table in my Database that has column names: buildNumber, result, versionStatus. Now in my SQL statement I want to display the 'buildNumber' where the 'versionStatus' is old and the 'result' is pass and where the versionStatus is new and the result is 'fail'. So I want to display anything that has a result of fail today but had a result of pass last time. So if I have these records in the DB (column separated by --):
build2--pass--old
build2--fail--new
The SQL statement should only display "build2" because it passed with "old" but now 'failed' with new version.
I have tried:
select *
from CSAResults.dbo.Details
where result = 'pass'
and versionStatus = 'Old'
and versionStatus IN (select CSAResults.dbo.Details.versionStatus
from CSAResults.dbo.Details
where versionStatus = 'New'
and result = 'fail')
but nothing is returned.
Thanks
This query does a self-joins of Details table to get the result you want.
SELECT distinct new.buildNumber
FROM CSAResults.dbo.Details old
JOIN CSAResults.dbo.Details new ON old.buildNumber = new.buildNumber
WHERE old.result = 'pass'
AND old.versionStatus = 'Old'
AND new.result='fail'
AND new.versionStatus='New'
I added the distinct in the select clause so you wouldn't get duplicate results if there were multiple old versions of the build that had passed
Your existing query should work if you change the IN condition to be:
and buildNumber IN (select CSAResults.dbo.Details.buildNumber
Alternatively, a better performing query might be:
Select buildNumber
from CSAResults.dbo.Details
group by buildNumber
having count(distinct case
when result = 'pass' and versionStatus = 'Old' then 1
when result = 'fail' and versionStatus = 'New' then 2
end) = 2
An alternative would be to use an INNER JOIN as so:
select t.*
from CSAResults.dbo.Details t INNER JOIN (SELECT t2.buildNumber
FROM CSAResults.dbo.Details t2
WHERE t2.versionStatus = 'New'
and t2.result = 'fail') t1
ON t.buildNumber = t1.buildNumber
where t.result = 'pass'
and t.versionStatus = 'Old'
Nothing returned? Hardly surprising: you're trying to assert
(VersionStatus='New') = (VersionStatus='Old')
Try something like this instead
select *
from CSAResults.dbo.Details
where result = 'pass'
and versionStatus = 'Old'
and buildNumber IN (select CSAResults.dbo.Details.buildNumber from CSAResults.dbo.Details
where versionStatus = 'New'
and result = 'fail')
i am trying to write a query that will update the schemeid of a row with the id of the matching row from another table.
tblschemes b holds the schemes detail and tblclaims_liberty a holds the claims data and the fields to join on are b.nett_scheme = a.schemename and a.agentcode = b.agentcode
why then id the query below allocating a schemeid to rows of data that do not match the agentcode??
update tblclaims_liberty
set tblclaims_liberty.schemeid = tblschemes.id
from tblschemes inner join tblclaims_liberty on tblclaims_liberty.schemename = tblschemes.nett_scheme and tblclaims_liberty.agentcode = tblschemes.agentcode
where
ce_report = 'yes'
and (tblclaims_liberty.schemeid != tblschemes.id or schemeid is null) --only updates rows that require updateing instead of 6mil+ lines of data.
can someone point me in the right direction?? why is it not recognising the agentcode match?
regards,
Adam
Try with this:
update tblclaims_liberty TLtoUpdate
set TLtoUpdate.schemeid =
(
select TS.id
from tblschemes TS inner join tblclaims_liberty TL on TL.schemename = TS.nett_scheme and TL.agentcode = TS.agentcode
where TL.schemeid = TLtoUpdate.schemeid
)
where TLtoUpdate.ce_report = 'yes' and TLtoUpdate.schemeid is null
Looks there was missing the TL.id condition in the subquery.