|
That's not going to work...
Consider
Customer
Cusnr
1
Sales
Cusnr Cat Amt
1 A 5.00
1 B 10.00
Credits
1 D -1.00
1 E -0.50
The intermediate table (before aggregation) will be
CUSNR CUSNR CAT AMT CUSNR CAT
AMT
1 1 A 5.00 1 D
1.00-
1 1 A 5.00 1 E
.50-
1 1 B 10.00 1 D
1.00-
1 1 B 10.00 1 E
.50-
basically cross joining between sales & credits since you're joining on
just customer...
We need to add a CATEGORY_MASTER
Then our basic statement ends up with
select *
from cust cst
cross join CATEGORY_MASTER cat
left outer join sales sls on cst.cusnr = sls.cusnr
and cat.catcd1 = sls.catcd1
left outer join credits crd on cst.cusnr = crd.cusnr
and cat.catcd1 = crd.catcd1
And our intermediate table looks like...
CUSNR CAT CUSNR CAT AMT CUSNR
CAT AMT
1 A 1 A 5.00 -
- -
1 B 1 B 10.00 -
- -
1 C - - - -
- -
1 D - - - 1
D 1.00-
1 E - - - 1
E .50-
Adding SUM and group by cst.cusnr, cat.catcd1
CUSNR CAT SALES_TOTAL
CREDIT_TOTAL
1 B 10.00
-
1 E -
.50-
1 D -
1.00-
1 C -
-
1 A 5.00
-
Lets add some more sales & credits for the existing cats...
insert into sales values (1,'A',15.00), (1,'B',12.00)
insert into credits values (1,'D',-0.75), (1,'E',-.25)
CUSNR CAT SALES_TOTAL
CREDIT_TOTAL
1 B 22.00
-
1 E -
.75-
1 D -
1.75-
1 C -
-
1 A 20.00
-
So yeah, could be done without a CTE...
Charles
On Thu, Apr 12, 2018 at 5:07 PM, Mark Murphy <jmarkmurphy@xxxxxxxxx>
wrote:
I think the CTE is overthinking it a bit here, How about this?will
SELECT m.cusnr,
201802 AS YYYYMM,
COALESCE(s.catcd1, c.catcd1, ' ') AS category,
COALESCE(SUM(s.itnsa), 0) AS Sales,
COALESCE(SUM(c.cdtam), 0) AS Credits
FROM DBMSTF.CUSRBTMST m
left JOIN SLSHST.SLSALL s ON m.cusnr = s.cusnr
left JOIN DBMSTF.CRDMEDTL c ON m.cusnr = c.cusnr
WHERE s.INVDT BETWEEN '2018-02-01' AND '2018-02-28' AND
c.DTWRTISO BETWEEN '2018-02-01' AND '2018-02-28'
GROUP BY m.CUSNR, COALESCE(s.catcd1, c.catcd1, ' ')
ORDER BY 1,2,3
I think that by grouping by the categories in the individual files
seperately, you are getting every combination of record that could exist.
But the Groupby can contain an expression, and by using the value that
appear in the output, you should get the right output.the
On Thu, Apr 12, 2018 at 6:42 PM, Charles Wilt <charles.wilt@xxxxxxxxx>
wrote:
You're running into a well known issue call "fan-out"going
Here's a nice animated picture
https://discourse.looker.com/t/outer-join-on-false-or-how-
i-learned-to-stop-fanning-out-and-love-the-null/4786
The FULL OUT JOIN solution that page presents is a new one to me..I'm
to have to play with it. :)
How I'd normally solve the problem would be with CTEs that summarize
jlcrosby@xxxxxxxxxxxxxxxx>data first, then join the CTEs.column
The key is that you want a single row for Customer, Category,
YearMonth...so you use CTEs to transform the raw data into that...
-- Note make sure DATFMT is *ISO
with sales as (
select cusnr, catcd1, substr(char(invdt),1,7) as YearMonth
sum(itnsa) as total
from slshst.slsall
where invdat between '2018-02-01' AND '2018-02-28'
group by cusnr, catcd1, substr(char(invdt),1,7)
), credits as (
select cusnr, catcd1, substr(char(dtwrtiso),1,7) as YearMonth
sum(cdtam) as total
from dbmstf.crdmedtl
where dtwrtiso between '2018-02-01' AND '2018-02-28'
group by cusnr, catcd1, substr(char(dtwrtiso),1,7)
), custs_cats as (
select cst.cusnr, cat.catcd1, '2018-02' as YearMonth
from dbmstf.cusrbtmst cst
cross join CATEGORY_MASTER cat
)
select cc.cusnr, cc.catcd1, cc.YearMonth
, coalesce(sls.Total, 0) as sales_total
, coalesce(crd.total, 0) as credit_total
from custs_cats cc
left outer join sales sls
on cc.cusnr = sls.cusnr
and cc.catcd1 = sls.catcd1
and cc.YearMonth = sls.YearMonth
left outer join credits crd
on cc.cusnr = crd.cusnr
and cc.catcd1 = crd.catcd1
and cc.YearMonth = crd.YearMonth;
Also note that a "Dates Table" or "Calendar Table", with a YearMonth
among many others comes in quite handy for things like this.
Lots of date related queries are become much easier with such a table.
Charles
On Thu, Apr 12, 2018 at 1:49 PM, Jeff Crosby <
perfectwrote:
Not the greatest at SQL but have a project where I think it's a
tofitprogram
so I want to figure this out. It's ultimately going into an RPG
not(as an INSERT statement), but the question at the moment is the SQL,
IRPG. That's why what you see below is a SELECT with stuff hardcoded.
just want the right numbers first.
Some customers are going to start getting rebates. The rebate will a
percent of (sales - credits) by item category for the month. I want
fileinsert into a file the following 5 fields:
customer number
YYYYMM
category
total sales for the month
total credits for the month
There is a file of customers and their rebate percents, the sales
category(with the category field in it), and a credits file (with the
creditsfield in it). There could be both sales and credits for a
customer/category, only sales for a customer/category, or only
credits.for
a customer/category. In all 3 cases I want 1 record percustomer/category.
CUSRBTMST - rebates percents file
SLSALL - sales file
CRDMEDTL - credits file
This is the SQL I have now:
SELECT m.cusnr,
201802 AS YYYYMM,
COALESCE(s.catcd1, c.catcd1, ' ') AS category,
COALESCE(SUM(s.itnsa), 0) AS Sales,
COALESCE(SUM(c.cdtam), 0) AS Credits
FROM DBMSTF.CUSRBTMST m
JOIN SLSHST.SLSALL s ON m.cusnr = s.cusnr
JOIN DBMSTF.CRDMEDTL c ON m.cusnr = c.cusnr
WHERE s.INVDT BETWEEN '2018-02-01' AND '2018-02-28' AND
c.DTWRTISO BETWEEN '2018-02-01' AND '2018-02-28'
GROUP BY m.CUSNR, s.catcd1, c.catcd1
ORDER BY m.CUSNR, s.catcd1, c.catcd1
That's a copy/paste from ACS run SQL scripts that I formatted a bit
further.
The results I get have no resemblance to the actual sales and
file.:)
I'm joining the rebate percent file to both the sales and credits
myThat might be the problem, I don't know. Are 2 SELECTS required?
Thanks for any pointers.
--
Jeff Crosby
VP Information Systems
UniPro FoodService/Dilgard
P.O. Box 13369
Ft. Wayne, IN 46868-3369
260-422-7531
direct.dilgardfoods.com
The opinions expressed are my own and not necessarily the opinion of
mailingcompany. Unless I say so.
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L)
affiliatelist
To post a message email: MIDRANGE-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/midrange-l.
Please contact support@xxxxxxxxxxxx for any subscription related
questions.
Help support midrange.com by shopping at amazon.com with our
listlistlink: http://amzn.to/2dEadiD--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing
To post a message email: MIDRANGE-L@xxxxxxxxxxxx--
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/midrange-l.
Please contact support@xxxxxxxxxxxx for any subscription related
questions.
Help support midrange.com by shopping at amazon.com with our affiliate
link: http://amzn.to/2dEadiD
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing
To post a message email: MIDRANGE-L@xxxxxxxxxxxx--
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/midrange-l.
Please contact support@xxxxxxxxxxxx for any subscription related
questions.
Help support midrange.com by shopping at amazon.com with our affiliate
link: http://amzn.to/2dEadiD
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list
To post a message email: MIDRANGE-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/midrange-l.
Please contact support@xxxxxxxxxxxx for any subscription related
questions.
Help support midrange.com by shopping at amazon.com with our affiliate
link: http://amzn.to/2dEadiD
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.