all groups > sql server mseq > march 2005 >
You're in the

sql server mseq

group:

A Challenging Query


A Challenging Query Kaushik
3/23/2005 10:23:01 PM
sql server mseq:
Hi EveryBody,
Here is a very interesting SQL which I failed to solve.

I have two tables called Main and Notes.Table notes has id field which is
primary key of the Main table and foreign key in notes table. Notes table has
an identity column called NotesId.

What I have to do is that I have to show all data of main table but they
will be ordered depending upon a particular value of the note field in the
notes table. What I mean is : say there is ID 100,101,102,103 existing in the
main table. They may have several entries in the notes table and some of
those entries containing say "Desired match" in their note field.

What I want : in time of selection those entries who have "Desired Match" in
their notes field they will be coming first in their chronological order.

But their are some constraints : you cannot use any group by or distinct
clause in the query. and the resultant data cannot have any duplicate rows
Here is what I tried :

select top 100 percent main.*,notes.note,notes.date
from main left join notes on main.id=notes.id
order by convert(
numeric,
case
when notes.note like('Desired%') then '100000'
else
'500'
end

) desc,notes.date asc

But problem is that this result set contains duplicate data

Any new or better Idea.

Thanks
Re: A Challenging Query Hugo Kornelis
3/24/2005 11:08:30 PM
[quoted text, click to view]
(snip)

Hi Kaushik,

I love solving SQL challenges. But I'm not quite as good at trying to
understand verbose descriptions. I'll refer you to a website that
explains what information you should include in a post in order for us
to help you: www.aspfaq.com/5006. If you follow the guidelines given
there, I'll probably be able to help you.


[quoted text, click to view]

I understand the need to eliminate duplicate rows, but the constraints
to not use GROUP BY or DISTINCT makes no sense to me. Can you explain
the reason for this restriction? Because I really don't understand why
any SQL coder would voluntarily part with part of the tools he needs to
do his job.

Best, Hugo
--

AddThis Social Bookmark Button