0

mysql:

SELECT * 
FROM (select * from songs order by artist asc) as songs2 
WHERE artist LIKE 'a%' GROUP BY artist`

mssql:

SELECT * 
FROM (select top 1000 * from songs order by artist asc) as songs2  
WHERE artist LIKE 'a%' GROUP BY artist

This works in mysql but in mssql i get an error:

Column 'songs2.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

++++++++++++++++++ now i select with this query : SELECT artist,album,song from (select top 1000 * from songs order by year asc, artist asc) as songs2 where artist like 'a%' group by artist,album,song; but it does not group by artist, i want only one artist per song available in the query

2
  • Do what the error message says and remove the clause. It doesn't really do anything (there's no guarantee that the outer query respects that order, even though it'll probably do it). Commented Jul 30, 2015 at 8:28
  • I changed the sql to Commented Jul 30, 2015 at 8:30

4 Answers 4

1

In mysql you can select values which are not in group by statement. It is not possible in mssql.

SELECT artist FROM 
(
    select * from songs 
) as songs2 
where artist like 'a%' group by artist;

And also you can not use order by in a subquery.

Sign up to request clarification or add additional context in comments.

1 Comment

actually you can use ORDER BY in a subquery when combined with TOP on sqlserver
0

This will find 1 song per artist and a maximum of 1000 songs:

SELECT TOP 1000 
  artist,album,song 
FROM 
(
  SELECT 
    artist,album,song, 
    row_number() over (partition by artist order by (SELECT 1)) rn
  FROM table_name 
  WHERE artist like 'a%' 
) as t2 
WHERE rn = 1

Comments

0

Remove the ORDER BY clause:

SELECT * FROM 
(
    select * from songs 
) as songs2 
where artist like 'a%';

There is no point to order the rows in the sub-query as you are not using TOP to get N of them, OFFSET to apply paging or FOR XML to order XML nodes.

If you want to order the results, do it in the outer query:

SELECT * FROM 
(
    select * from songs 
) as songs2 
where artist like 'a%'
order by year asc, artist asc;

So, basically, you can shorted this:

SELECT *  
from songs 
where artist like 'a%' 
order by year asc, artist asc;

2 Comments

]Column 'songs2.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
@shervinkhaleghjoo Remove the GROUP BY clause or add all columns in it.
0

Order By in subquery Require TOP/OFFSET/FOR XML Cluase

SELECT artist FROM 
(
    select top 100 percent * from songs 
    order by year asc, artist asc
) as songs2 
where artist like 'a%' group by artist;

2 Comments

]Column 'songs2.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
I want to know the song name also, only artist is not enough

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.