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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:18:49+00:00 2026-06-06T00:18:49+00:00

So I have a table like this Table 1 Quote Ref | Product A

  • 0

So I have a table like this

Table 1

 Quote Ref | Product A |  Product B  | Product C | Product D
-----------+-----------+-------------+-----------+-----------     
    12     |   222333  |  4748847478 |     0     |     0

I need to find out which Business Group this belongs to using the below Table .

I am not interested in fields for the Products that equal zero as the quote does not have those products so no Business Group to map . For this quote the Product A and B column have a non zero value and can be mapped to a business group. ( KEY POINT )

Table 2

Product Line |   Business Group 
Product A    |   Manfacturing 
Product B    | Tech Net

So I look to UNPIVOT data in Table 1 .

SELECT [QUOTE Ref], [Product Line], [Value]

FROM 
   (SELECT [QUOTE Ref], [Product A], [Product B], [Product C], [Product D]

FROM [Table1]) p
UNPIVOT
   ([Value] FOR [Product Line] 
   IN ([Product A], [Product B], [Product C], [Product D] )
)AS unpvt;

DATA for table 1 now like this

 Quote Ref | Product Line |   Value
-----------+--------------+------------
    12     |  Product A   |   222333
    12     |  Product B   | 4748847478
    12     |  Product C   |     0 
    12     |  Product D   |     0

The problem is that 40 k rows NOW becomes 4.7 million rows .

Now I know I dont need the rows in the unpivoted table where the value for a Product Line equals zero . How can I remove these entries in the unpivot query or is there something I could do to the base table before I even start the UNPIVOT ? My database is nto big enough to cope with about 20 similar tables and 60 extra million rows in the database.

  • 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-06T00:18:51+00:00Added an answer on June 6, 2026 at 12:18 am

    You should add the condition that removes the entries that have value = 0 in the final result set. You couldn’t do this in the SELECT query that produces the data, so enclose your current result set as subquery like this:

    ;WITH Data AS
    (
        SELECT [QUOTE Ref], [Product Line], [Value]
        FROM 
        (
            SELECT [QUOTE Ref], [Product A], [Product B], [Product C], [Product D]
            FROM [Table1]
        ) p
        UNPIVOT
           ([Value] FOR [Product Line] 
           IN ([Product A], [Product B], [Product C], [Product D] )
        )AS unpvt
     )
    SELECT * 
    FROM DATA 
    WHERE Value <> 0;
    

    DEMO

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

Sidebar

Related Questions

I have a table like this: name code group john 12 smith 15 how
I have a table like this one: id group value 1 GROUP A 0.641028
I have table like this : ID Name_1 Name_2 Name_3 1 Egon Spengler Ives
I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I have a Table like this one: |UserId | ContactID | ContactName --------------------------------------- |
I have a table like this... CustomerID DBColumnName Data 1 FirstName Joe 1 MiddleName
i have a table like this one: -------------------------------- id | name -------------------------------- 1 |
I have two table like this table_CN (_id, name, phone, favorite, title) table_EN (_id,
I have a table like this (using wordpress) +---------+----------+------------+ | meta_id | meta_key |
I have a table like this: <table id='inventory_table'> <tr id='no_items_avail'> <td> There are no

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.