Alvin Sim
Alvin Sim

Reputation: 338

Informix: Group By Clause Syntax Error

I have a SQL which I am trying to run in Informix but it keeps on prompting either "Syntax error" or "Column (...) cannot be found in table..".

My query is like below:

SELECT      SLS.STORE, ITM.CLASS, SUM(SLS.SLS_QTY) AS SUM_SLS_QTY, 
            SUM(SLS.SLS_COST) AS SUM_SLS_COST, SUM(SLS.SLS_SELL) AS SUM_SLS_SELL
FROM        SLSDATE SLS, ITEMMST ITM
WHERE       SLS.STORE = '1001' AND SLS.SHORT_SKU = ITM.SHORT_SKU AND 
            SLS.SALES_TRANS_DATE BETWEEN 
                TO_DATE('2011-04-01', '%Y-%m-%d') AND TO_DATE('2011-04-10', '%Y-%m-%d')
GROUP BY    SLS.STORE, ITM.CLASS
ORDER BY    SLS.STORE, ITM.CLASS;

When I run the query above, informix says "Column (class) not found in any table in the query (or SLV is undefined)".

When I changed the ITM.CLASS to ITM."CLASS" (as below), it says syntax error at the GROUP BY clause.

SELECT      SLS.STORE, ITM."CLASS", SUM(SLS.SLS_QTY) AS SUM_SLS_QTY, 
            SUM(SLS.SLS_COST) AS SUM_SLS_COST, SUM(SLS.SLS_SELL) AS SUM_SLS_SELL
FROM        SLSDATE SLS, ITEMMST ITM
WHERE       SLS.STORE = '1001' AND SLS.SHORT_SKU = ITM.SHORT_SKU AND 
            SLS.SALES_TRANS_DATE BETWEEN 
                TO_DATE('2011-04-01', '%Y-%m-%d') AND TO_DATE('2011-04-10', '%Y-%m-%d')
GROUP BY    SLS.STORE, ITM."CLASS"
                          ^ syntax error here
ORDER BY    SLS.STORE, ITM."CLASS";

Then I changed the GROUP BY clause to use numbers to represent the columns, and it was able to execute.

SELECT      SLS.STORE, ITM."CLASS", SUM(SLS.SLS_QTY) AS SUM_SLS_QTY, 
            SUM(SLS.SLS_COST) AS SUM_SLS_COST, SUM(SLS.SLS_SELL) AS SUM_SLS_SELL
FROM        SLSDATE SLS, ITEMMST ITM
WHERE       SLS.STORE = '1001' AND SLS.SHORT_SKU = ITM.SHORT_SKU AND 
            SLS.SALES_TRANS_DATE BETWEEN 
                TO_DATE('2011-04-01', '%Y-%m-%d') AND TO_DATE('2011-04-10', '%Y-%m-%d')
GROUP BY    1, 2
ORDER BY    SLS.STORE, ITM."CLASS";

At the moment I do not understand why is there problems with the GROUP BY clause.

UPDATE 1 The itemmst table schema looks something like below

create table 'informix'.itemmst (
    short_sku VARCHAR(13) not null,
    item_desc lvarchar(2000),
    item_del_cd VARCHAR(1),
    item_suppl VARCHAR(10),
    item_suppl_contract VARCHAR(10),
    div VARCHAR(3),
    dept VARCHAR(3),
    CLASS VARCHAR(6),
    ..
)

UPDATE 2 I did a change to one of WAS data source's custom properties, ifxDELIMIDENT. Originally it was blank. So, I changed it to true. Restarted WAS. And I couldn't login to our application. SQLExceptions were thrown by WAS but was not able to see the stack trace because WAS has truncated the last few lines. After changing the property back to blank, I was able to login to our application.

Upvotes: 1

Views: 3066

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753675

Informix treats both single-quotes and double-quotes are string delimiters, unless you force it to treat double quotes as marking delimited identifiers by setting the DELIMIDENT environment variable. Then strings are in single quotes and delimited identifiers are in double quotes. (You can still get away with either single or double quotes around owner names, even with DELIMIDENT set.)

That's why the "CLASS" notation is not working.

The residual problem appears to be that there isn't a column called class in the table itemmst. If you can demonstrate that it is there (by showing the schema for the itemmst table), then we have other issues. You will need to identify the version of Informix that you're using (down the the last digit after the last decimal point - so 11.50.UC9W1X1 if that is the version you have), and also the platform on which you are running it. This should be information provided automatically, though you're not the first, nor likely to be the last, to omit it.


It's hard work reverse engineering other people's schemas. But, sometimes it has to be done!

CREATE TEMP TABLE slsdate
(
    store               VARCHAR(13) NOT NULL,
    sls_qty             DECIMAL(6,0) NOT NULL,
    sls_cost            DECIMAL(10,2) NOT NULL,
    sls_sell            DECIMAL(10,2) NOT NULL,
    short_sku           VARCHAR(13) NOT NULL,
    sales_trans_date    DATE NOT NULL
);

CREATE TEMP TABLE itemmst
(
    short_sku           VARCHAR(13) NOT NULL,
    item_desc           LVARCHAR(2000),
--    item_del_cd         VARCHAR(1),
--    item_suppl          VARCHAR(10),
--    item_suppl_contract VARCHAR(10),
--    div                 VARCHAR(3),
--    dept                VARCHAR(3),
    CLASS               VARCHAR(6)
);

SELECT      SLS.STORE, ITM.CLASS, SUM(SLS.SLS_QTY) AS SUM_SLS_QTY,
            SUM(SLS.SLS_COST) AS SUM_SLS_COST, SUM(SLS.SLS_SELL) AS SUM_SLS_SELL
FROM        SLSDATE SLS, ITEMMST ITM
WHERE       SLS.STORE = '1001' AND SLS.SHORT_SKU = ITM.SHORT_SKU AND
            SLS.SALES_TRANS_DATE BETWEEN TO_DATE('2011-04-01', '%Y-%m-%d')
                                     AND TO_DATE('2011-04-10', '%Y-%m-%d')
GROUP BY    SLS.STORE, ITM.CLASS
ORDER BY    SLS.STORE, ITM.CLASS;

When I run that against IDS 11.70.FC2 on MacOS X 10.7.2 (using SQLCMD 87.02 and CSDK 3.70.FC2), I get no error (no data either, but then I didn't insert any into either table).

Which language are you using to get this SQL to the database?

Upvotes: 1

Related Questions