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 8222389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:15:47+00:00 2026-06-07T14:15:47+00:00

I am trying to pull invoice data from my database based on a PatientID.

  • 0

I am trying to pull invoice data from my database based on a PatientID. I am trying to figure out which invoices belong to which patient. Here is the important parts of my structure.

Invoices Table

InvoiceNumber | DateInvoice | DueDate | StudyID | TypeInvoice 

Patients Table

FirstName | LastName | PatientID

InvoiceFields

id | InvoiceNumber | PatientID | 

I need make a query that lists the invoice table data based upon a PatientID. Below is the query that I attempted, but got no where with. Thank you for your time.

SELECT    Distinct
          invoicefields.InvoiceNumber,
          invoices.DateInvoice
FROM      `invoices`, `patients`, `invoicefields`
WHERE     invoicefields.PatientID = patients.PatientID
          and invoicefields.InvoiceNumber = invoicefields.InvoiceNumber
GROUP BY  invoicefields.InvoiceNumber
  • 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-06-07T14:15:48+00:00Added an answer on June 7, 2026 at 2:15 pm
    SELECT InvT.InvoiceNumber, InvT.DateInvoice
    FROM InvoiceTable InvT
    INNER JOIN InvoiceFields InvF ON InvF.InvoiceNumber = InvT.InvoiceNumber AND InvF.PatientID = @PatientID
    

    So pretty much since you only need data from the InvoiceTable and you indicate you have the PatientID. I propose you just join to the Cross Reference table InvoiceFields and use the PatientID column in that query to filter it down to what you need. I had a more complex example using an exist before I realized you didn’t need anything from Patients.

    You could use this if you need information on the Patient as well (Just put the needed columns in the Select)

    SELECT InvT.InvoiceNumber, InvT.DateInvoice
    FROM InvoiceTable InvT
    INNER JOIN InvoiceFields InvF ON InvF.InvoiceNumber = InvT.InvoiceNumber AND InvF.PatientID = @PatientID
    INNER JOIN Patient Pat ON Pat.PatientID = InvF.PatientID
    

    You can put the @PatientID portion on the join for either Patient or InvoiceFields. There really shouldn’t be a performance difference between either way if you indexes are right.

    The Response to the Below Comment but where I can show it cleaner:

    SELECT  IT.InvoiceNumber
           ,IT.DateInvoice
    FROM    InvoiceTable InvT
    WHERE   EXISTS (SELECT  InvF.PatientID
                    FROM    InvoiceFields InvF
                    WHERE   InvF.InvoiceNumber = InvT.InvoiceNumber
                    AND InvF.PatientID = @PatientID)
    

    This will return all the rows for the patient from InvoiceTable and if InvoiceNumber is Unique will not have any duplicates. Though this way you only have access to InvoiceTable to return Data from. If you only want one put a TOP 1 on it:

    SELECT  TOP 1 IT.InvoiceNumber
           ,IT.DateInvoice
    FROM    InvoiceTable InvT
    WHERE   EXISTS (SELECT  InvF.PatientID
                    FROM    InvoiceFields InvF
                    WHERE   InvF.InvoiceNumber = InvT.InvoiceNumber
                    AND InvF.PatientID = @PatientID)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pull out information from a database with PHP and have the
I'm trying to pull some html data from a mysql database to populate a
I'm trying to pull data from the YouTube API into a local MySQL table.
I'm trying to pull some data out of logs on remote machines using awk,
I am trying to pull data from my populated javascript table. How do I
I'm trying to pull real time indication of interest data out of bloomberg and
I am trying to pull 1 week data from sql server, then trying to
I am trying to pull some data from a server (I don't own the
I've been trying to pull a copy of my sqlite database from my android
I am trying to pull data from our Oracle datamart into R using RODBC.

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.