QueryBuilder : how to compose subselect clause - doctrine-query

I'm having trouble converting a plain SQL clause to a Doctrine query.
I've added the clause in plain SQL below:
$query = $doc->getEntityManager ()
->createQueryBuilder ()
->select ( 'r')
->from ( 'AppBundle:CFormResponse', 'r' )
->where ( 'r.formId = :id AND NOT (select f.private from cform f where f.FORM_ID = :id) ' )
->setParameter ( 'id', $formId )->getQuery ();
$result = $query->getResult();
My attempt to convert this clause into DQL is as follows (so far):
$qb = $doc->getEntityManager ()->createQueryBuilder ();
$query = $qb
->select ( 'r')
->from ( 'AppBundle:CFormResponse', 'r' )
->where ( $qb->expr()->andX(
$qb->expr()->eq('r.formId', ':id'),
$qb->expr()->addSelect('(SELECT f.private
FROM AppBundle:CForm f
WHERE f.formId = :id)'
)
) )
->setParameter ( 'id', $formId )->getQuery ();
$result = $query->getResult();
with the addSelect indicating where I'm stuck.
Can anyone point me in the direction of how to do this?

Basically, you need to add not to your addSelect:
$query = $qb
->select ( 'r')
->from ( 'AppBundle:CFormResponse', 'r' )
->where ( $qb->expr()->andX(
$qb->expr()->eq('r.formId', ':id'),
$qb->expr()->not(
$qb->expr()
->select(`f.private`)
->from('AppBundle:CForm', 'f')
->where('f.formId = :id'))
)
) )
->setParameter ( 'id', $formId )->getQuery ();

Related

Group by query Rising ORA-00934 exception

I have the following query
SELECT
t.orgname,
t.phone,
t.phone2,
t.fax,
t.address1,
t.address2,
t.address3,
t.address4,
t.city,
t.postal,
t.glname,
t.acctvalue,
t.acctname,
t.dateacct,
t.fiscalfrom,
t.fiscalto,
t.folio,
t.piece,
regexp_replace(coalesce(
LISTAGG(t.description, ' || ') WITHIN GROUP(
ORDER BY
t.acctvalue
), ''), '[^[:print:]]', '')
INTO
description,
SUM(t.amtacctcr) AS amtacctcr,
SUM(t.amtacctdr) AS amtacctdr
FROM (
SELECT
org.name AS orgname,
oi.phone,
oi.phone2,
oi.fax,
loc.address1,
loc.address2,
loc.address3,
loc.address4,
loc.city,
loc.postal,
glc.name AS glname,
ev.value AS acctvalue,
ev.name AS acctname,
fa.description,
fa.dateacct,
fa.amtacctcr,
fa.amtacctdr,
--getfactdocumentno(ad_table_id, record_id) AS piece,
(
SELECT
gcs.seqno
FROM
gl_category_sequence gcs
WHERE
gcs.c_period_id = fa.c_period_id
AND gcs.ad_table_id = fa.ad_table_id
AND gcs.record_id = fa.record_id
AND gcs.gl_category_id = glc.gl_category_id
) AS piece,
fiscalyearforperiod(fa.c_period_id, '01/01/', 'dd/MM/yy') AS fiscalfrom,
fiscalyearforperiod(fa.c_period_id, '31/12/', 'dd/MM/yy') AS fiscalto,
p.periodno AS folio
FROM
fact_acct fa
INNER JOIN c_period p ON ( fa.c_period_id = p.c_period_id )
INNER JOIN gl_category glc ON ( fa.gl_category_id = glc.gl_category_id )
INNER JOIN c_elementvalue ev ON ( fa.account_id = ev.c_elementvalue_id )
INNER JOIN ad_org org ON ( fa.ad_org_id = org.ad_org_id )
INNER JOIN ad_orginfo oi ON ( org.ad_org_id = oi.ad_org_id )
INNER JOIN c_location loc ON ( oi.c_location_id = loc.c_location_id )
WHERE
fa.ad_table_id = 318
AND fa.record_id = 1454983
AND fa.c_acctschema_id=1000003
ORDER BY
fa.fact_acct_id
) t
GROUP BY
t.orgname,
t.phone,
t.phone2,
t.fax,
t.address1,
t.address2,
t.address3,
t.address4,
t.city,
t.postal,
t.glname,
t.acctvalue,
t.acctname,
t.dateacct,
t.fiscalfrom,
t.fiscalto,
t.folio,
t.piece
order
by t.acctvalue;
I m getting the ora-00934
But as I see here what I m trying is possible. Example 10.15.
so where I m I wrong ?
Only incorrect code that I can see is the INTO clause.
Please remove the following code from your query and try:
INTO
description,
SUM(t.amtacctcr) AS amtacctcr,
SUM(t.amtacctdr) AS amtacctdr

MDX - How to "Summarize" a Filtered result set

