|
I would normally break a query that is getting this large. It really depends upon the size of your data set and your indexes. Only testing can tell.
Given the current query you can simply add a correlated sub query to the WHERE Clause.
AND tGroup.ScheduleStamp =
(SELECT MAX(cor_tGroup.ScheduleStamp)
FROM tTester cor_tTester
INNER JOIN tResultQue cor_tResultQue
ON (cor_tTester.TesterID = cor_tResultQue.TesterID)
INNER JOIN tGroup cor_tGroup
ON (cor_tResultQue.GroupID = cor_tGroup.GroupID)
WHERE cor_tTester.TesterID = tTester.TestID)
I think I would use a memory temp table to solve the total issue (syntax not tested).
DECLARE @tbl TABLE
(
TesterID INTEGER NOT NULL,
ScheduleStamp DATETIME NOT NULL
)
--*******************************************************
-- find the correct testers and their last ScheduleStamp
--*******************************************************
INSERT INTO @tbl
(
TesterID,
ScheduleStamp
)
SELECT
tTester.TesterID,
MAX(tGroup.ScheduleStamp)
FROM tGroup
INNER JOIN tResultQue
ON (tGroup.GroupID = tResultQue.GroupID)
INNER JOIN tTester
ON (tResultQue.TesterID = tTester.TesterID)
GROUP BY TesterID
HAVING MAX(tGroup.ScheduleStamp) <= @DD
--***************************
-- final select for results
--***************************
SELECT
@tbl.ScheduleStamp,
@tbl.TesterID,
tTester.LastName,
tTester.FirstName,
tTester.WorkNo,
tTester.WorkExtension,
tTester.HomeNo,
tTester.SexID,
tTester.Birth,
tTester.RaceID,
tTester.SiteID
FROM @tbl
INNER JOIN tTester
ON (@tbl.TesterID = tTester.TesterID)
WHERE tTester.RaceID = @RaceID AND
tTester.Birth >= @LBirth AND
tTester.Birth <= @UBirth AND
tTester.SiteID = @SiteID AND
These two queries can even be put into one if you want. I find it easier to maintain this way. Testing for speed might change my mind.
|
|
|
|
|
Great! Thank you for your help....I don't do much on these boards-Is this one of those things where I can give you points? If so, let me know how-you deserve 'em.
|
|
|
|
|
Thanks for the compliment.
I don't know about any points - I just answer questions that intrigue me. I do love working with TSQL.
|
|
|
|
|
|
How do I attach .mdf and .ldf file from SQL Server 2000 to MSDE 2000 ?
Is it possible to attach on prior MSDE version such as MSDE 7.0 ?
Thanks before for your answer.
"Courage choose who will follow, Fate choose who will lead" - Lord Gunner, Septerra Core
"Press any key to continue, where's the ANY key ?" - Homer Simpsons
Drinking gives me amazing powers of insight. I can solve all the worlds problems when drunk, but can never remember the solutions in the morning. - Michael P Butler to Paul Watson on 12/08/03
|
|
|
|
|
did you try sp_attach_db ?
EXEC sp_attach_db @dbname = N'MyDB',
@filename1 = N'C:\Folder\DBData.mdf',
@filename2 = N'C:\Folder\DBLog.ldf'
|
|
|
|
|
Alternatively, you could restore a bckup of the SQL DB to the MSDE DB...have a little app that will allow that if you need it.
"Now I guess I'll sit back and watch people misinterpret what I just said......"
Christian Graus At The Soapbox
|
|
|
|
|
I want to insert a row in a table which has a PK in the first field. I make a INSERT and give parameters to the sql server for all fields except the id field which is set in the db as Autoincrement so i dont have to worry about.
ALL of this is working fine.
But now i want to work immediatly after the insert with the new id of this row, which was given to the row automaticly by the sql-server. My problem now is that i have no Idea how to get the id value into my code (sure I can make a select query questioning all the parameters which i still have from the insertcommand, and ask for the id but this way seems very complicated to me!!!)
So if any more experienced Programmer than me can help me, please!!!
10000 Thanks in advanced.
PS
Languages C#, ADO.Net, and Sql Server 2000
best regards helli
|
|
|
|
|
This works within a stored proceedure. The statement should work in ADO.NET correctly as long as you do not drop your connection or make another call between the INSERT and the SELECT.
<br />
INSERT INTO tbl(ColName1,ColName2)<br />
VALUES('One','Two')<br />
<br />
SELECT SCOPE_IDENTITY()<br />
|
|
|
|
|
Hi, guys
The below is the command I want to install MSDE2000:
setup SAPWD="123456"
But there will be a dialog appearing
SETUP [/?][/f][/i <inf src="" file="">]
...........
........... (....... is the parts I ignore)
And then installation will exit.
How can I install MSDE.
Help, please!!!!
vigorous
|
|
|
|
|
|
this is my first query on this site..
i just wanna optimize a sql query..cud anyone help me out.
This query will give the accounts which are there in table1 & not in table2.
select distinct A.account_id
from table1 A
where not exists
(
SELECT account_id from table2 B
where B.account_id = A.account_id
)
as per the show plan of this query it will do a table scan on table1..
in table2
Using Clustered Index.
Index : acct_id_indx
varun handa
|
|
|
|
|
Do you have any indexes or a primary key on table1?
If you do, do any of them have account_id as the first (or only) column in the index?
Finally, try this and see what it gets you:
SELECT DISTINCT
A.account_id
FROM
table1 A
LEFT OUTER JOIN table2 B
ON B.account_id = A.account_id
WHERE
B.account_id IS NULL
This assumes that account_id is a primary key or other non-nullable column in table2.
There are many ways this query could be written to achieve the same results, but the question as to which ones would actually work, and which one would be most efficient requires much more information than what you've provided.
Grim (aka Toby) MCDBA, MCSD, MCP+SB
|
|
|
|
|
thx for ur reply..
but this query is not functioning..
the error generated is::
""the word LEFT OUTER JOIN is not recognized parametr option
incorrect syntax near LEFT""
& this query qill always give me zero results as u r using --where B.account_id is NULL
i m using SQL Advantage
varun handa
|
|
|
|
|
Sorry, I assumed SQL Server 2000, since you didn't specify. I'm not familiar with Sybase.
the LEFT OUTER JOIN tells the query to include ALL records from A, even if they don't have a matching record in B. The B.account_id IS NULL would have caused the query to only return data from rows in A that did NOT have a matching row in B. (When there's no matching row in B, all of the B values would come back as NULL.)
But I'm not sure what the syntax is to get the same effect in Sybase.
My suggestion, then, would be to use your original query, but make sure that table2.account_id is indexed.
Grim (aka Toby) MCDBA, MCSD, MCP+SB
|
|
|
|
|
Hi there!
here is my problem:
using a couple of queries, i put 2 datatables in a dataset, now i need to do one more query on those 2 datatables. how can i do this last query without writing the datatables on disk first?
thanks in advance,
fuel2run
|
|
|
|
|
The DataTable class does have a Select method, which runs simple SELECT queries against the DataTable and returns an array of DataRows. I believe this only works with single-table queries, though, so I'm not sure how useful this will be... there's also the DataView component, but IIRC that has the same limitations; it probably just calls the Select method of its DataSource or something.
|
|
|
|
|
do you thinkthere's any way to do the query i need?
basically i don't want to write the 2 temporary tables on disk
any advice?
thanks!
fuel2run
|
|
|
|
|
What query are you doing on what tables, and what temporary tables do you think you need to create? I'm a bit unclear on exactly what you're trying to accomplish other than "run a query on tables in memory which involves creating temporary tables"...
|
|
|
|
|
i need the 2 temporary tables because i'm quering 2 times the same tables and then i'm doing a third query to cross the results
basically, i'm looking for the right bus (it's a bus timetable) to go from some origin to a destination (so i create the temp Table "from", the temp table "TO" and cross them)
it works, but it's horrible and slow
thanks
fuel2run
|
|
|
|
|
So if I've got this straight, you've got a single Buses table with a From and a To field, you're creating a From table with all the buses from some specific location and a To table with all the buses to a specific location, and you want to find the record which is in both those tables?
If that's all you want to do, why not put both conditions in one query and save yourself the trouble of creating the temp tables in the first place?
SELECT * FROM Buses WHERE [From] = ? AND [To] = ?
Or am I missing something here?
|
|
|
|
|
hi ekolis,
well, yes you are missing something, but that's why i haven' been clear enough,
the FROM and TO aren't necessarily the start end stop of the bus route, but they can be intermediate stops.
moreover i'v got a table with the sequence of stops for each bus route (every bus route has the same code and this is what i'm looking for). i'haven't got a column called "STARTS AT" nor a column "ENDS AT", i only have a column "STOPS AT".
so a condition like "WHERE [From] = ? AND [To] = ?" can't exists
thas's why i'm confused...
any advice?
thanks anyway for your suggestions and for interesting.
cheers!
fuel2run
|
|
|
|
|
So I suppose you have 3 fields: BusID, StopTime, and StopLocation, let's call them? And the primary key is BusID and StopTime? Why don't you try creating a query, call it BusSchedule, which looks like this:
SELECT * FROM BusStops WHERE BusID = ? ORDER BY StopTime
Now run this query in a foreach loop on BusID, and it will give you the bus's schedule in the standard timetable format. (Assuming StopTime is stored as a DateTime and not as a string!) Now just grab the first and last record from this query
drFirst = dtQuery.Rows[0];
drLast = dtQuery.Rows[dtQuery.Rows.Count - 1];
and compare drFirst["StopLocation"] to your desired start location and drLast["StopLocation"] to your desired end location. If both equalities hold true, then the BusID you just ran the query on is the bus you're looking for and you can break out of the foreach loop.
Is this the answer you're looking for, or do I still not have your table structure right?
|
|
|
|
|
well,
you figured out the table structure correctly, the primary key is BusID only, StopTime is a DateTime Field, but i can't use a query like this because "Origin" and "Destination" of the travel aren't necessarily the first and last stops of the bus travel.
my query would answer a question like: "i want go from, say, philadelphia to chicago leaving at 10.00am (within a range of a couple of hours: 10.00-12.00am), which bus i have to take?"
so i might take a bus stopping at philadelphia and at chicago but started somewhere else and destinated to somwhere else again.
i'm gettin' lost!
cheers!
fuel2run
|
|
|
|
|
If your primary key is only BusID, how can a single bus have multiple stops? It would need to be listed multiple times (once for each stop), but that wouldn't be allowed by the primary key...
I think I see what you're getting at with the "cross query" you wanted to use - I've never done any of those before, but it seems from what you've said that a cross query returns all records that are present in both tables you're comparing. So what you were originally doing is you had a query which returned all the buses that stopped at the source location and a query which returned all the buses that stopped at the target location? I guess what you could do then is something like this:
TimeSpan tsBest = tsBest.MaxValue;
string strBest = null;
foreach (DataRow drFrom in tblFrom.Rows)
{
// See if the bus is also in the "to" table
DataRow drTo = tblTo.Find(drFrom["BusID"]);
if (drTo != null)
{
// See if the bus stops at the from location *before* stopping
// at the to location (note: will not work if buses run past midnight!)
DateTime dtFromTime = (DateTime)drFrom["StopTime"];
DateTime dtToTime = (DateTime)drTo["StopTime"];
if (dtFromTime < dtToTime)
{
// See if bus leaves with desired range
if (dtFromTime <= dtDesiredMaxFromTime && dtFromTime >= dtDesiredMinFromTime)
{
// found a bus
// note: will not necessarily find the bus CLOSEST to the desired
// departure time, only the FIRST bus listed that is acceptable
strBest = drFrom["BusID"].ToString();
break;
}
}
}
else
{
// no bus goes here to there
strBest = null;
}
}
|
|
|
|