Solved

Amend a union query, to include Having/Where statement

Posted on 2008-10-24
5
231 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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…
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

821 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