i'm struggling to determine the proper MDX query syntax in order to return the expected result set after moving the filters in order to support advanced nested filtering.
The query below is what is being generated today and the screenshot shows the results. Does anyone have any insights as to how to get the results needed as described in the screenshot based on the query below.
Note - i believe the Filters need to stay where they are in order to support nested complex filters. i.e. ((A=1 OR B<1) AND (A>1 OR C=2)) || (B=3 AND C=4)
WITH
SET AllRowsSet AS
{
Filter
(
NonEmpty
(
called_cities.[called_city].Children*
called_countries.[called_country_name].Children*
billing_months.[billing_month].Children*
sources_pivot_v1.[source_name_l1].Children*
products_pivot.[product_name_l1].Children*
products_pivot.[product_name_l2].Children
,{
[Measures].[actual_duration]
,[Measures].[amount]
}
)
,
billing_months.[billing_month].CurrentMember.Member_Caption = '201805'
AND products_pivot.[product_name_l1].CurrentMember.Member_Caption = 'Usage'
AND products_pivot.[product_name_l2].CurrentMember.Member_Caption = 'LD International'
AND called_countries.[called_country_name].CurrentMember.Member_Caption <> 'TEST_NULL'
AND (
sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Company for Joint'
OR sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Cisco Call Manager'
)
)
}
MEMBER [Measures].totalrows AS AllRowsSet.Count
MEMBER [Measures].[called_city] AS called_cities.[called_city].CurrentMember.Member_Caption
MEMBER [Measures].[called_country_name] AS called_countries.[called_country_name].CurrentMember.Member_Caption
MEMBER [Measures].[billing_month] AS billing_months.[billing_month].CurrentMember.Member_Caption
MEMBER [Measures].[source_name_l1] AS sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption
MEMBER [Measures].[product_name_l1] AS products_pivot.[product_name_l1].CurrentMember.Member_Caption
MEMBER [Measures].[product_name_l2] AS products_pivot.[product_name_l2].CurrentMember.Member_Caption
SELECT
{
[Measures].[actual_duration]
,[Measures].[amount]
,[Measures].totalrows
,[Measures].[called_city]
,[Measures].[called_country_name]
,[Measures].[billing_month]
,[Measures].[source_name_l1]
,[Measures].[product_name_l1]
,[Measures].[product_name_l2]
} ON COLUMNS
,SubSet
(
Order
(
Filter
(
NonEmpty
(
called_cities.[called_city].Children*
called_countries.[called_country_name].Children*
billing_months.[billing_month].Children*
sources_pivot_v1.[source_name_l1].Children*
products_pivot.[product_name_l1].Children*
products_pivot.[product_name_l2].Children
,{
[Measures].[actual_duration]
,[Measures].[amount]
}
)
,
billing_months.[billing_month].CurrentMember.Member_Caption = '201805'
AND products_pivot.[product_name_l1].CurrentMember.Member_Caption = 'Usage'
AND products_pivot.[product_name_l2].CurrentMember.Member_Caption = 'LD International'
AND called_countries.[called_country_name].CurrentMember.Member_Caption <> 'TEST_NULL'
AND (
sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Company for Joint'
OR sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Cisco Call Manager'
)
)
,[Measures].[amount]
,BDESC
)
,0
,250
) ON ROWS
FROM
(
SELECT {billing_months.[v1_disabled].[v1_disabled].&[0]} ON 0 FROM calls
);
To aggregate you can -
1. Move the ON ROWS set into the WITH clause
2. Wrap it in the Aggregate
3. You will need to find a 'spare' dimension in your cube that is not being used in this query to create this new custom MEMBER:
WITH
SET AllRowsSet AS
{
Filter
(
NonEmpty
(
called_cities.[called_city].Children*
called_countries.[called_country_name].Children*
billing_months.[billing_month].Children*
sources_pivot_v1.[source_name_l1].Children*
products_pivot.[product_name_l1].Children*
products_pivot.[product_name_l2].Children
,{
[Measures].[actual_duration]
,[Measures].[amount]
}
)
,
billing_months.[billing_month].CurrentMember.Member_Caption = '201805'
AND products_pivot.[product_name_l1].CurrentMember.Member_Caption = 'Usage'
AND products_pivot.[product_name_l2].CurrentMember.Member_Caption = 'LD International'
AND called_countries.[called_country_name].CurrentMember.Member_Caption <> 'TEST_NULL'
AND (
sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Company for Joint'
OR sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Cisco Call Manager'
)
)
}
MEMBER [Measures].totalrows AS AllRowsSet.Count
MEMBER [Measures].[called_city] AS called_cities.[called_city].CurrentMember.Member_Caption
MEMBER [Measures].[called_country_name] AS called_countries.[called_country_name].CurrentMember.Member_Caption
MEMBER [Measures].[billing_month] AS billing_months.[billing_month].CurrentMember.Member_Caption
MEMBER [Measures].[source_name_l1] AS sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption
MEMBER [Measures].[product_name_l1] AS products_pivot.[product_name_l1].CurrentMember.Member_Caption
MEMBER [Measures].[product_name_l2] AS products_pivot.[product_name_l2].CurrentMember.Member_Caption
MEMBER [SpareDimension].[SpareHierarchy].[myAggregation] AS //<< REPLACE <[SpareDimension].[SpareHierarchy]> WITH A VALID DIMENSION/HIERARCHY IN YOUR CUBE THAT IS NOT BEING USED IN THE SCRIPT
Aggregate(
SubSet
(
Order
(
Filter
(
NonEmpty
(
called_cities.[called_city].Children*
called_countries.[called_country_name].Children*
billing_months.[billing_month].Children*
sources_pivot_v1.[source_name_l1].Children*
products_pivot.[product_name_l1].Children*
products_pivot.[product_name_l2].Children
,{
[Measures].[actual_duration]
,[Measures].[amount]
}
)
,
billing_months.[billing_month].CurrentMember.Member_Caption = '201805'
AND products_pivot.[product_name_l1].CurrentMember.Member_Caption = 'Usage'
AND products_pivot.[product_name_l2].CurrentMember.Member_Caption = 'LD International'
AND called_countries.[called_country_name].CurrentMember.Member_Caption <> 'TEST_NULL'
AND (
sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Company for Joint'
OR sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Cisco Call Manager'
)
)
,[Measures].[amount]
,BDESC
)
,0
,250
)
)
SELECT
{
[Measures].[actual_duration]
} ON COLUMNS
,[SpareDimension].[SpareHierarchy].[myAggregation] ON ROWS
FROM
(
SELECT {billing_months.[v1_disabled].[v1_disabled].&[0]} ON 0 FROM calls
);

