Hi,
I have the following two MySQL tables
TABLE NAMES
NAME_ID NAME
1 name1
2 name2
3 name3
TABLE STATUS
STATUS_ID NAME_ID TIMESTAMP
1 1 2010-12-20 12:00
2 2 2010-12-20 10:00
3 3 2010-12-20 10:30
4 3 2010-12-20 14:00
I would like to select all info from table NAMES and add most recent correspondent TIMESTAMP column from table STATUS
RESULT
NAME_ID NAME TIMESTAMP
1 name1 2010-12-20 12:00
2 name2 2010-12-20 10:00
3 name3 2010-12-20 14:00
Am stuck on this one.
How do I left join only on the newer timestamp?
Hi,
I have a big database (~4GB), with 2 large tables (~3M records) having ~180K SELECTs/hour, ~2k UPDATEs/hour and ~1k INSERTs+DELETEs/hour.
What would be the best practice to guarantee no locks for the reading tasks while inserting/updating/deleting?
I was thinking about using a NOLOCK hint, but there is so much discussed about this (is good, is bad, it depends) that I'm a bit lost. I must say I've tried this in a dev environment and I didn't find any problems, but I don't want to put it on production until I get some feedback...
Thank you!
Luiggi
This is an extremely common situation, so I'm expecting a good solution. Basically we need to update counters in our tables. As an example a web page visit:
Web_Page
--------
Id
Url
Visit_Count
So in hibernate, we might have this code:
webPage.setVisitCount(webPage.getVisitCount()+1);
The problem there is reads in mysql by default don't pay attention to transactions. So a highly trafficked webpage will have inaccurate counts.
The way I'm used to doing this type of thing is simply call:
update Web_Page set Visit_Count=Visit_Count+1 where Id=12345;
I guess my question is, how do I do that in Hibernate? And secondly, how can I do an update like this in Hibernate which is a bit more complex?
update Web_Page wp set wp.Visit_Count=(select stats.Visits from Statistics stats where stats.Web_Page_Id=wp.Id) + 1 where Id=12345;
Have a LinqtoSql query that I now want to precompile.
var unorderedc =
from insp in sq.Inspections
where insp.TestTimeStamp > dStartTime && insp.TestTimeStamp < dEndTime
&& insp.Model == "EP" && insp.TestResults != "P"
group insp by new { insp.TestResults, insp.FailStep } into grp
select new
{
FailedCount = (grp.Key.TestResults == "F" ? grp.Count() : 0),
CancelCount = (grp.Key.TestResults == "C" ? grp.Count() : 0),
grp.Key.TestResults,
grp.Key.FailStep,
PercentFailed = Convert.ToDecimal(1.0 * grp.Count() / tcount * 100)
};
I have created this delegate:
public static readonly Funct<SQLDataDataContext, int, string, string, DateTime, DateTime, IQueryable<CalcFailedTestResult>>
GetInspData = CompiledQuery.Compile((SQLDataDataContext sq, int tcount, string strModel, string strTest, DateTime dStartTime,
DateTime dEndTime, IQueryable<CalcFailedTestResult> CalcFailed) =>
from insp in sq.Inspections
where insp.TestTimeStamp > dStartTime && insp.TestTimeStamp < dEndTime
&& insp.Model == strModel && insp.TestResults != strTest
group insp by new { insp.TestResults, insp.FailStep } into grp
select new
{
FailedCount = (grp.Key.TestResults == "F" ? grp.Count() : 0),
CancelCount = (grp.Key.TestResults == "C" ? grp.Count() : 0),
grp.Key.TestResults,
grp.Key.FailStep,
PercentFailed = Convert.ToDecimal(1.0 * grp.Count() / tcount * 100)
});
The syntax error is on the CompileQuery.Compile() statement
It appears to be related to the use of the select new {} syntax.
In other pre-compiled queries I have written I have had to just use the select projection by it self. In this case I need to perform the grp.count() and the immediate if logic.
I have searched SO and other references but cannot find the answer.
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 have run into a bottleneck when trying to update one of my tables.
The player table has, among other things, id, skill, school, weight.
What I am trying to do is:
SELECT id, skill
FROM player
WHERE player.school = (current school of 4500)
AND player.weight = (current weight of 14)
to find the highest skill of all players returned from the query
UPDATE player
SET starter = 'TRUE'
WHERE id = (highest skill)
move to next weight and repeat
when all weights have been completed
move to next school and start over
all schools completed, done
I have this code implemented and it works, but I have approximately 4500 schools totaling 172000 players and the way I have it now, it would take probably a half hour or more to complete (did not wait it out), which is way too slow.
How to speed this up? Short of reducing the scale of the system, I am willing to do anything that gets the intended result.
Thanks!
*the weights are the standard folk style wrestling weights
ie, 103, 113, 120, 126, 132, 138, 145, 152, 160, 170, 182, 195, 220, 285 pounds
Below is a sample table:
fruits
+-------+---------+
| id | type |
+-------+---------+
| 1 | apple |
| 2 | orange |
| 3 | banana |
| 4 | apple |
| 5 | apple |
| 6 | apple |
| 7 | orange |
| 8 | apple |
| 9 | apple |
| 10 | banana |
+-------+---------+
Following are the two queries of interest:
SELECT * FROM fruits WHERE type='apple' LIMIT 2;
SELECT COUNT(*) AS total FROM fruits WHERE type='apple'; // output 6
I want to combine these two queries so that the results looks like this:
+-------+---------+---------+
| id | type | total |
+-------+---------+---------+
| 1 | apple | 6 |
| 4 | apple | 6 |
+-------+---------+---------+
The output has to be limited to 2 records but it should also contain the total number of records of the type apple.
How can this be done with 1 query?
INFORMIX-SE with ISQL 7.3:
I have separate tables for Loan, Purchase & Sales transactions. Each tables rows are
joined to their respective customer rows by:
customer.id [serial] = loan.foreign_id [integer];
= purchase.foreign_id [integer];
= sale.foreign_id [integer];
I would like to consolidate the three tables into one table called "transaction",
where a column "transaction.trx_type" [char(1)] {L=Loan, P=Purchase, S=Sale} identifies
the transaction type. Is this a good idea or is it better to keep them in separate tables?
Storage space is not a concern, I think it would be easier programming & user=wise to
have all types of transactions under one table.
i have maintained two datas into my table
1 column 2 column
PaidDate validitydate
in padidate ill give insert todaydate. but in validity date i may either insert validity for 1 week/1 month.
I have used validity=DATEADD(Day,7,@paiddate) to insert validity for 1 week. but how to gtet the validity for 1 month from todays date
Hello I have a database for a certain record where it needs to store a 1 or a 0 for each day of the week. So which one would be better? Bitshifting each bit into an integer and just having an integer in the database named days or should we make all of them separate boolean values so to have sunday, monday, tuesday... columns?
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?
Hello All,
I dont know how to copy table data from
one server - database - table
TO
other sever - database - table.
If it is with in the same server and different databases, i have used the following
SELECT * INTO DB1..TBL1 FROM DB2..TBL1 (to copy with table structure and data)
INSERT INTO DB1..TBL1(F1, F2) SELECT F1, F2 FROM DB2..TBL1 (copy only data)
Now my question is copy the data from SERVER1 -- DB1-- TBL1 to SERVER2 -- DB2 --TBL2
Thanks in advance!
I am preparing a chart which will display the number of orders placed for a particular day in the current month and year. I wanted the count of orders placed for each day.
I am showing the count of orders on the y-axis and the day on the x-axis.
In my database, there is table called "order" in which order data is placed: order date, user_id, order_price, etc. For example, if on 4 July, 10 orders are placed, on 5 july, 20 orders are placed, and so on.
How can I get the count of orders placed for day of the current month?
I have 2 tables:
Table A: code | name
Table B: barcode | name
Table B has full barcode and name, Table A has only code.
I need to run update query that fill name in Table A.
I tried something like:
update A set name = (select top 1 Name from B where B.Code = mid(A.Barcode,1,8))
but it doesn't work.
Hello,
I've a system, that have two types of users (Companies and individuals).all types have a shared set of properties but they differ in another. What is the best design merge all in one table that allows null for unmatched properties, or separate them in two tables related to a basic table with a one to one relationship.
Thanks.
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'm working for a university project, and I have the following question:
I have 2 tables in a Oracle DB... I need to select those rows from table1, which are not included in table2... But the main problem is that I need to exclude that rows from table2 wich was selected once... For example:
Table1 Table2 ResultTable
id | Number | Letter id | Number | Letter id | Number | Letter
_____________________ _____________________ _____________________
1 4 S 1 6 G 2 2 P
2 2 P 2 8 B 3 5 B
3 5 B 3 4 S 4 4 S
4 4 S 4 1 A 6 2 P
5 1 A 5 1 H
6 2 P 6 2 X
So, how you see it, if one row from Table1 has a "twin" in Table2, they both are excluded.
Hope I was explicit enough. Sorry for my bad English. Thanks.
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
Im using c# windows form application. I have a database with many tables. Each table has several columns. I need to populate the combo box with the column names for a selected table.
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;