Giter Site home page Giter Site logo

Comments (2)

jmcneese avatar jmcneese commented on June 20, 2024

does this still happen? if so can you submit a test showing it failing?

from permissionable.

Biggersmile avatar Biggersmile commented on June 20, 2024

I assume you use MS SQL.

You problem exists because the query was written for MySQL.

Assume you have a aggregate function in your select clause.
At this moment in In MS SQL it is not allowed to have columns in the select statement which are not used in any aggegated function or
not added to the group by clause. These queries have undefined results.

And this is the whole problem in MySQL. MySQL has two modes for group by queries. By default a less strict mode is used.
This allows you to use columns in your select statement without adding these columns to your group by queries.
MySQL makes some assumptions and comes up with a result.

If you add sql_mode=ONLY_FULL_GROUP_BY to your Mysql configuation my.ini file in Windows or in a .cnf in linux/unix.
MySQL won't allow these wrong groupby queries anymore.

A link with information to this matter: http://dev.mysql.com/tech-resources/articles/debunking-group-by-myths.html

Below I noted an example:

Example data:

MS SQL
CREATE TABLE [dbo].[Tree]([id] [int] NOT NULL,
[name] [nchar]%2810%29 NOT NULL,
[leaves] [int] NOT NULL,
[type] [nchar]%2810%29 NOT NULL) ON [PRIMARY]

INSERT [dbo].[Tree]([id], [name], [leaves], [type]) VALUES (1, N'Appletree ', 10, N'Fruit ')
INSERT [dbo].[Tree]([id], [name], [leaves], [type]) VALUES (2, N'Birch ', 15, N'Needleless')
INSERT [dbo].[Tree]([id], [name], [leaves], [type]) VALUES (3, N'Oak ', 5, N'Needleless')
INSERT [dbo].[Tree]([id], [name], [leaves], [type]) VALUES (4, N'Pinetree ', 7, N'Needles ')
INSERT [dbo].[Tree]([id], [name], [leaves], [type]) VALUES (5, N'Spruce ', 3, N'Needles ')

MySQL

CREATE TABLE IF NOT EXISTS tree (
id int(11) NOT NULL,
name varchar(10) NOT NULL,
leaves int(11) NOT NULL,
type varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO tree (id, name, leaves, type) VALUES
(1, 'Appletree', 10, 'Fruit'),
(2, 'Birch', 15, 'Needleless'),
(3, 'Oak', 5, 'Needleless'),
(4, 'Pinetree', 7, 'Needles'),
(5, 'Spruce', 3, 'Needles');

Incorrect Example Query:

MS SQL

SELECT type, MAX(leaves)
FROM [dbo].[Tree]

MySQL

SELECT TYPE, MAX( leaves )
FROM Tree

Results

MS SQL

Msg 8120, Level 16, State 1, Line 1
Column 'dbo.Tree.type' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

MySQL

Type MAX(leaves)
Fruit 15

Correct Example Query:

MS SQL

SELECT type, MAX(leaves)
FROM [dbo].[Tree]
GROUp BY type

MySQL

SELECT TYPE, MAX( leaves )
FROM Tree
GROUp BY type

Results
Both queries return, as you would expect.

Fruit 10
Needleless 15
Needles 7

from permissionable.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.