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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:46:30+00:00 2026-06-01T21:46:30+00:00

I have 2 tables…they are basically the same except for the column name in

  • 0

I have 2 tables…they are basically the same except for the column name in one of them because they deal with 2 different names, though.. the data I want is in columns that have the same name. Pretty much what I want to do… is .they also need to be distinct so I don’t count duplicates… I can get them as separate tables… but I can’t get them together.. I need them in 1 column because of how it is sent to the C3 code page and how it reads it… the structure has already been previously set.. and there are about 5 other statements that are being executed in this one stored procedure like this (also I wasn’t the one who set this up). So if this is possible.. let me know please.. I will also paste the exact code I have if you feel like seeing the real life code.. I just tried to simplify it to make it easier to understand.
I’m going to post some images to hopefully explain it a little better.

Image 1 is what is currently set up:

http://imgur.com/h0rNU

top part is what is stored in tables.. bottom is more of the result
it basically runs this code to get the bottom

DECLARE @id INT;
DECLARE @invest nvarchar(50);
SET @id = '7633';
SET @invest = '';

SELECT 'a' + CONVERT(nvarchar, orderfindings.risk_rating) AS cat, COUNT(DISTINCT orderfindings.prnt_id) AS stat
    FROM orderheader, orderaudits, orderfindings
    WHERE orderheader.id = orderaudits.orderheader_id AND orderaudits.ID = orderfindings.prnt_id
    AND orderheader.id = @id AND orderfindings.risk_rating > 0 AND orderaudits.Investor_Name LIKE '%' + @invest + '%'
    GROUP BY orderfindings.risk_rating

If I want agencies instead of findings.. just replace it.. agencies and findings are the 2 tables.. they are the pretty much identical column wise… but I want the result together.. I’ve tried several ways.. but I can’t seem to get it
image 2- the table at the bottom is more what I’m looking for.. it combines them both into 1:

http://imgur.com/a4qXW

If an order has a finding or agency or both in it.. then it gets marked as a 1 for that risk rating… if it doesn’t.. then 0 for that risk rating.. and then sum them all up to see what I got.. any help is appreciated .. I’ll clarify what I can if you don’t understand;

Edit:
I’ve been working with it.. .did this http://pastebin.com/117Z2TVh .. got it to display 2 columns.. but still not the right result… I’m getting a1 = 1…a2 = 1… so its not running through all the orders… or it needs a way to count it… I put a sum at beginning of case statement.. erro because of counts… so I took counts out… didn’t really work.. any advice?

  • 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-01T21:46:31+00:00Added an answer on June 1, 2026 at 9:46 pm

    ;WITH finding AS (select oa.id, oa.Risk_Rating, COUNT (orf.Prnt_ID) findingcount
    FROM orderaudits oa
    LEFT JOIN orderfindings orf
    on oa.ID = orf.Prnt_ID
    –WHERE oa.risk_rating > 0 and oa.Investor_Name like ‘%’ + @invest + ‘%’
    GROUP BY oa.id, oa.Risk_Rating),
    Agency AS (select oa.id, oa.Risk_Rating, COUNT (ora.Prnt_ID) agencycount
    FROM orderaudits oa
    LEFT JOIN orderagencies ora
    on oa.ID = ora.Prnt_ID
    –WHERE oa.risk_rating > 0 and oa.Investor_Name like ‘%’ + @invest + ‘%’
    GROUP BY oa.id, oa.Risk_Rating)
    /*
    SELECT ‘Finding Only’, oa.Risk_Rating, COUNT(oa.id) as a, SUM(CASE WHEN f.findingcount > 0 then 1 ELSE 0 END ) as b
    FROM orderaudits oa
    LEFT JOIN finding f
    ON oa.id = f.id
    LEFT JOIN orderheader oh
    ON oh.id = oa.orderheader_id
    WHERE oh.id = @id and oa.risk_rating > 0 and oa.Investor_Name like ‘%’ + @invest + ‘%’
    GROUP BY oa.Risk_Rating

    UNION ALL

    SELECT ‘Agency Only’, oa.Risk_Rating, COUNT(oa.id) as a, SUM(CASE WHEN a.agencycount > 0 then 1 ELSE 0 END ) as b
    FROM orderaudits oa
    LEFT JOIN Agency a
    ON oa.id = a.id
    LEFT JOIN orderheader oh
    ON oh.id = oa.orderheader_id
    WHERE oh.id = @id and oa.risk_rating > 0 and oa.Investor_Name like ‘%’ + @invest + ‘%’
    GROUP BY oa.Risk_Rating

    UNION ALL
    */
    –Together
    SELECT ‘aa’ + convert(nvarchar, oa.risk_rating) as cat, SUM(CASE WHEN f.findingcount > 0 OR a.agencycount > 0 then 1 ELSE 0 END ) as b
    FROM orderaudits oa
    LEFT JOIN finding f
    ON oa.id = f.id
    LEFT JOIN Agency a
    ON oa.id = a.id
    LEFT JOIN orderheader oh
    ON oh.id = oa.orderheader_id
    WHERE oh.id = @id and oa.risk_rating > 0 and oa.Investor_Name like ‘%’ + @invest + ‘%’
    GROUP BY oa.Risk_Rating

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

Sidebar

Related Questions

I have tables with latin1_swedish_ci column. I've done tests and inserted into them some
I have tables with listings, categories and one that maps them to each other.
I have several databases that have tables with the same name. I mean, I
I have tables named Contact and Address and both of them have ModifiedDate column.
I have two .bak files These files contain different tables in them, and it
I have tables Product ( ID, CategoryID, ... ) Category ( ID, Name, ..
hi i have tables like this: Persons: person_id, name then i have many langauge
I have many tables that use Lookup/Enum references for most of their column values.
I have three tables A: A.pID primary key, A.Name nvarchar(250) B: B.pID primary key,
I have 5 tables: customers id - name p_orders id - id_customer - code

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.