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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:05:48+00:00 2026-06-13T23:05:48+00:00

I need to have a query that returns the ff: Count from the latest

  • 0

I need to have a query that returns the ff:

  1. Count from the latest Date in each Name
  2. If the value of Count from the latest Date is -1 then it will return the count of the Date before the latest Date
  3. If the value of Count from the latest Date is -1 and the other Date is -1. Then return 0
  4. If the value of Count from the latest Date is -1 and no other Date of that Name. Then return 0

Example Table:

ID  Name  Date         Count
1   Adj   09/29/2012   2
2   Adj   09/30/2012   4
3   Ped   09/29/2012  -1
4   Ped   09/30/2012   5
5   Mel   09/29/2012   3
6   Mel   09/30/2012  -1
7   Rod   09/30/2012   7
8   Ney   09/30/2012  -1
9   Jin   09/29/2012  -1
10  Jin   09/30/2012  -1

Desired Output:

Name   Count
Adj    4
Ped    5
Mel    3
Rod    7
Ney    0
Jin    0

I am very confused on how to approach this in SQL since I only knew simple query.

Any idea on how to make a query for this? Thanks.

Btw, I’m sorry I forgot to include this. I am using SQL Server 2000.

  • 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-13T23:05:48+00:00Added an answer on June 13, 2026 at 11:05 pm

    Try this

    SQL FIDDLE EXAMPLE

    select A.name, isnull(T.[Count], 0) as [Count]
    from (select distinct T.name from table1 as T) as A
        outer apply 
        (
            select top 1 T.[Count]
            from table1 as T
            where T.name = A.name and T.[Count] <> -1 
            order by T.[date] desc
        ) as T
    order by A.name asc
    

    UPDATE: for SQL 2000 you can use query like this

    SQL FIDDLE EXAMPLE for SQL 2000

    select A.name, isnull(T1.[Count], 0) as [Count]
    from 
    (
        select T.name, max(case when T.[Count] <> -1 then T.[date] else null end) as [date]
        from table1 as T
        group by T.name
    ) as A
        left outer join table1 as T1 on T1.name = A.name and T1.[date] = A.[date]
    

    but it relies on suggestion that you have unique constraint on name, [date] columns

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

Sidebar

Related Questions

I currently have a SQL query that returns a number of fields. I need
Possible Duplicate: mysql count into PHP variable I have the following query that returns
I need a linq statement that returns all entries from a certain date. A
I need a Postgresql Query that returns the count of every type of combination
I need to craft a query in mysql that will return 12 rows (one
I have an HQL query that returns projections of an entity including a count
I have a query that I need to execute that I do not know
I have a query that does what i want joining table but i need
Say I have a query that fetches [type][show_name]. For all [type]==5 records, I need
Using Microsoft SQL server 2008 I have a query that is in need of

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.