Solved

SQL Syntax:  How to Find Commonality Among Similar Results

Posted on 2016-10-18
2
54 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 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…

860 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