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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:03:12+00:00 2026-06-14T03:03:12+00:00

I have a temp table that stores rollup data and would like to create

  • 0

I have a temp table that stores rollup data and would like to create a query to see if I could clean up the data a little bit. I thought it would be pretty easy but it has turned out to be much more difficult. Maybe it is easy and I’m just approaching it all wrong.

What I’m trying to do is simply remove rows where Item = ‘TOTALS’ but where a name only shows up twice. So in the table below, rows 7 and 9 should be removed. Just so you know, there are no keys.

Name . Item ……Dlvrd
————————–
Bob . 102 ………. 18
Bob . 106 ………. 32
Bob . 108 ………. 34
Bob . 999 ………. 184
Bob .. TOTALS . 702
John . 64 ……….. 86
John . TOTALS . 86
Jim .. 112 ………. 131
Jim .. TOTALS .. 131
Jane . 10 ………. 720
Jane . 12 …….. . 217
Jane . 999 …….. 867
Jane . TOTALS 1848

My first attempt consisted of adding a column with the count of the name column, then using a where clause like: where name != ‘2’ and Item != ‘TOTALS’. Didn’t work, even putting parenthesis around them. It just removes any row with a 2 or where Item = ‘TOTALS’. It’s totally ignoring the ‘AND’. Here my sql:

select t.count, tr.*
From #TempRollup tr
join (
    select 
      Name
    , COUNT(Name) count
    from #TempRollup
    Group by Name
    ) as t on t.Name = tr.Name
where (
t.count != 2
and Item != 'TOTALS'
)

Then I tried this method that involves creating a subquery to find the rows I want to ignore and then just not selecting them. My subquery grabs the correct rows but when the whole thing is run, nothing is returned.:

Select *
FROM #TempRollup
WHERE NOT EXISTS(  
    select tr.*
    From #TempRollup tr
    join (
            select 
              Name
            , COUNT(Name) count
            from #TempRollup
            Group by Name
            ) as t on t.Name = tr.Name
    where t.count = 2
    and Item = 'TOTALS'
)
  • 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-14T03:03:13+00:00Added an answer on June 14, 2026 at 3:03 am

    Try this:

    SELECT t.*
    FROM #TempRollup t
    INNER JOIN
    (
        SELECT Name, COUNT(*) cnt
        FROM #TempRollup
        GROUP BY Name
    ) counts ON t.Name = counts.Name
    WHERE t.Item <> 'TOTALS' OR counts.cnt <> 2
    

    SQL Fiddle example

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

Sidebar

Related Questions

I have temp table #xml that have 2 columns: CREATE TABLE #xml ( id
I have written a DB2 query to do the following: Create a temp table
I have a stored procedure that first inserts some data into a temp table
I have a string like following: CREATE GLOBAL TEMPORARY TABLE some_temp_table_name or CREATE GLOBAL
I have a sample file like the following: CREATE GLOBAL TEMPORARY TABLE tt_temp_user_11 (
I have a temp table that needs the values of a Stored procedure. So
I have a stored procedure that use temp table and explicitly drops it when
I am using Oracle 11g r2 . I have a table that stores images
I have a table that stores formulary drug information and needs to be updated
I have a temp table that has numeric integer values in one column. I

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.