so basically there is 1 question and 1 problem:
1. question - when I have 100 columns in a table (and no key or uindex is given) and I want to join this table or sub-select it, do I really need to write each column name?
2. problem - the example below shows question 1. and my actual problem with the SQL statement
Example:
A.FIELD1, (SELECT CASE WHEN B.FIELD2 = 1 THEN B.FIELD3 ELSE null FROM TABLE B WHERE A.* = B.*) AS CASEFIELD1 (SELECT CASE WHEN B.FIELD2 = 2 THEN B.FIELD4 ELSE null FROM TABLE B WHERE A.* = B.*) AS CASEFIELD2 FROM TABLE A GROUP BY A.FIELD1
The story is this: if I do not put CASE in my own select statement, then I have to put the actual rowname in GROUP BY, and GROUP BY does not group the NULL value from CASE, but the actual value from the string. And because of this, I will either have to join, or select all the columns, since there is no key and no uindex, or somehow find another solution.
DBServer is DB2.
So now, to describe it only with words and without SQL: I have “order items” that can be divided into “ZD” and “EK” (1 = ZD, 2 = EK) and can be grouped by a “distributor”. Even if the "order items" can have one of two different "departments" (ZD, EK), the fields / lines for "ZD" and "EK" are always filled. I need a grouping to consider a "department", and only if the changed "department" (ZD or EK) changes, then I want to create a new group.
SELECT (CASE WHEN TABLE.DEPARTEMENT = 1 THEN TABLE.ZD ELSE null END) AS ZD, (CASE WHEN TABLE.DEPARTEMENT = 2 THEN TABLE.EK ELSE null END) AS EK, TABLE.DISTRIBUTOR, sum(TABLE.SOMETHING) AS SOMETHING, FROM TABLE GROUP BY ZD EK TABLE.DISTRIBUTOR TABLE.DEPARTEMENT
This worked in SELECT and ZD, EK in GROUP BY. The only problem was that even if the EK was not assigned DEPARTEMENT, it still opened a new group if it changed because it used the real value of EK and not NULL from CASE, as I already explained up.