Solved

SQL Syntax:  How to Find Commonality Among Similar Results

Posted on 2016-10-18
2
32 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
2 Comments
 
LVL 24

Expert Comment

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

19 Experts available now in Live!

Get 1:1 Help Now