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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:27:20+00:00 2026-05-24T04:27:20+00:00

I have created a table like this: CREATE TABLE #TEMP(RecordDate datetime, First VARCHAR(255), Last

  • 0

I have created a table like this:

CREATE TABLE #TEMP(RecordDate datetime, First VARCHAR(255), Last VARCHAR(255), Value int)

INSERT INTO #TEMP VALUES('2011-03-01 00:00:00.000','john','smith','10')
INSERT INTO #TEMP VALUES('2011-03-01 00:00:00.000','john','adams','60')
INSERT INTO #TEMP VALUES('2011-03-01 00:00:00.000','john','resig','90')
INSERT INTO #TEMP VALUES('2011-03-01 00:00:00.000','john','balte','95')

INSERT INTO #TEMP VALUES('2011-03-01 01:00:00.000','john','smith','98')
INSERT INTO #TEMP VALUES('2011-03-01 01:00:00.000','john','adams','67')
INSERT INTO #TEMP VALUES('2011-03-01 01:00:00.000','john','resig','24')
INSERT INTO #TEMP VALUES('2011-03-01 01:00:00.000','john','balte','20')

SELECT * FROM #TEMP

DROP TABLE #TEMP

which now contains the following records:

RecordDate              First   Last    Value
2011-03-01 00:00:00.000 john    smith   10
2011-03-01 00:00:00.000 john    adams   60
2011-03-01 00:00:00.000 john    resig   90
2011-03-01 00:00:00.000 john    balte   95
2011-03-01 01:00:00.000 john    smith   98
2011-03-01 01:00:00.000 john    adams   67
2011-03-01 01:00:00.000 john    resig   24
2011-03-01 01:00:00.000 john    balte   20

I am trying to obtain a table like the following:

RecordDate                first    Good     Bad
2011-03-01 00:00:00.000   john     3        1
2011-03-01 01:00:00.000   john     2        2

The way I am computing Good and Bad is by taking the MAX of all people with the first name john on the specific date and then applying it as a filter on the original dataset for that particular date and first name. Only values greater than 0.5*MAXValue are considered Good.

In the result table, there are 3 good values because the maximum value for the first date was 95 and only 60,90,95 are greater than 0.5*95 so the result has (Good,Bad) = (3,1). In the second result, likewise, it is (2,2).

My table is sufficiently big and has close to 300 million records and I am not able to understand where to start to do this efficiently. Any suggestions on what an efficient way might look like?

My current (working but expensive) approach is give below:

SELECT    RecordDate
        , FirstName
        , 
        (
            SELECT COUNT(*) 
            FROM #TEMP
            WHERE Value > 0.5*(SELECT MAX(Value) FROM #TEMP WHERE RecordDate = A.RecordDate AND FirstName = A.FirstName)
            AND RecordDate = A.RecordDate AND FirstName = A.FirstName
        ) AS Good
        ,
        (
            SELECT COUNT(*) 
            FROM #TEMP
            WHERE Value < 0.5*(SELECT MAX(Value) FROM #TEMP WHERE RecordDate = A.RecordDate AND FirstName = A.FirstName)
            AND RecordDate = A.RecordDate AND FirstName = A.FirstName
        ) AS Bad
FROM #TEMP A
GROUP BY RecordDate, FirstName;
  • 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-24T04:27:21+00:00Added an answer on May 24, 2026 at 4:27 am

    Here you go:

    select 
       t.RecordDate,
       COUNT(case 
               when t.Value > MV.MaxValue * 0.5 then 1
               else null
             end) Good,
       COUNT(case 
               when t.Value <= MV.MaxValue * 0.5 then 1
               else null
             end) Bad
    from #Temp t inner join
    (select RecordDate, MAX(Value) MaxValue
     from #Temp Group By RecordDate) MV on t.RecordDate = MV.RecordDate
    Group by t.RecordDate
    

    The trick is creating a derived table with the max values for each record date and then INNER JOIN it with the table itself. Once you get the max values solved, you can access them directly.

    Update

    I see you updated your question and included the first name in the result. Never fear, here’s the solution:

    select 
       t.RecordDate,
       t.First,
       COUNT(case 
               when t.Value > MV.MaxValue * 0.5 then 1
               else null
             end) Good,
       COUNT(case 
               when t.Value <= MV.MaxValue * 0.5 then 1
               else null
             end) Bad
    from #Temp t inner join
    (select RecordDate, First, MAX(Value) MaxValue
     from #Temp Group By RecordDate, First) MV 
       on (t.RecordDate = MV.RecordDate and t.First = MV.First)
    Group by t.RecordDate, t.First
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL Server 2005 table like this: create table Taxonomy( CategoryId integer
If I have a table like this: CREATE TABLE sizes ( name ENUM('small', 'medium',
I have a table in PostgreSQL where the schema looks like this: CREATE TABLE
I have the following database table created thus: CREATE TABLE AUCTIONS ( ARTICLE_NO VARCHAR(20),
I have a table that was created using this mysql statement below. CREATE TABLE
I Have created a procedure which has code like this: Create PROCEDURE Sample( @ID
I have the following Query: create table #Result (Reward varchar(40), Value MONEY); insert #Result
Ok, I have one JavaScript that creates rows in a table like this: function
If I have a table like: CREATE TABLE FRED ( recordId number(18) primary key,
Let's say I have a payments table like so: CREATE TABLE Payments ( PaymentID

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.