Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a constant value column in the group by clause

Netezza sql is giving error on this query:Cause: Invalid column name 'dummy'.

 select col1,col2, '' as dummy, max(col3) from table1  group by col1,col2,dummy

If i remove the dummy from the group by clause, it works fine. But as per sql syntax, I am supposed to include all non aggregate columns in group by.

like image 929
Victor Avatar asked Feb 07 '14 20:02

Victor


People also ask

How do I add a constant to a column in SQL?

You can add static value when you use INSERT INTO SELECT MySQL query. Write the value directly in the select statement or you can add with the help of variable which initializes the value. SET @yourVariableName − = yourstaticValue; INSERT INTO yourSecondTableName(yourColumnName1,yourColumnName2,....

What type of column should be placed in a GROUP BY clause?

If you specify the GROUP BY clause, columns referenced must be all the columns in the SELECT clause that do not contain an aggregate function. These columns can either be the column, an expression, or the ordinal number in the column list.

Is GROUP BY clause use for creating group of same column values?

The Group By statement is used to group together any rows of a column with the same value stored in them, based on a function specified in the statement. Generally, these functions are one of the aggregate functions such as MAX() and SUM(). This statement is used with the SELECT command in SQL.

How do you add condition to GROUP BY?

Syntax: SELECT column1, function_name(column2) FROM table_name WHERE condition GROUP BY column1, column2 HAVING condition ORDER BY column1, column2; function_name: Name of the function used for example, SUM() , AVG(). table_name: Name of the table. condition: Condition used.


3 Answers

why do you need it in your group by, you can use an aggregate function and its result would always be right because the value is constant for example:

select col1,col2, min(' ') as dummy, max(col3) from table1  group by col1,col2
like image 199
Maryam Arshi Avatar answered Oct 12 '22 23:10

Maryam Arshi


It's due to the order of operations...

FROM JOIN WHERE GROUP BY ... SELECT

When using group by, only the fields remaining from the previous step are available. Since you are not declaring your "Dummy" column until the Select statement the group by doesn't know it exists and therefore doesn't need to account for it.

like image 34
4Logik Avatar answered Oct 12 '22 23:10

4Logik


"dummy" is a static column (not in the table), so it does not need to be in the group by because it is an external column.

SELECT col1,
       col2,
       cast(5 as int) AS [dummy],
       max(col3)
FROM test_1
GROUP BY col1,
         col2,
         col3,
        'dummy'

The code produces an outer reference error # 164.

Take a look at these links
http://www.sql-server-helper.com/error-messages/msg-164.aspx

http://www.sql-server-helper.com/error-messages/msg-1-500.aspx

like image 42
Fewless2019 Avatar answered Oct 13 '22 01:10

Fewless2019