Solved

SQL Syntax:  How to Find Commonality Among Similar Results

Posted on 2016-10-18
2
60 Views
Last Modified: 2016-10-19
Hello:

Below is my code.  After it is a screenshot.

I want only rows 10 - 18.  As you can see, the difference between these rows and the others is that you have more than one "DocumentNumber".  Yet, you have only one "APTODCNM".

Specifically, for rows 10 - 18, I want the "Cnt" value to say 1, and the other rows to say "0".

I mean no offense, by this next request.  But, I don't want to hear anybody ask for sample data.  

This is a simple SQL syntax question.  That's all.  No background is required.  I know that you solid SQL coding gurus out there can solve this in your sleep.  Perhaps, it's simply a matter of adjusting the "HAVING...COUNT" syntax at the end of my query.

I look forward to hearing back on this, when I arrive in the office tomorrow.

Have a great night!

John


DECLARE @AGE DATETIME;
DECLARE @RUN DATETIME;
SET @AGE = '2015-09-30 00:00:00.000';
SET @RUN = '2016-07-31 00:00:00.000';

select RM20101.CUSTNMBR as [CustomerID], RM00101.CUSTNAME as [CustomerName], RM20101.DOCNUMBR as [DocumentNumber],
COUNT(*) OVER (PARTITION BY RM20201.APFRDCNM, RM20201.APTODCNM) AS Cnt, 
RM20201.APTODCNM, RM20201.DATE1, RM20101.GLPOSTDT, RM20101.DUEDATE, RM20101.DINVPDOF,
CASE WHEN 
RM20101.RMDTYPAL > 6 and RM20201.DATE1 > @AGE and RM20201.APFRDCDT > @AGE AND (RM20101.ORTRXAMT = RM20201.APPTOAMT)
and RM20201.APTODCDT = @AGE and RM20101.ORTRXAMT <> RM20101.CURTRXAM
THEN RM20201.APPTOAMT ELSE 0 END
as [OPEN A/R]
from RM20101
INNER JOIN RM00101 on RM20101.CUSTNMBR = RM00101.CUSTNMBR
INNER JOIN RM20201 on RM20101.DOCNUMBR = RM20201.APFRDCNM 
AND RM20101.CUSTNMBR = RM20201.CUSTNMBR
LEFT OUTER JOIN CN00500 ON RM20101.CUSTNMBR = CN00500.CUSTNMBR
WHERE (RM20101.VOIDSTTS = 0) and (RM20101.CUSTNMBR IN (' 320651', '00065', '00351', '00370', '0035829', '0015200', '0100010149', '0100011881', '0100011303', '0100011766', '0100012897',
'195145', '0100550714', '238624', '0100013309', '0100010453', '266267', '179520', '0100012517', '0100011519', '0100012160',
'0100012558', '0100075597', '0100075290', '0100354828', '274173', '0100014190', '0100020981', '00144', '0100015049', '0100014968', '0100018654', '0100019363', '0100019371',
'0100020932'))    
GROUP BY RM20101.CUSTNMBR, RM00101.CUSTNAME, 
RM20201.APTODCDT, RM20101.DOCNUMBR, 
RM20101.RMDTYPAL, RM20201.DATE1, RM20101.ORTRXAMT, RM20101.CURTRXAM, RM20201.APPTOAMT, RM20201.APFRDCNM, RM20201.APFRDCDT,
RM20201.APTODCNM, RM20101.GLPOSTDT, RM20101.DUEDATE, RM20101.DINVPDOF
--, RM20101.DOCDATE,
HAVING COUNT(RM20201.APFRDCNM) = 1 AND COUNT(RM20201.APTODCNM) = 1 AND
CASE WHEN 
RM20101.RMDTYPAL > 6 and RM20201.DATE1 > @AGE and RM20201.APFRDCDT > @AGE AND (RM20101.ORTRXAMT = RM20201.APPTOAMT)
and RM20201.APTODCDT = @AGE and RM20101.ORTRXAMT <> RM20101.CURTRXAM
THEN RM20201.APPTOAMT ELSE 0 END
<> 0

Open in new window


Capture
0
Comment
Question by:John Ellis
[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
2 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 41849361
Try COUNT(*) OVER (PARTITION BY RM20201.APTODCNM) AS Cnt instead of your cnt
0
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41849375
Specifically, for rows 10 - 18, I want the "Cnt" value to say 1, and the other rows to say "0".

so you can have this for your "cnt" field:
case when COUNT(*) OVER (PARTITION BY RM20201.APTODCNM) > 1 then 1 else 0 end AS Cnt

Open in new window

0

Featured Post

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

696 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