MS Version of This MySQL View with GROUP BY?

筅森魡賤 提交于 2019-12-12 02:32:43

问题


I was shown the ease that one can make a view from multiple tables, GROUPing BY an id of one of the tables in xception's awesome answer here: CREATE VIEW WHERE SELECTid = VIEWrowID

Is there any way to do that in MS? Everywhere, I've read says "no", but no page gives an alternative.

I don't need the counts or anything, just multiple columns from multiple tables GROUPed BY(?) a single column on one table.

Thanks a lot in advance!

EXAMPLE

Thank-you for responding.

For the view's SELECT:

SELECT dbo.table1.column1 AS table1column1,
       dbo.table1.column2 AS table1column2,
       dbo.table2.column1 AS table2column1,
       dbo.table2.column2 AS table2column2
FROM table1, table2
WHERE table2.column1 = table1.column1
GROUP BY table1.column1

回答1:


As MySQL simply picks a random value from the non-grouped columns, the following should do it:

SELECT dbo.table1.column1 AS table1column1,
       min(dbo.table1.column2) AS table1column2,
       min(dbo.table2.column1) AS table2column1,
       min(dbo.table2.column2) AS table2column2
FROM table1, table2
WHERE table2.column1 = table1.column1
GROUP BY table1.column1

I highly recommend you read this blog posting http://rpbouman.blogspot.de/2007/05/debunking-group-by-myths.html to understand what MySQL is doing (wrong)



来源:https://stackoverflow.com/questions/13239820/ms-version-of-this-mysql-view-with-group-by

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!