| Author |
Topic |
|
fmardani
Constraint Violating Yak Guru
433 Posts |
Posted - 2006-01-30 : 09:38:42
|
| Do you know how this query can be optimised please?SELECT CODOCS01.ParentID, CODOCS01.DocType, CODOCS01.FileType, CODOCS01.Status, CODOCS01.Description, CODOCS01.FileName, CODOCS01.ModifiedBy, CODOCS01.ModifiedOn, CODOCS01.IDFROM CODOCS01 CROSS JOIN RIMAST01WHERE (CODOCS01.ParentID = N'NPI00004') OR (RIMAST01.QuoteID = N'NPI00004 ') AND (CODOCS01.ParentID = RIMAST01.ClientID)GROUP BY CODOCS01.ParentID, CODOCS01.DocType, CODOCS01.FileType, CODOCS01.Status, CODOCS01.Description, CODOCS01.FileName, CODOCS01.ModifiedBy, CODOCS01.ModifiedOn, CODOCS01.IDHAVING (NOT (CODOCS01.FileName IS NULL)) OR (NOT (CODOCS01.FileName IS NULL)) |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2006-01-30 : 11:08:26
|
quote: Originally posted by fmardaniFROM CODOCS01 CROSS JOIN RIMAST01WHERE (CODOCS01.ParentID = N'NPI00004') OR (RIMAST01.QuoteID = N'NPI00004 ') AND (CODOCS01.ParentID = RIMAST01.ClientID)
Do you really mean this?I'd be amazed if AND and OR are parenthesized correctly.It should probably say:FROM CODOCS01INNER JOIN RIMAST01 ON CODOCS01.ParentID = RIMAST01.ClientIDWHERE CODOCS01.ParentID = N'NPI00004' OR RIMAST01.QuoteID = N'NPI00004' |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2006-01-30 : 12:23:02
|
I liked this bit too:quote: Originally posted by fmardaniHAVING (NOT (CODOCS01.FileName IS NULL)) OR (NOT (CODOCS01.FileName IS NULL))
Maybe SQL Server won't spot it the first time, so give it another chance? |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2006-01-30 : 12:32:28
|
So, in summary, it should probably be:SELECT DISTINCT C.ParentID, C.DocType, C.FileType, C.Status, C.Description, C.FileName, C.ModifiedBy, C.ModifiedOn, C.IDFROM CODOCS01 AS CINNER JOIN RIMAST01 AS R ON C.ParentID = R.ClientIDWHERE (C.ParentID = N'NPI00004' OR R.QuoteID = N'NPI00004') AND C.FileName IS NOT NULL If ID is a candidate key of CODOCS01 and ParentID is a foreign key referencing RIMAST01.ClientID then the DISTINCT is unnecessary too. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2006-01-30 : 13:34:20
|
quote: Originally posted by X002548 Why is the join required?You only want to find parental rows with children?
because of... RIMAST01.QuoteID = N'NPI00004' ...presumably. Why am I having this argument? It's not my code |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-01-30 : 15:05:48
|
"missed that part...I was going blind"A BlindMan will probably offer an opinion in a moment ... Kristen |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2006-01-30 : 15:12:20
|
Yeah, probably about how much he doesn't like my one character table aliases. |
 |
|
|
fmardani
Constraint Violating Yak Guru
433 Posts |
Posted - 2006-01-30 : 16:29:45
|
| Running the original query (My first post) returns four records whereas running the following query (someone suggested) returns only one.SELECT DISTINCT C.ParentID, C.DocType, C.FileType, C.Status, C.Description, C.FileName, C.ModifiedBy, C.ModifiedOn, C.IDFROM CODOCS01 AS CINNER JOIN RIMAST01 AS R ON C.ParentID = R.ClientIDWHERE (C.ParentID = N'NPI00004' OR R.QuoteID = N'NPI00004') AND C.FileName IS NOT NULL |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2006-01-30 : 16:43:44
|
| > Running the original query (My first post) returns four records whereas running the following query (someone suggested) returns only one.I'm not surprised. I did say that I didn't think the original query was returning what the person (or program) that wrote it wanted it to. So I made some assumptions and wrote something that, in the absence of any indication of what the query was supposed to do, any DDL for the tables, or the merest hint of example data, looked more plausible. It's possible I was wrong. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-01-31 : 01:30:51
|
>>A BlindMan will probably offer an opinion in a moment ... But actaully he is b lindman MadhivananFailing to plan is Planning to fail |
 |
|
|
|