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

  • Home
  • SEARCH
  • 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 6792197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:56:23+00:00 2026-05-26T17:56:23+00:00

A bit of background. My company has produced an application that takes a SQL

  • 0

A bit of background. My company has produced an application that takes a SQL query, then uses the values it returns to produce graphs and K.P.I.s. In my problem, we are producing a graph to show the total amount of orders broken down by despatch “route” for a specific formatted date.

There are two tables involved, order header (ORDERS) and items (ITEMS).

The program requires two fields, XAxis and YAxis to be passed to it to produce a graph. The graph we wish to produce has the Route Reference (Text) as the XAxis and a sum of items as the YAxis.

Complications occur with the route. The route can be changed by the operations before despatch, the new route is stored in a different field; lets call these OLDROUTE and NEWROUTE. If NEWROUTE is blank then they are using the OLDROUTE. If NEWROUTE has any value then use NEWROUTE as XAxis.

The date used in our tables is an integer, in the format YYMMDD. The code made to generate it dynamically is given as

@DATE=(((year(curdate())-2000)*10000)+(month(curdate())*100)+(day(curdate()))) 

This is compared with the field DESPDATE to get all orders completed today.

I have a version that works OK, posted below

select CASE WHEN ORDERS.NEWROUTE='' then substr(ORDERS.OLDROUTE) else substr(ORDERS.NEWROUTE,1,6) end AS XAxis,  SUM(ITEMS.QUANTITY) as YAxis
FROM ORDERS
join ITEMS
on ORDERS.ORDNUM=ITEMS.ORDNUM
where 
ORDERS.DESPDATE=@DATE    
and ORDERS.STATUS='COMPLETE'
group by CASE WHEN ORDERS.NEWROUTE='' then substr(ORDERS.OLDROUTE) else substr(ORDERS.NEWROUTE,1,6) end
order by CASE WHEN ORDERS.NEWROUTE='' then substr(ORDERS.OLDROUTE) else substr(ORDERS.NEWROUTE,1,6) end

This works fine. Now the complications come when they want to use a different field for a specific route. So for example: for route (XAxis) = 'PARCEL' then want to use ORDERS.ISSUEDATE, for route 'BOX' they want to use ORDERS.PACKDATE and for everything else they want it to be the same.

So the new code would be something like:

select CASE WHEN ORDERS.NEWROUTE='' then substr(ORDERS.OLDROUTE) else substr(ORDERS.NEWROUTE,1,6) end AS XAxis,  SUM(ITEMS.QUANTITY) as YAxis
FROM ORDERS
join ITEMS
on ORDERS.ORDNUM=ITEMS.ORDNUM
where 
CASE WHEN XAxis='PARCEL' then ORDERS.ISSUEDATE=@DATE 
ELSE WHEN XAxis='BOX' then ORDERS.PACKDATE=@DATE
else ORDERS.DESPDATE=@DATE end
and ORDERS.STATUS='COMPLETE'
group by CASE WHEN ORDERS.NEWROUTE='' then substr(ORDERS.OLDROUTE) else substr(ORDERS.NEWROUTE,1,6) end
order by CASE WHEN ORDERS.NEWROUTE='' then substr(ORDERS.OLDROUTE) else substr(ORDERS.NEWROUTE,1,6) end

This does not work. IS actually possible to use different table fields in a where clase case statement? Are they asking more than we can deliver?

I have tried a similar query using IF instead of CASE but have not had any success with it.

  • 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-05-26T17:56:24+00:00Added an answer on May 26, 2026 at 5:56 pm

    Just use OR in some parenthetical filters:

    WHERE ((XAxis = 'Parcel' AND Orders.Issuedate = @DATE) OR
          (XAxis = 'Box' AND Orders.Packdate = @DATE))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A bit of background. I'm writing an application that uses UDP. The application will
A bit of background: I am currently working on an application that allows novice
For a bit of background, I'm writing a meter reading application in C for
Bit support question. Apologies for that. I have an application linked with GNU readline.
First a bit of background: I have a WPF application, which is a GUI-front-end
Background : I have a small Python application that makes life for developers releasing
As a bit of background - Windows has a facility for Touch/TabletPCs whereby it
A bit of background first: GeoModel is a library I wrote that adds very
First, a bit of background info: The HTTP 1.1 specification, circa 1999, recommends that
First of all bit of background. We are developing an application which receives messages

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.