Home All Groups Group Topic Archive Search About

Need help with sql-query using group by

Author
23 Sep 2005 9:53 AM
SteveSu
Hi!
I have a table which look like this:

Country | Ordinal | Value
  US        1        sjja
  US        2        ksjaj
  SE        1        pll
  SE        2        öäläö
  NO        2         poop
  NO        3        plål

I want the result of the query to be like this:

Country | Ordinal | Value
  US        1        sjja
  SE        1        pll
  NO        2         poop

Also get the row for each country which has the lowest ordinal.

I have tried something like this:
select country, min(ordinal) from table
group by pricelistcountry

.... but I also want to get the column Value from the row. If I add the
column Value to the select I also have to add it to the group by clause
but then I get all rows in result. Can someone please show me how to
accomplich this.

Thanks In Advance
/ Steve

Author
23 Sep 2005 10:24 AM
Erland Sommarskog
SteveSu (ssunnerb***@hotmail.com) writes:
Show quote
> I have a table which look like this:
>
> Country | Ordinal | Value
>   US        1        sjja
>   US        2        ksjaj
>   SE        1        pll
>   SE        2        öäläö
>   NO        2         poop
>   NO        3        plål
>
> I want the result of the query to be like this:
>
> Country | Ordinal | Value
>   US        1        sjja
>   SE        1        pll
>   NO        2         poop
>

SELECT a.Country, a.Ordinal, a.Value
FROM   tbl a
JOIN   (SELECT Country, Ordinal = MIN(a.Ordinal)
        FROM   tbl
        GROUP  BY Country) AS b ON a.Country = b.Country
                               AND a.Ordinal = b.Ordinal

The thing in parentheses is a *derived* table. You can think of it as a
temp table within the query, but it necessarily not materialised. In
fact, the optimizer is very good in recasting the evaluation order for
good performance. A derived table is an independent query in itself;
it does not refer to any tables elsewhere in the query.

Derived tables are extremely powerful when you work with complex queries.


--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp
Author
23 Sep 2005 10:25 AM
Herbert
Hi,
Try this..
Select Country,Ordinal,Value from Table T1
Where Ordinal=(Select Min(Ordinal) from Table T2 where T1.Country=T2.Country)

Hope this will help
--
Herbert


Show quote
"SteveSu" wrote:

> Hi!
> I have a table which look like this:
>
> Country | Ordinal | Value
>   US        1        sjja
>   US        2        ksjaj
>   SE        1        pll
>   SE        2        öäläö
>   NO        2         poop
>   NO        3        plÃ¥l
>
> I want the result of the query to be like this:
>
> Country | Ordinal | Value
>   US        1        sjja
>   SE        1        pll
>   NO        2         poop
>
> Also get the row for each country which has the lowest ordinal.
>
> I have tried something like this:
> select country, min(ordinal) from table
> group by pricelistcountry
>
> .... but I also want to get the column Value from the row. If I add the
> column Value to the select I also have to add it to the group by clause
> but then I get all rows in result. Can someone please show me how to
> accomplich this.
>
> Thanks In Advance
> / Steve
>
>
Author
23 Sep 2005 10:39 AM
Jose G. de Jesus Jr MCP, MCDBA
hi,

try this


use northwind
select p.supplierid, p.unitprice,p.productid from products p
join
(
select supplierid, min(unitprice)as unitprice from products
group by supplierid
) as p2
on p.supplierid=p2.supplierid
and p.unitprice=p2.unitprice


--
thanks,

------------------------------------
Jose de Jesus Jr. Mcp,Mcdba
Data Architect
Sykes Asia (Manila philippines)
MCP #2324787


Show quote
"SteveSu" wrote:

> Hi!
> I have a table which look like this:
>
> Country | Ordinal | Value
>   US        1        sjja
>   US        2        ksjaj
>   SE        1        pll
>   SE        2        öäläö
>   NO        2         poop
>   NO        3        plÃ¥l
>
> I want the result of the query to be like this:
>
> Country | Ordinal | Value
>   US        1        sjja
>   SE        1        pll
>   NO        2         poop
>
> Also get the row for each country which has the lowest ordinal.
>
> I have tried something like this:
> select country, min(ordinal) from table
> group by pricelistcountry
>
> .... but I also want to get the column Value from the row. If I add the
> column Value to the select I also have to add it to the group by clause
> but then I get all rows in result. Can someone please show me how to
> accomplich this.
>
> Thanks In Advance
> / Steve
>
>
Author
26 Sep 2005 5:33 AM
SteveSu
Thanks a lot!
Works fine!

AddThis Social Bookmark Button