Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Syntax:  How to Find Commonality Among Similar Results

Posted on 2016-10-18
2
Medium Priority
?
87 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 53

Accepted Solution

by:
Ryan Chong earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

715 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