Solved

Amend a union query, to include Having/Where statement

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help writing a query 6 71
MS SQL Inner Join - Multiple Join Parameters 2 18
Make query more efficient 1 16
Update data using formula 22 20
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now