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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:45:20+00:00 2026-05-18T19:45:20+00:00

My SQL query joins on multiple tables, and because of this it is displaying

  • 0

My SQL query joins on multiple tables, and because of this it is displaying multiple results. I know about SELECT DISTINCT, but one of the fields (‘Account.Name’) is occasionally different, so it treats this record as a new row. Any ideas? Here is my SQL:

    SELECT DISTINCT 
                      d.ID, ISNULL(d.OnHold, 0) AS 'OnHold', d.OnHoldReason AS 'On Hold Reason', d.LegacyID AS 'Consignment#', d.IntConNo AS 'Consignment Ref', 
                      CASE WHEN JobType = 'O' THEN 'Outward' ELSE 'Collection' END AS 'Job Type', d.TripDate AS 'Delivery Date', d.DeliveryTown AS 'Delivery Town', 
                      d.DeliveryPostcode AS 'Delivery Postcode', d.VehicleReg AS 'Vehicle Reg', d.RequiredCollectionDate AS 'Req. Collection Date', 
                      d.VehicleReg AS 'Vehicle Registration', CASE WHEN d .DeliveryStatus = 2 THEN 'OK' END AS 'Delivery Status', d.ItemsignedBy AS 'POD Signed By', 
                      d.BaseRate AS 'Base Rate', d.FuelSurcharge AS 'Fuel Surcharge', d.AdditionalCharges AS 'Additional Charges', 
                      d.BaseRate + d.FuelSurcharge + d.AdditionalCharges AS 'Value', CASE WHEN InvoiceNumber IS NULL THEN CONVERT(bit, 0) ELSE CONVERT(bit, 1) 
                      END AS Tagged, CASE WHEN di.Total = di.Delivered THEN 'Recieved' ELSE 'Not Recieved' END AS 'Items', Account.Name
FROM         Deliveries AS d LEFT OUTER JOIN
                          (SELECT     Consignment, COUNT(*) AS Total, COUNT(CASE WHEN Status = 2 THEN 1 END) AS Delivered
                            FROM          Items
                            GROUP BY Consignment) AS di ON d.ID = di.Consignment INNER JOIN
                      Account ON d.Customer = Account.InvoiceAccount
WHERE     (d.InvoiceNumber IS NULL) AND (d.TripDate > 29 / 11 / 2010)
ORDER BY 'Consignment#'
  • 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-18T19:45:20+00:00Added an answer on May 18, 2026 at 7:45 pm

    The easiest solution would be to replace the DISTINCT with a GROUP BY. The dowside is that only one accountname get’s returned.

    Other solutions could be

    • create a function returning the concatenated account names
    • use a sub-select to return only one name

    Using group by

    SELECT  d.ID
            , ISNULL(d.OnHold, 0) AS 'OnHold'
            , d.OnHoldReason AS 'On Hold Reason'
            , d.LegacyID AS 'Consignment#'
            , d.IntConNo AS 'Consignment Ref'
            , CASE WHEN JobType = 'O' THEN 'Outward' ELSE 'Collection' END AS 'Job Type'
            , d.TripDate AS 'Delivery Date'
            , d.DeliveryTown AS 'Delivery Town'
            , d.DeliveryPostcode AS 'Delivery Postcode'
            , d.VehicleReg AS 'Vehicle Reg'
            , d.RequiredCollectionDate AS 'Req. Collection Date'
            , d.VehicleReg AS 'Vehicle Registration'
            , CASE WHEN d .DeliveryStatus = 2 THEN 'OK' END AS 'Delivery Status', d.ItemsignedBy AS 'POD Signed By'
            , d.BaseRate AS 'Base Rate'
            , d.FuelSurcharge AS 'Fuel Surcharge'
            , d.AdditionalCharges AS 'Additional Charges'
            , d.BaseRate + d.FuelSurcharge + d.AdditionalCharges AS 'Value'
            , CASE WHEN InvoiceNumber IS NULL THEN CONVERT(bit, 0) ELSE CONVERT(bit, 1) END AS Tagged
            , CASE WHEN di.Total = di.Delivered THEN 'Recieved' ELSE 'Not Recieved' END AS 'Items'
            , MIN(Account.Name)
    FROM    Deliveries AS d 
            LEFT OUTER JOIN (
              SELECT  Consignment
                      , COUNT(*) AS Total
                      , COUNT(CASE WHEN Status = 2 THEN 1 END) AS Delivered
              FROM    Items
              GROUP BY 
                      Consignment
            ) AS di ON d.ID = di.Consignment 
            INNER JOIN Account ON d.Customer = Account.InvoiceAccount
    WHERE   (d.InvoiceNumber IS NULL) 
            AND (d.TripDate > 29 / 11 / 2010)
    GROUP BY
            d.ID
            , ISNULL(d.OnHold, 0)
            , d.OnHoldReason
            , d.LegacyID
            , d.IntConNo
            , CASE WHEN JobType = 'O' THEN 'Outward' ELSE 'Collection' END
            , d.TripDate
            , d.DeliveryTown
            , d.DeliveryPostcode
            , d.VehicleReg
            , d.RequiredCollectionDate
            , d.VehicleReg
            , CASE WHEN d .DeliveryStatus = 2 THEN 'OK' END AS 'Delivery Status', d.ItemsignedBy
            , d.BaseRate
            , d.FuelSurcharge
            , d.AdditionalCharges
            , d.BaseRate + d.FuelSurcharge + d.AdditionalCharges
            , CASE WHEN InvoiceNumber IS NULL THEN CONVERT(bit, 0) ELSE CONVERT(bit, 1) END
            , CASE WHEN di.Total = di.Delivered THEN 'Recieved' ELSE 'Not Recieved' END
    ORDER BY 
            'Consignment#'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I have duplicate results from this query because the tables ReleaseHistory and
I have a MS SQL query that joins multiple tables and an example of
I'm writing a linq-to-sql query that joins 4 tables. I have something like this:
I have a complex SQL query that joins multiple tables. Here's a watered down
i have a general question about how sql server evaluates the joins.The query is
I have an SQL query with a nested join: SELECT rh.host, rh.report, COUNT(results.id), COUNT(results_2.id),
SQL query which select the record from three tables and there is no relation
I want to Join multiple tables using IF ELSE statement within Sql Query when
I've got a SQL query that joins a pricing table to a table containing
I am running an SQL query which self-joins the same table 24 times in

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.