Results 1 to 9 of 9

Thread: MSSQL Group By problems

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    9

    MSSQL Group By problems

    I am having an SQL problem.

    Example Data;
    name shop price description
    XXXX 247elec 1000 a
    XXXX argos 1200 aa
    XXXX comet 1300 aaa
    XXXX doitall 1400 aaaa

    Basicly i want to creata a view that will give me

    name shop price description
    XXXX 247elec 1000 aaaa

    This is
    Group by name
    Min of price
    MAX length description
    corresponding shop to min price

    In reality i want to be able to GROUP BY name (coming from a join) select the MIN(price) all the corresponding columns to the MIN(price) AND The longest Description from a feed

    Any help will be very much appreciated as it has had me stumped now for about a day.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MSSQL Group By problems

    That's an awkward set of requirements, and I dont think it's possible with a Group By. You can use a sub-query to get most of it tho:
    Code:
    SELECT [name], shop, price, [description]
    FROM tableA t1
    WHERE price = (
      SELECT Min(price) 
      FROM tableA t2
      WHERE t2.[name] = t1.[name])
    ..this will get everything you want, except that the description will come from the record with the lowest price.
    Last edited by si_the_geek; Sep 6th, 2006 at 07:09 AM. Reason: oops.. corrected as below!

  3. #3
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: MSSQL Group By problems

    Not sure if I'm slightly missunderstanding what you're doing there Si but shouldn't that inner select be Select Min(Price)?

    That raises a potential issue as well. What happens if there are 2 shops with the minimum price - which would you want returned? Si's query will return both as two separate lines.

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    9

    Re: MSSQL Group By problems

    CHeers, Ill give it a go

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MSSQL Group By problems

    Quote Originally Posted by FunkyDexter
    Not sure if I'm slightly missunderstanding what you're doing there Si but shouldn't that inner select be Select Min(Price)?
    yep, corrected.

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    9

    Re: MSSQL Group By problems

    Thank you Very much for the help guys,

    This is producing Results

    SELECT [myname], advertiser, searchprice, [description]
    FROM dbo.FeedJOINmodelNoTable t1
    WHERE searchprice = (
    SELECT Min(searchprice)
    FROM dbo.FeedJOINmodelNoTable t2
    WHERE t2.[myname] = t1.[myname])

    BUT it is not quite as i want it.
    There are duplicated rows in 'myname' but with different details in description
    and I am not selecting the longest description. I would also like to add a WHERE price > 0

    I dont know enough about MS SQL to know how possible what i want is.

    At the moment i am trying to build this database in MSSQL having built a working version in Access but it seems every step i make in the right direction i am finding 3 new problems that totaly baffle me.

    Also i can see where improvments to the access version should be made but again i am having more trouble than i expected as i am a total novice at MSSQL.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MSSQL Group By problems

    I know about the description issue, but can't think how to deal with it appropriately.

    Here's the Price > 0 bit:
    Code:
    SELECT myname, advertiser, searchprice, [description]
    FROM dbo.FeedJOINmodelNoTable t1
    WHERE searchprice = (
        SELECT Min(searchprice)
        FROM dbo.FeedJOINmodelNoTable t2
        WHERE t2.myname = t1.myname
        AND searchprice > 0)
    There are duplicated rows in 'myname' but with different details in description
    Can you show us an example of the data? (some rows from the original table, and the output rows that relate to them).


    If you have this same query working in Access, can you show us the SQL from that?

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    9

    Re: MSSQL Group By problems

    In access as i said it was not ideal.

    There is a join between the feed and the myproducts table where %feed.name% LIKE mytable.modelno.

    This gives me a big table with all the info from mytable repeated every time there is acorresponding product in the feed.

    This Join table is an "access make-table query" so (SELECT INTO) and this sorted the grouped results by price asc eg.

    name shop price Description
    XXX 247 100 bla bla bla
    XXX emd 110 bla bla bla bla
    XXX M&M 120 bla bla bla bla bla
    YYY 247 100 bla bla bla
    YYY emd 110 bla bla bla bla
    YYY M&M 120 bla bla bla bla bla

    The way i got the end result was not very good and I never managed to get the longest Description.

    I did a GROUP BY myproduct Table and got MIN.price and FIRST of everything else in access, a function not supported by MSSQL.


    What i am thinking of now is to create multiple Views to pull the individual things i want one at a time and then use an identifier to pull the results from how ever many views it takes back together to create the table of data i am looking for.

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: MSSQL Group By problems

    This sub-query will get the max length item - here's a test of it:

    Code:
    Declare @TestTbl Table (IdVal int, Descr varchar(100))
    Insert into @TestTbl Values (1,'a')
    Insert into @TestTbl Values (2,'ab')
    Insert into @TestTbl Values (3,'abcd')
    Insert into @TestTbl Values (4,'abc')
    Insert into @TestTbl Values (5,'abcde')
    Insert into @TestTbl Values (6,'abcdefg')
    Insert into @TestTbl Values (7,'abcdef')
    select * from @testtbl where len(descr)=(select max(len(descr)) from @testtbl)
    returns...

    Code:
    IdVal       Descr                                                                                                
    ----------- ---------------------------------------------------------------------------------------------------- 
    6           abcdefg
    
    (1 row(s) affected)
    You probably want it to be a "SELECT TOP 1" so that it won't ever return two rows (which sub-queries don't like!).

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width