|
The analysis of the execution plan is far too much to reply in a forum, the MSDN documentation gives all the information you need.
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
I think you may have the wrong handle on what to use the execution plan for, it is not for timing parts of a query, more for identifying high cost points of your query.
Do study the MSDN stuff if you need more detailed information but I look for the highest % nodes and make sure they are for index scans. This is a VERY crude use of the tool but a good first step.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I agree. I typically use the execution plan to make sure I can reduce the number of I/O's; no full table scans ... It has been my experience that if you reduce (or limit) the I/O's your application will respond quickly.
Regarding, timing of SQL: Why don't you build your own timer logic and try various SQL statements out? Keep it simple, something like this:
Begin timer1
Execute SQL1
End timer1
Begin timer2
Execute SQL2
End timer2
Now check which timer is the smallest. You will have to collect these statistics multiple times to ensure that you are not getting skewed results because the data happens to be in cache.
|
|
|
|
|
I have table FEE,There are 9 rows in this Fee table
PK ........ FEE ........... TRANS. ID
11 ........ 5000$ ........ 2222
12 ........ 9000$ ........ 2222
13 ........ 1000$ ........ 2222
14 ........ 2000$ ........ 7777
15 ........ 1000$ ........ 7777
16 ........ 1000$ ........ 7777
17 ........ 2500$ ........ 9999
18 ........ 4100$ ........ 9999
19 ........ 4500$ ........ 9999
I want to fetch the only one primary key of each
duplicate 'trans. id'
For example: The result(PK) should be
11,14,17
OR
13,14,17
OR
14,14,17
......
......
Duplicate Trans. id are 2222,7777,9999.
|
|
|
|
|
try this
select min(PK) from FEE
where Trans.ID in (select distinct Trans.ID from FEE)
group by Trans.ID
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
Hi Ashfield
First of all thankz for your reply
It returns only one PK....I want to get
one PK of each duplicate record ....
According to last example I want one PK of 2222(Trans. ID),one PK of 7777(Trans. ID) and one PK of 9999(Trans. ID)
so one possible result is
11,14,17
Other possible result is
12,14,17
One more possible result is
13,14,17
.........
Now I think you understand what I want
waiting your reply
|
|
|
|
|
Thats odd. I used this:
create table #b1(pk int identity, refid int)
insert into #b1 (refid)
select 1
union all
select 2
union all
select 3
union all
select 1
union all
select 2
union all
select 3
select min(PK) from #b1 where refid in (select distinct refid from #b1)group by refid
and it gave me 1, 2 and 3. I'm using sql server 2005, what are you using?
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
I am using SQLServer 2005
|
|
|
|
|
So does my test script work for you?
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
select min(PK),Trans.ID from FEE
group by Trans.ID
try this
|
|
|
|
|
Why do you need the WHERE clause? Can't you just do:
select TransId, min(PK) from FEE group by TransId
|
|
|
|
|
You can if you also want the transid (which I suspect is the case), but the OP just wanted the PK
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
Hi guys,
I'm struggling with something conceptually that maybe somebody would be able to help me with. I'm writing an application to match vehicles in one database to vehicle in another database. As you can imagine there are difference all over the place so there will be a lot of manual matching to be done but I'm doing my best to cut down on the amount of manual work where possible. The LIKE predicate has definitely been my friend here, but how can I make it work the other way around, i.e. instead of C_Model LIKE '%Fiesta%' I could do with 'Fiesta' LIKE %C_Model%. If you know what I mean?
For instance I can match this:
DB1 DB2
'Fiesta' -> 'Fiesta 1'
-> 'Fiesta 2'
-> 'Fiesta 3'
...but I don't know how to go this way:
DB1 DB2
'306 -97' -> '306'
'306 97-' -> '306'
Cheers,
Chris Chambers.
|
|
|
|
|
You may now get ready to kick yourself....
You have no problem getting DB2 like 'Fietsa%'
what is wrong with getting Db1 like '306%'
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Yeah, maybe I should have explained myself better.
Take this statement from the second databases DataSet class:
return (ModelsRow[])this.Models.Select("C_Make = '" + make + "' AND C_Model LIKE '%" + model + "%'");
Is there an expression I can use to try to further enhance this expression to help me retrieve the records where C_Model is like the model passed in? If '306 -97' is passed in and the models table contains '306' rows how can I get to those rows? Like I say, it's fine if I pass in 'Fiesta' and the models table contains 'Fiesta 1', 'Fiesta 2', 'Fiesta 3' etc.
The more I think about this the more I'm beginning to think it's not possible. I just don't see how I can select rows where a certain field matches a substring of a literal string.
|
|
|
|
|
return (ModelsRow[])this.Models.Select("C_Make = '" + make + "' AND C_Model LIKE '%" + model + "%' or C_Model like '306%'");
This answer is too simple I must be missing something in your question.
I use stored procs for 99.9% of my database work and therefore can design the SQL to do exactly what I want in QA/EM. I suggest you do this to get the result you want and then move it to a string if that is where you want to have you SQL. Also look into parameterised queries, you current model is subject to sql injection.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi Mycroft,
Mycroft Holmes wrote: return (ModelsRow[])this.Models.Select("C_Make = '" + make + "' AND C_Model LIKE '%" + model + "%' or C_Model like '306%'");
This won't work because the '306' is what's actually in C_Model; at the point this statement is executed I don't actually know what that is. I just used that '306' string as a typical example.
To be honest, the more I've thought about this the more I've decided it's a stupid thing to do in the context of my application anyway so I've abandoned the idea for now. I'm sorry if I've wasted your time. Many thanks for your help anyway.
Thanks again,
Chris Chambers.
|
|
|
|
|
Not a problem
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I'm not sure if I understand your question. I think this will do what you want:
select * from Models
where C_Model like '%306 -97%'
or '306 -97' like C_Model + '%'
It won't be the fastest query in the world, but it should do what you want. It will pick out models that have 306 -97 anywhere in the model number, (e.g. '4306 -97') and it will also get all the rows with model numbers '306'. If there are any rows with model number '3' or '30' it will get those too, but that's what you seem to be asking for.
|
|
|
|
|
Hi David, thanks for your input.
I might try that to see if it works in my application, however as you rightly said it's likely to pick up quite a few entries that I don't want so as I said to Mycroft I think I've decided not to go ahead with it. I'll let you know if that works though.
Regards,
Chris Chambers.
|
|
|
|
|
I think Mycroft is right.
And if all else fails, you could still use substrings and go for an exact match of the first N characters.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Thanks for your input Luc, much appreciated.
Regards,
Chris Chambers.
|
|
|
|
|
Id Credit Debit CurrentCredit Current Debit
51 21500 0 21500 0
52 0 21500 0 0
89 0 2150 0 2150
93 0 2140 0 4290
128 2140.5 0 0 2149.5
129 2151.5 0 2 0
171 0 1628 0 1626
190 1628 0 2 0
i have column of credit and debit and want to get columns of current credit and current debit
example:
51. credit=21500 and debit=0
i have credit more than debit so this increase credit and decrease debit
then
current credit = 0+21500
52. credit=0 and debit=21500
i have debit more than credit so this decrease credit and increase debit
then current credit =0 and current debit =0
89 credit=0 and debit =2150
i have debit more than credit so this decrease credit and increase debit
then current credit =0 and current debit=0+2150=2150
93 credit=0 and debit =2140
i have debit more than credit so this decrease credit and increase debit
then current credit =0 and current debit=2140+2150= 4290
and so on...
|
|
|
|
|
That's very interesting. Is there a question in there?
|
|
|
|
|
Are you just looking for this?
SELECT SUM(CurrentCredit) - SUM(CurrentDebit) AS FinalBalance FROM myTable
|
|
|
|