I am trying to add the SQL_CALC_FOUND_ROWS into a query (Please note this isn't for pagination)
please note I am trying to add this to a cakePHP query the code I currently have is below:
return $this->find('all', array(
'conditions' => $conditions,
'fields'=>array('SQL_CALC_FOUND_ROWS','Category.*','COUNT(`Entity`.`id`) as `entity_count`'),
'joins' => array('LEFT JOIN `entities` AS Entity ON `Entity`.`category_id` = `Category`.`id`'),
'group' => '`Category`.`id`',
'order' => $sort,
'limit'=>$params['limit'],
'offset'=>$params['start'],
'contain' => array('Domain' => array('fields' => array('title')))
));
Note the 'fields'=>array('SQL_CALC_FOUND_ROWS',' this obviously doesn't work as It tries to apply the SQL_CALC_FOUND_ROWS to the table e.g. SELECTCategory.SQL_CALC_FOUND_ROWS,
Is there anyway of doing this? Any help would be greatly appreciated, thanks.
I want to get all Images not used by current ItemID. The this subquery but it also always returns duplicate Images:
EDITED
select Images.ImageID, Images.ItemStatus, Images.UserName, Images.Url,
Image_Item.ItemID, Image_Item.ItemID
from Images
left join (select ImageID, ItemID, MAX(DateCreated) x
from Image_Item
where ItemID != '5a0077fe-cf86-434d-9f3b-7ff3030a1b6e'
group by ImageID, ItemID
having count(*) = 1)
image_item on Images.imageid = image_item.imageid
where ItemID is not null
I guess the problem is with the subquery which I can't avoid duplicate rows:
select ImageID, ItemID, MAX(DateCreated) x
from Image_Item
where ItemID != '5a0077fe-cf86-434d-9f3b-7ff3030a1b6e'
group by ImageID, ItemID
having count(*) = 1
Result:
F2EECBDC-963D-42A7-90B1-4F82F89A64C7 0578AC61-3C32-4A1D-812C-60A09A661E71
F2EECBDC-963D-42A7-90B1-4F82F89A64C7 9A4EC913-5AD6-4F9E-AF6D-CF4455D81C10
42BC8B1A-7430-4915-9CDA-C907CBC76D6A CB298EB9-A105-4797-985E-A370013B684F
16371C34-B861-477C-9A7C-DEB27C8F333D 44E6349B-7EBF-4C7E-B3B0-1C6E2F19992C
Table: Images
ImageID uniqueidentifier
UserName nvarchar(100)
DateCreated smalldatetime
Url nvarchar(250)
ItemStatus char(1)
Table: Image_Item
ImageID uniqueidentifier
ItemID uniqueidentifier
UserName nvarchar(100)
ItemStatus char(1)
DateCreated smalldatetime
Any kind help is highly appreciated.
Hi ,
I have created Dealer dimension in SSAS 2005 and it has 3 hierarchies. By default the hierarchy created first is the default hierarchy of the dimension.
Is there any way to change the default hierarchy to another hierarchy.
I have a this table, where I store multiple ids and an age range (def1,def2)
CREATE TABLE "template_requirements" ("_id" INTEGER NOT NULL,
"templateid" INTEGER,
"def1" VARCHAR(255),
"def2" VARCHAR(255),
PRIMARY KEY("_id"))
Having values such as:
templateid | def1 | def2
100 | 7 | 25
200 | 40 | 90
300 | 7 | 25
300 | 40 | 60
as you see for templateid 300 we have an or logic: age between 7 and 25 or age between 40 and 60.
I want to get all the template ids that are not for a certain age like 25...
What's the problem?
If I run a query like this one:
SELECT group_concat(templateid) FROM template_requirements where
and '25' not between cast(def1 as integer) and
cast(def2 as integer)
it returns 200, 300, which is wrong, as the 300 matched on row 40 to 60, but shouldn't be included in the result as we have a condition with same templateid 7 to 25 that fails the not beetween stuff.
How would be the correct query in SQLite, I would like to keep the group_concat stuff.
I have a database that contains data for many "clients". Currently, we insert tens of thousands of rows into multiple tables every so often using .Net SqlBulkCopy which causes the entire tables to be locked and inaccessible for the duration of the transaction.
As most of our business processes rely upon accessing data for only one client at a time, we would like to be able to load data for one client, while updating data for another client.
To make things more fun, all PKs, FKs and clustered indexes are on GUID columns (I am looking at changing this).
I'm looking at adding the ClientID into all tables, then partitioning on this. Would this give me the functionality I require?
I've got a monitoring system that is collecting data every n seconds (n ~=10 but varies). I'd like to aggregate the collected data by 15 minute intervals. Is there a way to corral the timestamp column into 15 minute chunks to allow for grouping to work?
I have a string of length 1,44,000 which has to be passed as a parameter to a stored procedure which is a select query on a table.
When a give this is in a query (in c# ) its working fine. But when i pass it as a parameter to stored procedure its not working.
Here is my stored procedure where in i have declared this parameter as NVARCHAR(MAX)
------------------------------------------------------
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE PROCEDURE [dbo].[ReadItemData](@ItemNames NVARCHAR(MAX),@TimeStamp as DATETIME)
AS
select * from ItemData
where ItemName in (@ItemNames) AND TimeStamp=@TimeStamp
---------------------------------------------------------------------
Here the parameter @ItemNames is a string concatinated with different names such as
'Item1','Item2','Item3'....etc.
Can anyone tell what went wrong here?
Thanks & Regards
Padma
I'm using the Repository Pattern with some LinqToSql objects. My repository objects all implement IDisposable, and the Dispose() method does only thing--calls Dispose() on the DataContext. Whenever I use a repository, I wrap it in a using person, like this:
public IEnumerable<Person> SelectPersons()
{
using (var repository = _repositorySource.GetNew<Person>(dc => dc.Person))
{
return repository.GetAll();
}
}
This method returns an IEnumerable<Person>, so if my understanding is correct, no querying of the database actually takes place until Enumerable<Person> is traversed (e.g., by converting it to a list or array or by using it in a foreach loop), as in this example:
var persons = gateway.SelectPersons();
// Dispose() is fired here
var personViewModels = (
from b in persons
select new PersonViewModel
{
Id = b.Id,
Name = b.Name,
Age = b.Age,
OrdersCount = b.Order.Count()
}).ToList(); // executes queries
In this example, Dispose() gets called immediately after setting persons, which is an IEnumerable<Person>, and that's the only time it gets called.
So, a couple questions:
How does this work? How can a disposed DataContext still query the database for results when I walk the IEnumerable<Person>?
What does Dispose() actually do?
I've heard that it is not necessary (e.g., see this question) to dispose of a DataContext, but my impression was that it's not a bad idea. Is there any reason not to dispose of it?
I'm trying to do pagination with a very old version of DB2 and the only way I could figure out selecting a range of rows was to use the OVER command.
This query provide's the correct results (the results that I want to paginate over).
select MIN(REFID) as REFID, REFGROUPID from ARMS_REFERRAL where REFERRAL_ID<>'Draft' and REFERRAL_ID not like 'Demo%' group by REFGROUPID order by REFID desc
Results:
REFID REFGROUPID
302 242
301 241
281 221
261 201
225 142
221 161
... ...
SELECT * FROM ( SELECT row_number() OVER () AS rid, MIN(REFID) AS REFID, REFGROUPID FROM arms_referral where REFERRAL_ID<>'Draft' and REFERRAL_ID not like 'Demo%' group by REFGROUPID order by REFID desc ) AS t WHERE t.rid BETWEEN 1 and 5
Results:
REFID REFGROUPID
26 12
22 11
14 8
11 7
6 4
As you can see, it does select the first five rows, but it's obviously not selecting the latest.
If I add a Order By clause to the OVER() it gets closer, but still not totally correct.
SELECT * FROM ( SELECT row_number() OVER (ORDER BY REFGROUPID desc) AS rid, MIN(REFID) AS REFID, REFGROUPID FROM arms_referral where REFERRAL_ID<>'Draft' and REFERRAL_ID not like 'Demo%' group by REFGROUPID order by REFID desc ) AS t WHERE t.rid BETWEEN 1 and 5
REFID REFGROUPID
302 242
301 241
281 221
261 201
221 161
It's really close but the 5th result isn't correct (actually the 6th result).
How do I make this query correct so it can group by a REFGROUPID and then order by the REFID?
for eg...
SELECT * FROM
( SELECT RANK() OVER (ORDER BY stud_mark DESC) AS ranking, stud_id, stud_name, stud_mark FROM tbl_student )
AS foo WHERE ranking = 10
Here foo is present...actually what it does ?..
Hi
In my mode I am selecting a field as
$query1 = $this->db->query("SELECT dPassword
FROM tbl_login
WHERE dEmailID='[email protected]'");
How to return dpassword as a variable to my controller
I tried this way return dpassword;
Hi, all.
I have a table x that's like the one bellow:
id | name | observed_value |
1 | a | 100 |
2 | b | 200 |
3 | b | 300 |
4 | a | 150 |
5 | c | 300 |
I want to make a query so that in the result set I have exactly one record for one name:
(1, a, 100)
(2, b, 200)
(5, c, 300)
If there are multiple records corresponding to a name, say 'a' in the table above, I just pick up one of them.
In my current implementation, I make a query like this:
select x.* from x ,
(select distinct name, min(observed_value) as minimum_val
from x group by name) x1
where x.name = x1.name and x.observed_value = x1.observed_value;
But I think there may be some better way around, please tell me if you know, thanks in advance.
I want to make a festival calendar using asp.net from that I used two ajax calendar and one textbox it is a festival textbox where we enter festival which FromDate and ToDate respectively. I want to do this as following point
If I enter in textbox Christmas and Choose Fromdate=25/12/2011 and ToDate=31/12/2011 then it will be valid
If I choose fromDate=25/12/2011 and ToDate=24/12/2011 then it will invalid
If I choose Fromdate=25/12/2011 and Todate=28/12/2011 then also it is invalid because it coming in between 25/12/2011 and 31/12/2011
If I Choose fromdate=1/1/2011 and ToDate=1/1/2011 then it is valid
If I choose fromdate=21/12/2011 and 25/12/2011 then it is invalid because of already Christmas done in 1/1/2011
And all date should show in gridview like 25-dec-2011 format
Here is my code:
DateTime dt1 = Convert.ToDateTime(txt_fromdate.Text);
DateTime dt2 = Convert.ToDateTime(txt_todate.Text);
if (dt1 > dt2)
{
con.Open();
com = new SqlCommand("BTNN_MovieDB_Festival_Details_Insert", con);
com.Parameters.Add("@fromdate", SqlDbType.VarChar).Value = dateformat_mmdd(txt_fromdate.Text.ToString().Trim());
com.Parameters.Add("@todate", SqlDbType.VarChar).Value = dateformat_mmdd(txt_todate.Text.ToString().Trim());
com.Parameters.Add("@return", SqlDbType.VarChar).Direction = ParameterDirection.ReturnValue;
com.ExecuteNonQuery();
con.Close();
showdata();
}
else if (dt1 < dt2)
{
lblerror.Text = "ToDate should be greater than FromDate";
}
I have select, insert, update and delete query.
if i have to write all query in same store procedure that is good for performance or i should write all query in separate store procedure?
I recently decided to crawl over the indexes on one of our most heavily used databases to see which were suboptimal. I generated the built-in Index Usage Statistics report from SSMS, and it's showing me a great deal of information that I'm unsure how to understand.
I found an article at Carpe Datum about the report, but it doesn't tell me much more than I could assume from the column titles.
In particular, the report differentiates between User activity and system activity, and I'm unsure what qualifies as each type of activity.
I assume that any query that uses a given index increases the '# of user X' columns. But what increases the system columns? building statistics?
Is there anything that depends on the user or role(s) of a user that's running the query?
I have two entities. Groups. Pools. A Group can create many pools.
So I setup my Pool table to have a GroupID foreign key.
My code:
using (entity _db = new entity()) {
Pool p = new Pool();
p.Name = "test";
p.Group.ID = "5";
_db.AddToPool(p);
}
This doesn't work. I get a null reference exception on p.Group.
How do I go about creating a new "Pool" and associating a GroupID?
how do i write a query that returns aggregate sales data for California in the past x months.
----------------------- -----------------------
| order | | customer |
|-----------------------| |-----------------------|
| orderId int | | customerId int |
| customerId int | | state varchar |
| deposit decimal | -----------------------
| orderDate date |
-----------------------
-----------------------
| orderItem |
|-----------------------|
| orderId int |
| itemId int |
| qty int |
| lineTotal decimal |
| itemPrice decimal |
-----------------------
Hi,
i have two tables
Order(id, date, note)
and
Delivery(Id, Note, Date)
I want to create a trigger that updates the date in Delivery when the date is updated in Order.
I was thinking to do something like
CREATE OR REPLACE TRIGGER your_trigger_name
BEFORE UPDATE
ON Order
DECLARE
BEGIN
UPDATE Delivery set date = ??? where id = ???
END;
How do I get the date and row id?
thanks
I am confused, I don't know what's wrong. I'm about to transfer all data from my first table to the other. Here is my code:
$getdata = mysql_query("SELECT Quantity, Description, Total FROM ordercart");
while($row = mysql_fetch_row($getdata))
{
foreach($row as $cell){
$query1 = mysql_query("INSERT INTO ordermem (Quantity, Description, Total) VALUES
($cell)",$connect);
}
mysql_free_result($getdata);
}
I get the error: Warning: mysql_fetch_row(): 5 is not a valid MySQL result resource.
I have used Microsoft.SqlServer.Smo.dll and Microsoft.SqlServer.ConnectionInfo.dll in a number of VS2008 projects but they don't appear in the VS2010 Add Reference dialog. Why did they disappear and how can I add them to my VS2010 project?