Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9136573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:58:13+00:00 2026-06-17T08:58:13+00:00

I have a Union of two queries in a report using crosstab. The purpose

  • 0

I have a Union of two queries in a report using crosstab.
The purpose of the reports is to list the values corresponding to each date.

1st Query returns the values corresponding to a date.
2nd Query returns the remaining dates and their values.

Finally, Union of both the queries is done.
Many people might suggest to achieve this using a single query but I have been unable to do this with a single query using LEFT JOIN as it does not return the NULL values.
Even if I get that working, the GROUP BY clause that I have will separate into two rows in iReport.. NON NULLS AND NULLS.

So, the only way I can think of doing this is using a Union.
The output looks like

      01/01/2013 02/01/2013 03/01/2013 04/01/2013 05/01/2013
X     £120.00    £120.00    £120.00                         
                                       £0.00     £0.00           .

The resulting two rows is because of the GROUP BY.(rowGroup).
Date is the columnGroup.
Ideally, I would like this to be in a single row.

The query is

SELECT
   rates_Booking.date
   rates_Booking.amount,    
   booking.reference    

FROM
   rates_Booking
   LEFT JOIN booking ON booking.reference = rates_Booking.bookingReference                                                                  

   LEFT JOIN unit ON booking.apartment = unit.unit

   LEFT JOIN property ON property.property = unit.property

 WHERE
   rates_Booking.date BETWEEN $P{startDate} AND $P{endDate} 

   AND  $X{IN,property.property,buildings}
   AND  $X{IN,property.area,location}
   AND  $X{IN, unit.unit, unit}

UNION


   SELECT
    rates_Calendar.date
    0.00 as amount,
    '' as reference     

FROM
    rates_Calendar,unit


    LEFT JOIN property ON property.property = unit.property
    LEFT JOIN regions ON regions.region = property.area

    # unit to apartments
    LEFT JOIN apartments ON (apartments.unit = unit.unit)
    LEFT JOIN apartmentTypes ON (apartmentTypes.id = apartments.apartmentTypeId)


WHERE
    rates_Calendar.date BETWEEN $P{startDate} AND $P{endDate}
    AND rates_Calendar.date NOT IN (
                                       SELECT   
                                          rates_Booking.date
                                       FROM                                                                           
                                          rates_Booking                                                                             
                                          LEFT JOIN booking ON booking.reference =   rates_Booking.bookingReference                                 
                                          LEFT JOIN unit ON booking.apartment = unit.unit
                                          LEFT JOIN property ON property.property = unit.property                                                                               

                                          LEFT JOIN apartments ON (apartments.unit =  unit.unit)                                                                                                                         
                                          LEFT JOIN apartmentTypes ON (apartmentTypes.id = apartments.apartmentTypeId)

                                        WHERE                                                                            
                                           rates_Booking.date BETWEEN $P{startDate} AND $P{endDate}                                                                                                                                                         
                                           AND  $X{IN, unit.unit, unit}                                                                                                 
                                           AND  $X{IN, apartmentTypes.id,apartmentType}

                                        GROUP BY

                                           rates_Booking.date



                                            )                                                                                        



    AND $X{IN,property.property,buildings}
    AND $X{IN,property.area,location}
    AND $X{IN, unit.unit, unit}

Also, can someone suggest so that it does not need a UNION.
I know using the subquery will not result in optimal performance.
But having the WHERE conditions in the ON clauses in JOINS does not help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:58:14+00:00Added an answer on June 17, 2026 at 8:58 am
     SELECT DATE, SUM(AMOUNT)
     FROM (first query UNION second query)
     GROUP BY DATE
     ORDER BY DATE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have joined two SQL Queries using SQL UNION Operator Example: $query1 = SELECT
I have a simple stored proc with two queries joined with a union: select
I have a query that uses union to join two sub queries e.g. SELECT
I am using two queries to select the values from their respective tables, and
Let's say I have two SQL queries: INSERT INTO tableA VALUES ('', 'mike', '21')
I am using VBA to generate SQL queries in Access. I have two SQL
i have two queries being combined with a UNION ALL 1 : --Query 1
I have two queries that I'm UNION ing together such that I already know
I am using SQL Server 2008. I have two sql queries select Course from
I have a view table which is a union of two separate tables (say

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.