I'm having an issue with Doctrine's pager when the original query has a GROUP BY. The pager will generate this query:
SELECT COUNT(*) AS num_results FROM event e GROUP BY e.type
which is not correct as the original query should be encapsulated in parenthesis. It should be:
SELECT COUNT(*) FROM (SELECT * AS num_results FROM event e GROUP BY e.type)
Do you have any suggestions on how to go around this?
Try this:
$table->setAttribute(Doctrine_Core::ATTR_QUERY_LIMIT, Doctrine_Core::LIMIT_ROWS);
http://www.doctrine-project.org/projects/orm/1.2/docs/manual/dql-doctrine-query-language/en
Related
I 've got this problem:
I have a select statement, which is rather time consuming.
I have to join the result with itself.
I want to do something like this:
Select table1.*, table2.Consumption
from (heavy select statement) table1 left outer join
(same heavy statement) table2
on table1."id" = table2."id" and table1."Year" -1 = table2."Year"
I don't want to catch the same data 2 times. I would rather like to do something like table1 table2. Is this possible?
I need this for an application, which executes querys but isn't able to use create or something like this, otherwise i would store the data in a table.
You can use a common table expression (CTE) and materialize the results of the heavy select statement:
WITH heavy AS ( SELECT /*+ MATERIALIZE */ ... (heavy select statemenet) )
Select table1.*, table2.Consumption
from heavy table1 left outer join
heavy table2
on table1."id" = table2."id" and table1."Year" -1 = table2."Year"
I can't create my view and I don't know how to fix it.
when trying to create the view it tries to add all the columns from select to the group by, which isn't what I want. and doesn't work anyways
CREATE OR REPLACE FORCE VIEW CustomerSaleHistoryView AS
Select SALE.SaleID,
SALE.SaleDate,
CUSTOMER.LastName,
CUSTOMER.FirstName,
SALE_ITEM.SaleItemID,
SALE_ITEM.ItemID,
ITEM.ItemPrice,
SUM(SALE_ITEM.ITEMPRICE),
AVG(SALE_ITEM.ITEMPRICE)
from customer
join sale on customer.CUSTOMERID = Sale.CUSTOMERID
join sale_item on sale_item.saleid = sale.saleID
join item on sale_item.itemID = item.itemID
group by CUSTOMER.LastName, CUSTOMER.FirstName, SALE.SaleID;
In order for you query to work you MUST group by any columns that are not being aggregated with a formula like SUM() or AVG(). In your case GROUP BY SALE.SaleID, SALE.SaleDate, CUSTOMER.LastName, CUSTOMER.FirstName, SALE_ITEM.SaleItemID, SALE_ITEM.ItemID, ITEM.ItemPrice
What are you trying to accomplish that you don't believe this GROUP BY is appropriate?
When using group by clause the list of columns that you are selecting should be either part of the group by clause or they should be part of an aggregate function. In your case SALE.SaleDate, SALE_ITEM.SaleItemID, SALE_ITEM.ItemID and ITEM.ItemPrice doesn't satisfy that rule. You need to include these to your group by clause. Once you fix the select statement and when it returns the desired output convert that into a view.
How to order after grouping in Rethinkdb like in sql:
select count(id) as cid, ... from x
group by y
order by cid desc
If I properly understood your question, the query you are looking for is (in JavaScript)
r.table("x").group("y").count().ungroup().orderBy("reduction")
In Python/Ruby, it would be
r.table("x").group("y").count().ungroup().order_by("reduction")
I have a table something like this:
ID|Value
01|1
02|4
03|12
01|5
02|14
03|22
01|9
02|32
02|62
01|13
03|92
I want to know how much progress have each id made (from initial or minimal value)
so in sybase I can type:
select ID, (value-min(value)) from table group by id;
ID|Value
01|0
01|4
01|8
01|12
02|0
02|10
02|28
02|58
03|0
03|10
03|80
But monetdb does not support this (I am not sure may be cz it uses SQL'99).
Group by only gives one column or may be average of other values but not the desired result.
Are there any alternative to group by in monetdb?
You can achieve this with a self join. The idea is that you build a subselect that gives you the minimum value for each id, and then join that to the original table by id.
SELECT a.id, a.value-b.min_value
FROM "table" a INNER JOIN
(SELECT id, MIN(value) AS min_value FROM "table" GROUP BY id) AS b
ON a.id = b.id;
My query
select kc.prod_id, kc.prod_actv_ts
from kit_cmpnt kc ,kit_cmpnt_stock kcs, prod p
where kc.cmpnt_cd='016'
and kcs.kit_cmpnt_nbr= kc.kit_cmpnt_nbr
and kcs.stock_id=1
and kcs.prod_id=kc.prod_id
and kcs.prod_actv_ts=kc.prod_actv_ts
and p.prod_id= kc.prod_id
and p.prod_actv_ts= kc.prod_actv_ts
and p.prod_inactv_ts is null;
I want to get a distinct combination of kc.prod_id, kc.prod_actv_ts
like distinct(kc.prod_id, kc.prod_actv_ts)
But what i am getting is a combination of repeated prod_id and prod_actv_ts
please help
I'd restructure the query as follows :
select kc.prod_id, kc.prod_actv_ts
from kit_cmpnt kc
where kc.cmpnt_cd='016'
and exists
(select 1 from kit_cmpnt_stock kcs
where kcs.stock_id=1
and kcs.kit_cmpnt_nbr= kc.kit_cmpnt_nbr
and kcs.prod_id=kc.prod_id
and kcs.prod_actv_ts=kc.prod_actv_ts)
and exists
(select 1 from prod p
where p.prod_id= kc.prod_id
and p.prod_actv_ts= kc.prod_actv_ts
and p.prod_inactv_ts is null);
General principle is that you shouldn't have something in the FROM clause unless you are taking something from it. If you aren't taking anything from it, it is a filter and should be in the WHERE clause as a subquery.
Try using select * to find the reason for the duplicates.
use distinct to filter unique combination...
eg..
select distinct(a,b,c) from table
where
some condition