Solved

Amend a union query, to include Having/Where statement

Posted on 2008-10-24
5
234 Views
Last Modified: 2012-05-05
Hello, I have the following code which I have attached and in the 2nd table called "ledger_current_irb_balances" there is a field called "cust_type_code"
I want to change the syntax so it doesn't pull across records with the world
*Internal* in cust_type_code field.

Can some change the code for this to work?

Thanks
SELECT 
ccs.figures AS rr, 
sum(remain_to_roll) AS remain, 
sum(month_end_debt)-sum(remain_to_roll) AS sixty_plus, 
sum(month_end_debt) AS month_end, 
sum(ninety_plus) AS over_ninety
 
FROM 
ledger_current_icoms_balances l_bal 
 
 
INNER JOIN 
 
cht_collections_stuff ccs 
ON l_bal.c_c_c = ccs.index_field
 
GROUP BY 
ccs.figures
 
 
 
UNION ALL SELECT
 
ccs.figures as rr, 
sum(remain_to_roll) AS remain, 
sum(0-remain_to_roll+month_end_debt) AS sixty_plus,
sum(month_end_debt) AS month_end, 
sum(ninety+onetwenty) AS over_ninety,
 
 
FROM
ledger_current_irb_balances l_bal
 
INNER JOIN
cht_collections_stuff ccs
ON
l_bal.c_c_c = ccs.index_field
 
 
GROUP BY
ccs.figures

Open in new window

0
Comment
Question by:ShockUK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 22794624
INNER JOIN
cht_collections_stuff ccs
ON
l_bal.c_c_c = ccs.index_field
Where 1_bal.cust_type_code= chr(42) & "Internal" & chr(42)

(I am assuming you mean "*Internal*" literally and you are not using * as a wildcard.)
0
 

Author Comment

by:ShockUK
ID: 22795361
I created the changes as below, but when I run the syntax I get the following error:

The SELECT statement includes a reserved word or an argument name that is misspelled or missing,
or the punctuation is incorrect
SELECT 
ccs.figures AS rr, 
sum(remain_to_roll) AS remain, 
sum(month_end_debt)-sum(remain_to_roll) AS sixty_plus, 
sum(month_end_debt) AS month_end, 
sum(ninety_plus) AS over_ninety
 
FROM 
ledger_current_icoms_balances l_bal 
 
 
INNER JOIN 
 
cht_collections_stuff ccs 
ON l_bal.c_c_c = ccs.index_field
 
GROUP BY 
ccs.figures
 
 
 
UNION ALL SELECT
 
ccs.figures as rr, 
sum(remain_to_roll) AS remain, 
sum(0-remain_to_roll+month_end_debt) AS sixty_plus,
sum(month_end_debt) AS month_end, 
sum(ninety+onetwenty) AS over_ninety,
 
 
FROM
ledger_current_irb_balances l_bal
 
INNER JOIN
cht_collections_stuff ccs
ON
l_bal.c_c_c = ccs.index_field
Where l_bal.cust_type_code= chr(42) & "Internal" & chr(42)
 
 
GROUP BY
ccs.figures;

Open in new window

0
 
LVL 77

Expert Comment

by:peter57r
ID: 22795869
You need to be sure that there is a space between _field and Where (lines 37 & 38)
0
 

Author Comment

by:ShockUK
ID: 22811392
100% sure there is a space there, also a carriage return
0
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
ID: 22812053
How are you running this?
0

Featured Post

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question