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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:14:09+00:00 2026-05-29T09:14:09+00:00

There are two sql queries, I want to write as one query. the second

  • 0

There are two sql queries, I want to write as one query.
the second query display the count of distinct records where action_text like ‘%STAFF%’.
I tried using UNION but It did not work.

select date(dated) date_ord,
count(DISTINCT order_number) as orders_placed, 
sum(case when action_text like '%STAFF%' then 1 else 0 end) AS orders_staff,
sum(case when action_text in (
                'CBA Capture attempt',
                'GCO Capture attempt',
                'PPP Capture',
                'PPE Capture',
                'Staff CC capture',
                'Web CC capture',
                'Staff Finance WIRE authorized',
                'Staff Finance PO authorized',
                'Staff Finance COD authorized',
                'Authorized The CPIC') then 1 else 0 end)     AS     orders_manuallycaptured
 from stats.sales_actions
 group by date_ord 
 order by dated desc


SELECT COUNT(DISTINCT order_number) as unique_orderstouched,
date(dated) date_ords
FROM sales_actions
WHERE action_text like '%STAFF%' 
group by date_ords 
order by dated desc
  • 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-05-29T09:14:10+00:00Added an answer on May 29, 2026 at 9:14 am

    As far as I can tell, the only new column in the second query is COUNT(DISTINCT order_number) ... WHERE action_text LIKE '%STAFF%'.

    Then you can just add in a COUNT(DISTINCT IF(action-text LIKE '%STAFF%',order_number,NULL)) as unique_orderstouched to your original query.

    (Also I assume the table stats.sales_actions in your first query is the same as the table sales_actions in your second query?).

    You’d end up with:

    select date(dated) date_ord,
    count(DISTINCT order_number) as orders_placed, 
    sum(case when action_text like '%STAFF%' then 1 else 0 end) AS orders_staff,
    sum(case when action_text in (
                    'CBA Capture attempt',
                    'GCO Capture attempt',
                    'PPP Capture',
                    'PPE Capture',
                    'Staff CC capture',
                    'Web CC capture',
                    'Staff Finance WIRE authorized',
                    'Staff Finance PO authorized',
                    'Staff Finance COD authorized',
                    'Authorized The CPIC') then 1 else 0 end) AS orders_manuallycaptured,
    -- new line follows
    COUNT(DISTINCT IF(action-text LIKE '%STAFF%',order_number,NULL)) 
       AS unique_orderstouched
     from stats.sales_actions
     group by date_ord 
     order by dated desc
    

    The COUNT( DISTINCT IF( condition, order_number, NULL ) ) is like saying COUNT( DISTINCT order_number ) ... WHERE <condition> — you can also think of it as being like your SUM( CASE WHEN condition THEN 1 ELSE 0), but with a DISTINCT in it.

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

Sidebar

Related Questions

There are two databases in SQL Server 2005: One called A and another one
I have a SQL query that I'm currently solving by doing two queries. I
Suppose I have the two following Linq-To-SQL queries I want to refactor: var someValue1
We have two tables in a SQL Server 2005 database, A and B.There is
I want to update rows of a table in a specific order, like one
I am trying to write what should ostensibly, be relative easy SQL queries -
i have 2 tables(result of two separate SQL queries and this result will be
i have two sql queries. select EmployeeID,FName from [Mydatabase].[dbo].EMPLOYEE_TABLE where EmployeeID not in (select
In SQL Server 2000 and 2005: what is the difference between these two WHERE
There are two weird operators in C#: the true operator the false operator If

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.