I am using Crystal Reports and I am trying to upload a view. I want the view to reference itself but I get this error message:
" View or function 'ProductionOrdreItemsFull' contains a self-reference. Views or functions cannot reference themselves directly or indirectly."
Does anyone have a clue how can I call a view from itself?
.
.
.
.
.
.
.
UNION
SELECT T8.DocEntry, - 3, T9.Code, T8.PlannedQty * T9.Quantity,
T8.CompTotal * T9.Quantity, T10.ItemName, T10.QryGroup5
FROM dbo.ProductionOrdreItemsFull T8 INNER JOIN
ITT1 T9 ON T8.ItemCode = T9.Father INNER JOIN
OITM T10 ON T9.Code = T10.ItemCode
WHERE T8.LineNum = - 2
A View definition can't refer to itself. But, you can achieve the same thing using Common Table Expressions (CTEs). See for example this blog about 'Hierarchies WITH Common Table Expressions'.
Crystal also has a special "Hierarchical Grouping" feature you can Google.
Related
When querying one table using the doctrine query builder a partial select can be written like this:
$queryBuilder = $this->createQueryBuilder('person');
$queryBuilder->addSelect('partial person.{id, name}');
How can one write a partial select be written for a left joined table? I tried something like this, but can't figure out the correct syntax:
$queryBuilder = $this->createQueryBuilder('person');
$queryBuilder->join('person.address');
$queryBuilder->addSelect('partial person.{id, name} person.address.city'); // ???
My goal would be to select only parts of the Person and the Address object when executing the query to be more memory efficient.
Your syntax is off for your join operation. You have to give an alias when using join. From there, you can just use the same syntax to query your partial Address object:
// In a method of PersonRepository
$qb = $this->createQueryBuilder('person')
->select(['partial person.{id, name}', 'partial address.{id, city}'])
->join('person.address', 'address');
Notice that I added id to the fields retrieved for Address. If you don't, Doctrine will give you the following error:
Error: The partial field selection of class Path\To\Entity\Address must contain the identifier
As a side note, you said you wanted to write this select for a left joined table. If you want to perform a LEFT JOIN, you need to use leftJoin instead of join (the signature of both methods is the same).
hi so im trying to create a dropdown that is based form a sql joined query so far here is what i have (got a help from fellow stackoverflow-ers)
$cats = DB::table('nsa_subcategory')
->join('nsa_maincategory' , 'nsa_subcategory.maincategoryid' , '=' , 'nsa_maincategory.maincategoryid')
->lists(DB::raw('CONCAT(nsa_subcategory.subcategoryname , " | ", nsa_maincategory.maincategoryname)'),'nsa_subcategory.subcategoryid');
what im trying to do was join 2 tables display the subcategory and maic category but the value i would get is the subcategory id the code above produces this kind of error
any ideas what im doing wrong or any ideas on how to improve my code? thanks so much in advance!
Instead of putting the DB::raw in the ->lists, put it in your select and give it a name as in the code below category, then you retrieve it using ->lists
$cats = DB::table('nsa_subcategory')
->select(DB::raw('CONCAT(nsa_subcategory.subcategoryname , " | ", nsa_maincategory.maincategoryname) AS category'),'nsa_subcategory.subcategoryid')
->join('nsa_maincategory' , 'nsa_subcategory.maincategoryid' , '=' , 'nsa_maincategory.maincategoryid')
->lists(category, subcategoryid);
Ok, I feel really stupid for asking this, but it's driving me nuts and I can't figure it out. The docs say I should be able to use select AS in a Rails/ActiveRecord query. So:
d = Dvd.where(id: 1).select("title AS my_title")
Is a valid query and if I do a to_sql on it, it produces the expected SQL:
SELECT title AS my_title FROM `dvd` WHERE `dvd`.`id` = 1
However, d.my_title will give an error:
NoMethodError: undefined method `my_title' for #<ActiveRecord::Relation
I need to be able to use AS since the columns I want to retrieve from different joins have the same name so I can't access them the "regular" way and have to resort to using AS.
I also don't want to resort to using find_by_sql for future compatibility and a possible switch form Mysql to PostGresql.
Just to clarify, what I'm really trying to do is write this SQL in a Railsy way:
SELECT tracks.name AS track_name, artists.name AS artist_name, composers.name AS composer_name, duration
FROM `tracks_cds`
INNER JOIN `tracks` ON `tracks`.`id` = `tracks_cds`.`track_id`
INNER JOIN `artists` ON `artists`.`id` = `tracks_cds`.`artist_id`
INNER JOIN `composers` ON `composers`.`id` = `tracks_cds`.`composer_id`
WHERE cd_id = cd.id
The top example was just a simplification of the fact that SELECT AS will not give you an easy way to refer to custom fields which I find hard to believe.
ActiveRecord automatically creates getter and setter methods for attributes based on the column names in the database, so there will be none defined for my_title.
Regarding the same common names, why not just do this:
d = Dvd.where(id: 1).select("dvds.title")
You can write your sql query and then just pass into ActiveRecord's execute method
query = "SELECT title AS my_title FROM `dvd` WHERE `dvd`.`id` = 1"
result = ActiveRecord::Base.connection.execute(query)
I have been trying to join two custom table using magento's commands. After searching i came across this block of generic code
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join( array('table_alias'=>$this->getTable('module/table_name')),
'main_table.foreign_id = table_alias.primary_key',
array('table_alias.*'),
'schema_name_if_different');
Following this as template I have tried to join my tables together but have only returned errors such as incorrect table name or table doesn't exist or some other error.
Just to clear things up can someone please correct me on my understanding
$collection = Mage::getModel('module/model_name')->getCollection();
Gets an instance of your model. Within that model is the table that holds the required data (for this example I shall call the table p)
$collection->getSelect()
Select data from table p
->join()
Requires three parameters to join two table together
PARAM1
array('table_alias'=>$this->getTable('module/table_name'))
'the alised name you give the table' => 'the table you want to add to the collection (this has been set up in the model folder)'
PARAM2
'main_table.foreign_id = table_alias.primary_key'
This bit i don't get (it seems straight forward though)
my main table (p) doesn't have a foreign id (it has it's primary key - is that also its foreign id)?
has to be equal to the alised name you gave in param1
PARAM3
'main_table.foreign_id = table_alias.primary_key'
get all from alised name
Where have I gone wrong on my understanding?
Please have a look in below sql join statement, I am using it in my project and it is working perfectly.
Syntax
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join(Mage::getConfig()->getTablePrefix().'table_name_for_join', 'main_table.your_table_field ='.Mage::getConfig()->getTablePrefix().'table_name_for_join.join_table_field', array('field_name_you_want_to_fetch_from_db'));
Working Query Example
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join(Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar', 'main_table.products_id ='.Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar.entity_id', array('value'));
Hope this will work for you !!
I have a simple data model: a Product can have many Tag objects.
The Tag model has a tag field and a value field, both strings (value is not important here).
The following DQL query gets me all Products and their tags:
SELECT p, t FROM Product p LEFT JOIN p.tags t
However, how do I only select Products that have a certain tag (e.g. "blue")? It is important that I get all the tags for the returned product, so I cannot simply do a WHERE t.tag = 'blue'.
As a side question; do you have any thoughts on whether it would be better to implement this using a separate Tag table, and then having a ProductTag table knitting them together (proper many-to-many relation)?
You can use a DQL query like this:
"SELECT p, t FROM Product p LEFT JOIN p.tags t WHERE p.id IN (
SELECT sp.id FROM Product sp INNER JOIN sp.tags st WHERE st.tag = 'blue'
)"
This will return all product object where a Tag.tag = 'blue' is found in their collection of tags, but also other tags are joined.
I tried this out in a Symfony2 project and it worked like this. Because I got all the information used for this from the Doctrine Documentation I think it will work too in the standalone version.
If it doesn't work tell me the error and I will take a close look at this problem.