Solved

SQL Syntax:  How to Find Commonality Among Similar Results

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SHOWPLAN permission denied in database 'AdventureWorks'. 13 101
sql query 8 50
export sql results to csv 6 36
date diff with Fiscal Calendar 4 30
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…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 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