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

  • Home
  • SEARCH
  • 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 9200929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:52:53+00:00 2026-06-17T22:52:53+00:00

I have a simple table : DECLARE @t TABLE(item INT, itemType INT ) insert

  • 0

I have a simple table :

DECLARE @t TABLE(item INT, itemType INT  )

insert INTO @t SELECT 1000,0
insert INTO @t SELECT 1000,3
insert INTO @t SELECT 1000,5
insert INTO @t SELECT 1000,6
insert INTO @t SELECT 2000,0
insert INTO @t SELECT 2000,3
insert INTO @t SELECT 2000,5
insert INTO @t SELECT 2000,6
insert INTO @t SELECT 3000,0
insert INTO @t SELECT 3000,10
insert INTO @t SELECT 4000,11

I want to select all items where itemtype = 3 but if there is a row , provide also its base itemtype (if it exists) which is itemType = 0.

For example :

  • for itemType = 3

    1000,0 should be provided –why ? because table also has 1000 + itemType 0
    1000,3 should be provided –why ? because we looked for itemType=3
    2000,0 should be provided –why ? because table has found 2000,3 and this 2000 also has itemType=0
    2000,3 should be provided –why ? because we looked for itemType=3

  • for itemType = 10

    3000,0 should be provided –why ? because table has found 3000,10 and this 3000 also has itemType=0
    3000,10 should be provided –why ? because we looked for itemType=10

  • for itemType = 11

    4000,11 should be provided –why ? because we looked for itemType=11 ( notice , there isn’t itemType 0 , so only itself).

I started doing :

;with cte as(
             SELECT * FROM @t  
            )
select * from cte where itemType=3

In summary, if the itemType is found, provide itself + its zero type (if exists), and also for his siblings ( sample(#1) )

But I can’t do union here cause CTE is not recognized there… rubbish. it is possible.

How can I solve it ?

SQL ONLINE

  • 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-17T22:52:55+00:00Added an answer on June 17, 2026 at 10:52 pm

    To avoid evaluating the itemType = 3 query twice you can self outer join then use CROSS APPLY ... VALUES to UNPIVOT

    DECLARE @itemType INT = 3;
    
    WITH T(item1, itemType1, item2, itemType2 )
         AS (SELECT *
             FROM   @t T1
                    LEFT JOIN @t T2
                      ON T1.item = T2.item
                         AND T2.itemType = 0
                         AND T1.itemType <> 0
             WHERE  T1.itemType = @itemType) 
    SELECT item,
           itemType
    FROM   T
           CROSS APPLY ( VALUES (item1, itemType1),
                                (item2, itemType2) ) v(item, itemType) 
    WHERE item IS NOT NULL
    

    SQL Fiddle

    Execution Plans

    Plans

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

Sidebar

Related Questions

I have simple SQL Server query: declare @table table (id int, name nvarchar(5), deleted
I have a simple table with autoincrementing ID's and one text column. id(int), value(nvarchar)
Let's say I have the following simple table variable: declare @databases table ( DatabaseID
I have a StoredProcedure that returns a simple table containing several records: DECLARE @STEPS_TABLE
Currently in a simple form i have the following declared table in code: declare
Very simplified, I have two tables Source and Target. declare @Source table (SourceID int
I have simple table in Sybase -- Creating table 'SimpleText' CREATE TABLE [dbo].[SimpleText] (
I have simple table creating script in Postgres 9.1. I need it to create
I have a simple table that has this structure: <table> <thead> <tr> <td>Some info</td>
I have a simple table maintaining messages between users. The table structure looks like

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.