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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:37:08+00:00 2026-06-09T14:37:08+00:00

In Oracle, it’s possible to get a count of distinct values in multiple columns

  • 0

In Oracle, it’s possible to get a count of distinct values in multiple columns by using the || operator (according to this forum post, anyway):

SELECT COUNT(DISTINCT ColumnA || ColumnB) FROM MyTable

Is there a way to do this in SQL Server 2008? I’m trying to perform a single query to return some group statistics, but I can’t seem to do it.

For example, here is a table of values I’m trying to query:

AssetId MyId    TheirId   InStock
328     10      10        1
328     20      20        0
328     30      30        0
328     40      10        0
328     10      10        0
328     10      10        0
328     10      10        0
328     10      10        0

For AssetId #328, I want to compute the total number of unique IDs in the MyId and TheirId columns (4 = 10, 20, 30, 40), as well as the total number of non-zero rows in the InStock column (1):

AssetId     TotalIds    AvailableIds
328         4           1

Is there a way to work this magic somehow?

  • 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-09T14:37:10+00:00Added an answer on June 9, 2026 at 2:37 pm

    You can use a cross apply and values.

    select T1.AssetId,
           count(distinct T2.ID) TotalIds,
           sum(case T2.InStock when 0 then 0 else 1 end) AvailableIds 
    from YourTable as T1
      cross apply(values(T1.MyId, T1.InStock),
                        (T1.TheirId, 0)
                 ) as T2(ID, InStock)
    group by T1.AssetId  
    

    SE-Data

    Or you can do a union all in a sub query.

    select T.AssetId,
           count(distinct T.ID) TotalIds,
           sum(case T.InStock when 0 then 0 else 1 end) AvailableIds 
    from (
         select AssetId, MyId as ID, InStock
         from YourTable
         union all
         select AssetID, TheirId, 0
         from YourTable
         ) as T
    group by T.AssetId  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Oracle pads values in char columns so if I insert a in a CHAR(2)
ORACLE does not permit NULL values in any of the columns that comprise a
Using Oracle, is it possible to indicate which rows are currently locked (and which
In oracle converting this date 2012-07-03 11:38:41 to unix_timestamp we get select (to_date('2012-07-03 11:38:41','YYYY-MM-DD
In Oracle SQL, how do I return count(*) column along side of a regular
In Oracle, we do this: def TNAME=&1 create table &TNAME (foo varchar(10)); How to
Reference:Oracle documentation Oracle Database enforces all PRIMARY KEY constraints using indexes Q's 1> I
Oracle's documentation on atomic access (at http://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html ) says this: a volatile variable establishes
Oracle SQL Developer allows you to update field values directly to the table without
Oracle's table server offers a built-in function, TRUNC(timestamp,'DY') . This function converts any timestamp

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.