Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1061507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:28:59+00:00 2026-05-16T18:28:59+00:00

I have the following tables: Orders, Notes, Permits The following columns are in each

  • 0

I have the following tables:
Orders, Notes, Permits

The following columns are in each table:

Orders = ID
Notes = ID, RelatedID, Note, Timestamp
Permits = ID, OrderId

I have the following query

SELECT o.id
     , op.id
     , n.timestamp
  FROM [tblOrders] o
INNER JOIN [tblNotes] n ON n.[RelatedID] = o.[ID]
INNER JOIN [tblPermits] op ON o.[id] = op.[OrderID]
     WHERE n.[Text] LIKE 'Line item is created%'

An order has 1 to many permits and a order has 1 to many notes

The problem here is that the notes relate to the order and not the individual permit so when you join o.id with n.relatedID if there is more that 1 permit in an order it will actually show 4 records instead of 2 since it joins twice for each permit since the orderID is the same. How can I get this to only return 2 records?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:29:00+00:00Added an answer on May 16, 2026 at 6:29 pm

    The issue is using JOINs risks duplication in the resultset because there’ll be a record for each supporting record in the tblnotes. My first recommendation is to re-write so you aren’t using a JOIN:

    Using EXISTS:

    SELECT o.id,
           p.id
      FROM tblorders o
      JOIN tblpermits p ON p.orderid = o.id
     WHERE EXISTS(SELECT NULL
                    FROM tblnotes n 
                   WHERE n.[Text] LIKE 'Line item is created%'
                     AND n.relatedid = o.id)
    

    Using IN:

    SELECT o.id,
           p.id
      FROM tblorders o
      JOIN tblpermits p ON p.orderid = o.id
     WHERE o.id IN (SELECT n.relatedid
                      FROM tblnotes n 
                    WHERE n.[Text] LIKE 'Line item is created%')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables Orders(ID,ORDERDATE,DELIVERYDATE,GOODID,QUANTITY,COLLECTIONFROM,DELIVERYTO,NOTES) and ROLLINGSTOCK_ORDER(ORDERID,ROLLINGSTOCKID,DEPARTUREDATE,DELIVERYDATE,ROUTEID) i have created a trigger to update
I have the following query selecting everything from the notes table where the input(ex:
My Schema I have the following tables table notes/example values ------------------------------------------------ users ( id
I have a fact table with the following rows: WorkerName, OrderId, NumberOfPackagesPerOrder Note that
Suppose that we have following tables create table Employee( 2 EMPNO NUMBER(3), 3 ENAME
I have the following tables. I want to run a query but I think
I have the following tables: Table 1 - NODES (I access it through a
I have the following table that contains checkboxes in each row. When checkbox status
I have two Tables: Table-1: notes Table-2: contacts My basic objective is to perform
I have the following tables: Order ---- ID (pk) OrderItem ---- OrderID (fk ->

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.