Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Problem

How to select rows from table shippolicyh that have related one row only on table trxfootersafe ?

meaning to modify query that get all policycode on shippolicyh that have one row only related on

table trxfootersafe .

the following query not give me result

so that what I change .

Sample to result

policycode 1211 must not show because it have two lines on trxfootersafe table .
<pre lang="SQL">Shippolicyh
BranchCode, PolicyCode EgyPolicyNum  Sender
1             1211        3494939      12

trxfootersafe
serial  year    subldgcode1   subldgcode2  debit credit typetrxsafe
50      2019        12           1211       0     90    -1
55      2019        12           1211       90     0     1

policycode 5555 must show because it have one lines only on trxfootersafe table .

SQL
Shippolicyh
BranchCode, PolicyCode EgyPolicyNum  Sender
1             5555        3494939      12

trxfootersafe
serial  year    subldgcode1   subldgcode2  debit credit typetrxsafe
70      2019        12           5555       90     0   -1


but problem that query above show all policy code on shippolicyh table that have one line or two lines
so that how to make it show one line related only
meaning show policy code number 5555 because it have one line
and not show policy code 1211 because it have two lines

What I have tried:

SELECT
   BranchCode,PolicyCode,PolicyDate,EgyPolicyNum
FROM
    SHIPPolicyH H
WHERE
    EXISTS (
        SELECT
            COUNT (*)
        FROM
            TrxFooterSafe F
        WHERE
            SubLdgCode2 = H.PolicyCode AND BranchCode = H.BranchCode AND SubLdgCode1=H.Sender and BranchCode=1 and Debit > 0 and Credit=0 and TypeTrxSafeCode=-1
        GROUP BY
            SubLdgCode2,SubLdgCode1
        HAVING
            COUNT (*) =1
    )
ORDER BY
    PolicyCode,
    BranchCode;
Posted
Updated 29-Jul-19 9:27am

1 solution

Check this:

SQL
SELECT *
FROM SHIPPolicyH H
WHERE  EXISTS (
        SELECT COUNT (F.SubLdgCode2)
        FROM TrxFooterSafe F
        WHERE SubLdgCode2 = H.PolicyCode AND BranchCode = H.BranchCode AND SubLdgCode1=H.Sender and BranchCode=1 and Debit > 0 and Credit=0 and TypeTrxSafeCode=-1
        GROUP BY SubLdgCode2,SubLdgCode1
        HAVING COUNT (F.SubLdgCode2) =1
    )
ORDER BY
    PolicyCode,
    BranchCode;


SQL Fiddle[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900