T-SQL: Pulling Data Based on Only One Value

  • Hello:

    The results of my first code block below are shown in the attached Excel spreadsheet called "Clause Results". I don't want the last two rows, for 266267. I only want the first two rows, for 0100012160.

    The second block of code below shows the results that I want--one iteration of the APTODCNM field. A picture of this is shown in the attached file called "One APTODCNM.png".

    The third block of code shows three iterations of APTODCNM. I don't want this. This is shown in the final attached file called "Three APTODCNMs.png".

    What sort of syntax do I place in the first block of code to tell it to return data where there is only one APTODCNM?

    Thank you! Much appreciated!

    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],

    CASE WHEN RM20101.RMDTYPAL > 6 and RM20101.DOCDATE < @AGE and RM20201.DATE1 > @AGE

    and RM20201.APTODCDT > @AGE AND RM20201.APFRDCDT < @AGE

    AND RM20101.DOCNUMBR not in (select RM20201.APFRDCNM from RM20201

    INNER JOIN RM20101 ON RM20201.CUSTNMBR = RM20101.CUSTNMBR AND RM20201.APTODCNM = RM20101.DOCNUMBR

    AND RM20201.ORAPTOAM = RM20101.ORTRXAMT

    GROUP BY RM20201.APTODCNM, RM20201.APTODCDT, RM20201.APFRDCNM

    HAVING COUNT(RM20201.APFRDCNM) = 1)

    and RM20101.ORTRXAMT <> RM20101.CURTRXAM

    AND RM20201.APFRMAPLYAMT <> RM20101.ORTRXAMT

    AND RM20101.DUEDATE <> ''

    THEN RM20201.APFRMAPLYAMT * -1 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'))

    GROUP BY RM20101.CUSTNMBR, RM00101.CUSTNAME, RM00101.PYMTRMID, CN00500.CRDTMGR, RM00101.COMMENT1, RM00101.COMMENT2, RM20201.APTODCDT, RM20201.APTODCNM, RM20101.DOCNUMBR, RM20101.DOCDATE,

    RM20201.APFRDCDT, RM20101.RMDTYPAL, RM20201.DATE1, RM20101.ORTRXAMT, RM20101.CURTRXAM, RM20101.DUEDATE, RM20201.APPTOAMT,

    RM20201.APFRMAPLYAMT, RM20201.ORAPTOAM

    HAVING

    CASE WHEN RM20101.RMDTYPAL > 6 and RM20101.DOCDATE < @AGE and RM20201.DATE1 > @AGE

    and RM20201.APTODCDT > @AGE AND RM20201.APFRDCDT < @AGE

    AND RM20101.DOCNUMBR not in (select RM20201.APFRDCNM from RM20201

    INNER JOIN RM20101 ON RM20201.CUSTNMBR = RM20101.CUSTNMBR AND RM20201.APTODCNM = RM20101.DOCNUMBR

    AND RM20201.ORAPTOAM = RM20101.ORTRXAMT

    GROUP BY RM20201.APTODCNM, RM20201.APTODCDT, RM20201.APFRDCNM

    HAVING COUNT(RM20201.APFRDCNM) = 1)

    and RM20101.ORTRXAMT <> RM20101.CURTRXAM

    AND RM20201.APFRMAPLYAMT <> RM20101.ORTRXAMT

    AND RM20101.DUEDATE <> ''

    THEN RM20201.APFRMAPLYAMT * -1 ELSE 0 END

    <> 0

    select * from RM20101 where CUSTNMBR = '0100012160' AND DOCNUMBR IN ('P0192700A')

    select * from RM20201 where CUSTNMBR = '0100012160' AND APTODCNM IN ('P0519167')

    select * from RM20101 where CUSTNMBR = '266267' AND DOCNUMBR = 'G1676571'

    select * from RM20201 where CUSTNMBR = '266267' AND APFRDCNM = 'G1676571A'

  • Can you please post the DDL (create table) scripts for the relevant tables and sample data as an insert statement?

    😎

  • Thanks, for the response!

    Actually, I figured this out on my own.

    I used a COUNT(*) clause, as follows:

    COUNT(*) OVER (PARTITION BY RM20101.CUSTNMBR, RM20201.APTODCNM, RM20201.APTODCDT) as Cnt.

    Specifically, I embedded this clause within a cte statement as shown in the code below.

    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'

    ;with cte as

    (

    select RM20101.CUSTNMBR as [CustomerID], RM00101.CUSTNAME as [CustomerName], RM20101.DOCNUMBR as [DocumentNumber],

    COUNT(*) OVER (PARTITION BY RM20101.CUSTNMBR, RM20201.APTODCNM, RM20201.APTODCDT) AS Cnt,

    CASE WHEN RM20101.RMDTYPAL > 6 and RM20101.DOCDATE < @AGE and RM20201.DATE1 > @AGE

    and RM20201.APTODCDT > @AGE AND RM20201.APFRDCDT < @AGE

    AND RM20101.DOCNUMBR not in (select RM20201.APFRDCNM from RM20201

    INNER JOIN RM20101 ON RM20201.CUSTNMBR = RM20101.CUSTNMBR AND RM20201.APTODCNM = RM20101.DOCNUMBR

    and RM20201.APPTOAMT = RM20101.ORTRXAMT

    GROUP BY RM20201.APTODCNM, RM20201.APTODCDT, RM20201.APFRDCNM)

    AND

    RM20101.ORTRXAMT <> RM20101.CURTRXAM

    AND RM20201.APFRMAPLYAMT <> RM20101.ORTRXAMT

    AND RM20101.DUEDATE <> ''

    THEN RM20201.APFRMAPLYAMT * -1 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 ('266267', '0100012160'))

    GROUP BY RM20101.CUSTNMBR, RM00101.CUSTNAME, RM00101.PYMTRMID, CN00500.CRDTMGR, RM00101.COMMENT1, RM00101.COMMENT2,

    RM20101.RMDTYPAL, RM20101.DUEDATE, RM20201.APFRMAPLYAMT, RM20101.DOCNUMBR, RM20201.APFRDCNM, RM20101.DOCDATE,

    RM20201.DATE1, RM20201.APTODCDT, RM20201.APPTOAMT, RM20201.APTODCNM, RM20201.APFRDCDT, RM20101.ORTRXAMT, RM20101.CURTRXAM

    HAVING

    CASE WHEN RM20101.RMDTYPAL > 6 and RM20101.DOCDATE < @AGE and RM20201.DATE1 > @AGE

    and RM20201.APTODCDT > @AGE AND RM20201.APFRDCDT < @AGE

    AND RM20101.DOCNUMBR not in (select RM20201.APFRDCNM from RM20201

    INNER JOIN RM20101 ON RM20201.CUSTNMBR = RM20101.CUSTNMBR AND RM20201.APTODCNM = RM20101.DOCNUMBR

    and RM20201.APPTOAMT = RM20101.ORTRXAMT

    GROUP BY RM20201.APTODCNM, RM20201.APTODCDT, RM20201.APFRDCNM)

    and

    RM20101.ORTRXAMT <> RM20101.CURTRXAM

    AND RM20201.APFRMAPLYAMT <> RM20101.ORTRXAMT and RM20201.APPTOAMT <> RM20101.ORTRXAMT

    AND RM20101.DUEDATE <> '' AND RM20101.DOCNUMBR = RM20201.APFRDCNM

    THEN RM20201.APFRMAPLYAMT * -1 ELSE 0 END

    <> 0)

    select * from cte where Cnt > 1;

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply