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.
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.
Re: MSSQL Group By problems
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)?
:blush: yep, corrected.
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.
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)
Quote:
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?
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.
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!).