subquery inside orWhere in laravel

I need assistance to build up the query like below in laravel:
SELECT *
FROM table t
WHERE t.a = 1
OR (t.a=0
AND t.id IN (
SELECT o.a_id
FROM other_table o
WHERE o.x > 3
)
);
You could try to build your exact current query, and in fact it might even be the most efficient to write it. But, if we rephrase your query using a left join, it becomes somewhat easier to express in Laravel code.
SELECT *
FROM your_table t
LEFT JOIN other_table o
ON t.id = o.a_id AND o.x > 3
WHERE
t.a = 1 OR
(t.a = 0 AND o.a_id IS NOT NULL);
This would translate to the following Laravel code:
$result = DB::table('your_table t')
->leftJoin('other_table o', function($join) {
$join->on('t.id', '=', 'o.a_id');
$join->on('o.x', '>', '3');
})
->where('t.a', '=', '1')
->orWhere(function($query) {
return $query->where('t.a', '=', '0')
->whereNotNull('o.a_id')
})
->get();

Laravel morphedByMany return all records by where condition and count

Table 1: products: id, title, publish,created_at
Table 2: tags: id,name,created_at
Table 3: taggable: id,tag_id,taggable_type,taggable_id
I want get all distinct tags with count order by name.
I want it to return an array like this:
[
['name1'=>'value1','count'=>3],
['name2'=>'value2','count'=>3]
]
I have tried to do it like this:
$tags = \App\Tag::distinct()->where( function( $query ){
$query->whereHas('products', function ( $subquery ){
$subquery->where('publish', 1 );
})->get()->toarray();
})->withCount('products')->get()->toarray();
but it returns all (product) tags and all products_count values are 1 like this:
[...],
[▼
"id" => 75
"name" => "test1"
"created_at" => "2018-10-30 18:49:51"
"products_count" => 1
],
[...]
...
EDIT:
SELECT DISTINCT `tags`.*,
(SELECT count(*)
FROM `products`
INNER JOIN `taggables` ON `products`.`id` = `taggables`.`taggable_id`
WHERE `tags`.`id` = `taggables`.`tag_id`
AND `taggables`.`taggable_type` = ?) AS `products_count`
FROM `tags`
WHERE (EXISTS
(SELECT *
FROM `products`
INNER JOIN `taggables` ON `products`.`id` =`taggables`.`taggable_id`
WHERE `tags`.`id` = `taggables`.`tag_id`
AND `taggables`.`taggable_type` = ?
AND `publish` = ?))
withCount('products') must be called first and distinct() must be the last before ->get()->toArray().
Try this (not tested though):
$tags = \App\Tag::withCount('products')->where( function( $query ){
$query->whereHas('products', function ( $subquery ){
$subquery->where('publish', 1 );
})->get()->toArray();
})->distinct()->get()->toArray();

Doctrine DQL update andWhere( 'DATE_FORMAT(date, "%Y-%m-%d" ) = ?', '2013-08-09 12:24:09')

I have problem with updating rows , date format in db is: Y-m-d H:i:s
$date ='2013-08-09';// from POST
Doctrine_Query::create()
->update( 'column z2ud' )
->set( array( 'z2ud.column' => 'value' ) )//
->where( 'z2ud.column = ?', 'value' )
->andWhere( 'DATE_FORMAT(date, "%Y-%m-%d" ) = ?', $date)
->execute();
SQL returns: Unknown aggregate alias: "%Y-%m-%d"
SOLVED : You must use \'%Y-%m-%d\' not -> "%Y-%m-%d"

Resources