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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:40:42+00:00 2026-06-05T19:40:42+00:00

My table structure is below : MyTable (ID Int, AccID1 Int, AccID2 Int, AccID3

  • 0

My table structure is below :

MyTable (ID Int, AccID1 Int, AccID2 Int, AccID3 int)

 ID      AccID1      AccID2      AccID3
----    --------    --------    --------
  1        12          2          NULL
  2        4           12           1
  3       NULL        NULL          5
  4        7          NULL          1   

I want to create indexed view with below output :

 ID    Level     Value
----   -----    -------
 1       1         12
 1       2         2 
 2       1         4
 2       2         12
 2       3         1
 3       3         5
 4       1         7
 4       3         1

EDIT :

My table is very huge and I want to have above output.
I can Get my query such as below :

Select  ID,
        Case StrLevel
            When  'AccID1' Then 1
            When  'AccID2' Then 2
            Else 3
        End AS [Level],
        AccID as Value
From    (
        Select A.ID, A.AccID1, A.AccID2, A.AccID3
        From MyTable A
        )as p
UNPIVOT (AccID FOR [StrLevel] IN (AccID1, AccID2, AccID3)) AS unpvt

or

Select *
from (
        select MyTable.ID,
                num.n as [Level],
                Case Num.n
                    When 1 Then MyTable.AccID1
                    When 2 Then MyTable.AccID2
                    Else MyTable.AccID3
                End AS AccID
        from myTable
        cross join (select 1
                    union select 2
                    union select 3)Num(n)
    )Z
Where Z.AccID IS NOT NULL

or

    Select  A.ID,
            2 AS [Level],
            A.AccID1 AS AccID
     From MyTable A
     Where A.AccID1 IS NOT NULL

    Union

    Select  A.ID,
            2 AS [Level],
            A.AccID2
     From MyTable A
     Where A.AccID2 IS NOT NULL

    Union

    Select  A.ID,
            3 AS [Level],
            A.AccID3
     From MyTable A
     Where A.AccID3 IS NOT NULL  

But Above query is slow and I want to have indexed view to have better performance.
and in indexed view I can’t use UNION or UNPIVOT or CROSS JOIN in indexed view.

  • 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-05T19:40:45+00:00Added an answer on June 5, 2026 at 7:40 pm

    What if you created a Numbers table to essentially do the work of your illegal CROSS JOIN?

    Create Table Numbers (number INT NOT NULL PRIMARY KEY) 
    Go
    
    Insert Numbers 
    Select top 30000 row_number() over (order by (select 1)) as rn
    from sys.all_objects s1 cross join sys.all_objects s2
    go
    
    Create view v_unpivot with schemabinding
    as  
    Select MyTable.ID,
            n.number as [Level],
            Case n.number
                When 1 Then MyTable.AccID1
                When 2 Then MyTable.AccID2
                Else MyTable.AccID3
            End AS AccID
    From dbo.Mytable
    Join dbo.Numbers n on n.number BETWEEN 1 AND 3 
    go
    
    Create unique clustered index pk_v_unpivot on v_unpivot (ID, [Level]) 
    go
    
    Select 
      ID,
      [Level],
      AccID
    From v_unpivot with (noexpand)
    Where AccID IS NOT NULL 
    Order by ID, [Level]
    

    The WHERE AccID IS NOT NULL must be part of the query because derived tables are not allowed in indexed views.

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

Sidebar

Related Questions

My table structure is below: CREATE TABLE [ACC].[Document]( [DocumentID] [int] IDENTITY(1,1) NOT NULL, [Date]
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
My table structure is: TABLE `licenses` ( `id` int(11) NOT NULL auto_increment, `code` varchar(30)
I have a table T (structure below) which initially contains all-NULL values in an
This is the structure of my table CREATE TABLE [dbo].[TableA] ( [ObjectID] [int] IDENTITY(1,1)
newb question there. Having MySQL database/tables structure below: CREATE DATABASE `dbTest` CREATE TABLE IF
Using the table structure below how would I create a bcp bulk insert that
I have a below table structure for friends table TABLE STRUCTURE CREATE TABLE `my_friends`
How to create below mentioned table structure in jquery/javascript: <table> <tr> <td> one </td>
My table structure is as shown below CREATE TABLE MCARE_HOUSE_DETAILS ( HOUSE_DTL_ID VARCHAR(20) NOT

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.