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.
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,....
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.
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.
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.
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
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.
"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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With