Solved

Amend a union query, to include Having/Where statement

Posted on 2008-10-24
5
229 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
Comment Utility
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
Comment Utility
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
Comment Utility
You need to be sure that there is a space between _field and Where (lines 37 & 38)
0
 

Author Comment

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

Accepted Solution

by:
peter57r earned 500 total points
Comment Utility
How are you running this?
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

8 Experts available now in Live!

Get 1:1 Help Now