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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:50:45+00:00 2026-06-16T11:50:45+00:00

I have a table that I need to use to build a result set

  • 0

I have a table that I need to use to build a result set from where certain rows from the table are columns in the result set. I started to chain LEFT JOINs together on the table multiple times but I need to eliminate results that are a different combination of another result already in the set:

For example, if I get 1, 21, 25 as result columns, I can’t have ANY other combination of those numbers in the results.

My table definition is:

Table tblKPIDetails
Column Month int
Column Year int
Column Division varchar(3)
Column KPI int
Column Value decimal(18,4)

My current query is:

SELECT *
FROM tblKPIDetails J1
    LEFT JOIN tblKPIDetails J2 ON J2.Month = J1.Month AND J2.Year = J1.Year  AND J2.Division = J1.Division AND NOT(J2.KPI = J1.KPI ) AND (J2.KPI = 1 OR J2.KPI = 21 OR J2.KPI = 25)
    LEFT JOIN tblKPIDetails J3 ON J3.Month = J1.Month AND J3.Year = J1.Year  AND J3.Division = J1.Division AND NOT(J3.KPI = J1.KPI ) AND (J3.KPI = 1 OR J3.KPI = 21 OR J3.KPI = 25)
WHERE J1.KPI = 1 OR J1.KPI = 21 OR J1.KPI = 25

I know this is wrong, but it’s a super-set of what I need. In the results from the query above, I can get J1.KPI, J2.KPI, J3.KPI or J1.KPI, J3.KPI, J2.KPI, or any other combination.

My expected result would be:

Division | Month | Year | KPIA | KPIAValue | KPIB | KPIBValue | KPIC | KPICValue

for each division, month, and year

where KPIA, KPIB, or KPIC = 1, 21, or 25 but only 1 combination of 1,21,25 exists per division|month|year

EDIT

To clarify the expected results a little more, using the above query, I’m getting the following results:

Division | Month | Year | KPIA | KPIAValue | KPIB | KPIBValue | KPIC | KPICValue
--------------------------------------------------------------------------------
000          1     2012     1      1000       21      2000       25     3000
000          1     2012    21      2000        1      1000       25     3000
000          1     2012    25      3000       21      2000        1     1000
111          1     2012     1      555        21      10000      25     5000

I need to make it so my results would only be ANY 1 of the first 3 results and then the last one…for example:

Division | Month | Year | KPIA | KPIAValue | KPIB | KPIBValue | KPIC | KPICValue
--------------------------------------------------------------------------------
000          1     2012    25      3000       21      2000        1     1000
111          1     2012     1      555        21      10000      25     5000
  • 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-16T11:50:46+00:00Added an answer on June 16, 2026 at 11:50 am

    I think you are looking for the PIVOT table operator like so:

    SELECT 
      Devision,
      Month, 
      Year,
      [1]  AS KPIAValue,
      [21] AS KPIBValue,
      [25] AS KPICValue
    FROM
    (
      SELECT t1.*
      FROM tblKPIDetails t1
      INNER JOIN
      (
        SELECT Month, Year, Devision
        FROM tblKPIDetails
        WHERE KPI IN(1, 21, 25)
        GROUP BY Month, Year, Devision
        HAVING COUNT(DISTINCT KPI) = 3
      ) t2 ON t1.Month = t2.Month AND t1.Year = t2.Year 
      AND t1.Devision = t2.Devision
    ) t
    PIVOT
    (
      MAX(Value)
      FOR KPI IN([1], [21], [25])) p;
    

    SQL Fiddle Demo

    This will give you the data in the form:

    | DEVISION | MONTH | YEAR | KPIAVALUE | KPIBVALUE | KPICVALUE |
    ---------------------------------------------------------------
    |        A |     2 | 2012 |        16 |        16 |        16 |
    |        B |    10 | 2012 |        16 |        18 |        20 |
    

    Note that: This will give you the only combination of the Year, Month, DEVISION that have all the values 1, 21 and 25, and that what this query do:

    SELECT Month, Year, Devision
    FROM tblKPIDetails
    WHERE KPI IN(1, 21, 25)
    GROUP BY Month, Year, Devision
    HAVING COUNT(DISTINCT KPI) = 3
    

    Update: If you are looking for those that had at least one of 1, 21 or 25, just remove the HAVING COUNT(DISTINCT KPI) = 3, but this will make you expect more values than these three, in this case it will ignore other values and return only those three. Also it will return NULL for any of the missing values of them like so:

    SELECT 
      Devision,
      Month, 
      Year,
      [1]  AS KPIAValue,
      [21] AS KPIBValue,
      [25] AS KPICValue
    FROM
    (
      SELECT t1.*
      FROM tblKPIDetails t1
      INNER JOIN
      (
        SELECT Month, Year, Devision
        FROM tblKPIDetails
        WHERE KPI IN(1, 21, 25)
        GROUP BY Month, Year, Devision
      ) t2 ON t1.Month = t2.Month AND t1.Year = t2.Year 
      AND t1.Devision = t2.Devision
    ) t
    PIVOT
    (
      MAX(Value)
      FOR KPI IN([1], [21], [25])) p;
    

    Updated SQL Fiddle Demo

    | DIVISION | MONTH | YEAR | KPIAVALUE | KPIBVALUE | KPICVALUE |
    ---------------------------------------------------------------
    |        A |     2 | 2012 |      15.5 |      15.5 |      15.5 |
    |        B |    10 | 2012 |      15.5 |      17.5 |     20.24 |
    |        C |    12 | 2012 |      15.5 |    (null) |     20.24 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that I need to update where all columns are optionally
I have a database table with a field that I need to read from
I have a large database table that I need to display on a Windows
I have a table of data that I need to dynamically add a column
I have table to test score data that I need to pivot and I
I have a table with string values that need to matched individually to a
My code contains a table that I need to have a fixed width. The
I have a table that has duplicate email address, I need to insert just
I have a database two tables and a linking table that I need a
I have to develop a table that would contain distance between destinations and need

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.