Search code examples
sqloracleconcatenationunion

SQL - Error Code "query block has incorrect number of result columns"


I try to make a timetable for all appointments in March and April 2030 of the course "xy". I have some problems with getting this started. My SQL-Code so far is

SELECT  abhaltung.persnr  , abhaltung.raum_id , abhaltung.tag , abhaltung.lvanr, LVA.name  
from abhaltung, lva 
WHERE ((abhaltung.tag BETWEEN TO_DATE  ('2030/03/01','yyyy/mm/dd')AND TO_DATE('2030/04/30','yyyy/mm/dd'))
AND lva.name like 'xy')
UNION
SELECT abhaltung.to_hour|| ' : '|| abhaltung.to_minute as "BIS" from abhaltung
UNION
SELECT abhaltung.from_hour|| ' : '|| abhaltung.from_minute as "VON" from abhaltung

I run into the Problem that I Recieve " 01789. 00000 - "query block has an incorrect number of result columns""

I know it might be that the renaming ad too many columns but I am unable to fix this somehow

edited (renamed and some further clarification)

my output should look like

from to 10:15 11:00 09:30 10:00

however, the timestamps minutes and hours are in separate tabs they look something like that

from_hour from_minute to_hour to_minute 10 15 11 00 09 30 10 00

this is why I want to merge them.


Solution

  • It appears that what you're trying to do is compute the BIS and VON times and include them to each row. In this case you don't need to use a UNION, you just need to add the calculations to your SELECT:

    SELECT  a.persnr,
            a.raum_id,
            a.tag,
            a.lvanr,
            l.name,
            a.to_hour   || ' : '|| a.to_minute as BIS,
            a.from_hour || ' : '|| a.from_minute as VON
    from abhaltung a,
         lva l
    WHERE a.tag BETWEEN TO_DATE('2030/03/01', 'yyyy/mm/dd')
                    AND TO_DATE('2030/04/30', 'yyyy/mm/dd') AND
          l.name like 'xy')
    

    I've also shown how to use table aliases (a and l) to help simplify the code.