Re: SQL or programming?
From: Tom Moreau (tom_at_dont.spam.me.cips.ca)
Date: 06/08/04
- Next message: Aaron [SQL Server MVP]: "Re: SQL or programming?"
- Previous message: Aaron [SQL Server MVP]: "Re: SQL or programming?"
- In reply to: Ivan Debono: "SQL or programming?"
- Next in thread: Aaron [SQL Server MVP]: "Re: SQL or programming?"
- Reply: Aaron [SQL Server MVP]: "Re: SQL or programming?"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 8 Jun 2004 10:30:05 -0400
Problem 1:
select
a.Item
, b.Cost
, b.Category
from
(
Item
, min (Cost) as Cost
from
TableB
group by
Item
) as x
join TableB as b on b.Item = x.Item
and b.Cost = x.Cost
right join TableA as a on a.Item = b.Item
It will give you output even if there are no corresponding entries in
TableB.
Problem 2:
select
a.Item
, b.Cost
, b.Category
from
(
Item
, max (Cost) as Cost
from
TableB
group by
Item
) as x
join TableB as b on b.Item = x.Item
and b.Cost = x.Cost
right join TableA as a on a.Item = b.Item
-- Tom --------------------------------------------------------------- Thomas A. Moreau, BSc, PhD, MCSE, MCDBA SQL Server MVP Columnist, SQL Server Professional Toronto, ON Canada www.pinnaclepublishing.com/sql "Ivan Debono" <ivanmdeb@hotmail.com> wrote in message news:uFRiZKWTEHA.3752@TK2MSFTNGP12.phx.gbl... Hi all, I'm no expert in SQL syntax and I would solve the following 2 problems through code. But I thought I could be better off writing a SQL statement. The problem is... I don't how to do it!! Well, here's the scenario: I have a Table A containing a list of items. Any of these items can be listed more than once in a Table B and have a different cost. Apart from the cost field in Table B, each item has a category field. So here are my problems: 1. I would like to list the items in Table A with their lowest cost which is found in Table B. 2. List the items in Table A grouping by the most category. As an example, I have Table A: Item 1 Item 2 Item 3 Item 4 Table B: Item 1, $10, Category1 Item 1, $15, Category2 Item 2, $16, Category1 Item 2, $12, Category2 Item 3, $12, Category1 Item 3, $10, Category3 Item 4, $10, Category4 For problem 1, I need to have the results: Item 1, $10, Category1 Item 2, $12, Category2 Item 3, $10, Category3 Item 4, $10, Category4 For problem 2, I need to have the results: Item 1, $10, Category1 Item 2, $16, Category1 Item 3, $12, Category1 Item 4, $10, Category4 Is it possible to achieve this?? If yes, any examples would be appreciated!!! Thanks, Ivan
- Next message: Aaron [SQL Server MVP]: "Re: SQL or programming?"
- Previous message: Aaron [SQL Server MVP]: "Re: SQL or programming?"
- In reply to: Ivan Debono: "SQL or programming?"
- Next in thread: Aaron [SQL Server MVP]: "Re: SQL or programming?"
- Reply: Aaron [SQL Server MVP]: "Re: SQL or programming?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|