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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:45:58+00:00 2026-05-26T14:45:58+00:00

I show a grid with parent data and need to show icon if it

  • 0

I show a grid with parent data and need to show icon if it has a relevant child row(s) exist. My DB is in SQL Server 2008. Let me simplify, I’ve the following two tables –

Order (PK : ID)

File (PK: FileID, FK: OrderID)

An Order can have zero or more files related to it. The File table has a column OrderID which holds FK reference to Order. Now, I’m displaying a grid which lists all the Orders and I want to display an icon image indicating if that Order has any child rows (files) or not.

Here’s a tricky way I’ve experimented but not sure how efficient/scalable it is –

SELECT DISTINCT o.ID, o.OrderNum, ...
,(CASE f.ID/f.ID WHEN 1 THEN 1 ELSE 0 END) as FilesExist
...
FROM  Order AS o LEFT OUTER JOIN dbo.FileHeader as f ON f.OrderID = o.ID

The CASE statement seems to work perfectly as required. It’ll return 1 if one or more file(s) exists, else 0. If multiple file rows exist then it will try to repeat the Order row for which I’ve added the DISTINCT and I’m not selecting f.ID but f.ID/f.ID which will be 1 (it exists) and 0 for null (doesn’t exist). I’ve learned that JOINs are better than inline SELECT COUNT(*) statements.

I’ve tested and it works but I need expert opinion and need to make sure that this is the best way of doing it. This is a very simple example but my SELECT statement is complex as there are many lookups and its going to be a costly fetch so I need to make sure its scalable.

Thank you.

EDIT #1:
To conclude – either it is going to be internal SELECT with a COUNT(*)

SELECT c.ClaimNo,(SELECT COUNT(*) FROM dbo.FileHeader AS f WHERE f.ClaimID = c.ID ) AS FilesHExist
FROM  dbo.Claim AS c

Internal Select

or the LEFT OUTER JOIN approach that I’ve mentioned.

SELECT DISTINCT c.ClaimNo, (CASE fh.ID / fh.ID WHEN 1 THEN 1 ELSE 0 END) AS FilesHExist               
FROM  dbo.Claim AS c LEFT OUTER JOIN dbo.FileHeader AS fh ON fh.ClaimID = c.ID

My JOIN approach

Attached two images of query execution plan. Kindly suggest which one is better.

  • 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-26T14:45:59+00:00Added an answer on May 26, 2026 at 2:45 pm

    For selecting few rows, this should be fastest:

    SELECT o.ID
         , o.OrderNum 
         , CASE WHEN EXISTS (SELECT * FROM dbo.FileHeader f WHERE f.OrderID = o.ID)
                THEN 1
                ELSE 0
           END AS FilesHExist
    FROM   "Order" o;
    

    (I quoted the reserved word "Order".)

    For a SELECT that includes large portions of dbo.FileHeader this should perform better:

    SELECT o.ID
         , o.OrderNum 
         , CASE WHEN f.OrderID IS NULL THEN 0 ELSE 1 END AS FilesHExist
    FROM   "Order" o
    LEFT   JOIN
          (SELECT OrderID FROM dbo.FileHeader GROUP BY OrderID) f ON f.OrderID = o.ID
    

    It should be cheaper to group OrderID first and then left join. No need for DISTINCT at the end.

    Never use:

    (CASE f.ID/f.ID WHEN 1 THEN 1 ELSE 0 END)
    

    It opens you up to division by zero exception. Replace it with a check for NULL like demonstrated above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to show a Master/Child data in a page and I have used
I need to show a grid or table of data in a pop-up dialog
How to read the excel file and show its data in grid view ?
I have a grid that has multiple rows. I want to hide/show one of
I have written following to show my data into grid view. This is the
I am using following class to show a grid like appearance with pagination on
Is it possible to show multiple lines of text in grid cells using the
MessageBox.Show has forms like MessageBox.Show( ownerWindow, .... ). What do I gain by assigning
I show an image on the navigation bar (I use UIImageView). Could you let
YQL SHOW TABLES has CSV and HTML. What about a table for Apache access

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.