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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:10:17+00:00 2026-06-15T09:10:17+00:00

I have a table that looks like this: DECLARE @myTable TABLE (SampleID varchar(max), site

  • 0

I have a table that looks like this:

DECLARE @myTable TABLE (SampleID varchar(max), site varchar(max), SPDate date)  
  INSERT @myTable
    SELECT 'A-1', 1, '9/1/2012'   UNION ALL
    SELECT 'A-2', 1, '10/1/2012'  UNION ALL
    SELECT 'A-3', 1, '10/15/2012' UNION ALL
    SELECT 'A-5', 1, '5/1/2012'   UNION ALL
    SELECT 'A-4', 1, '1/1/2012'   UNION ALL
    SELECT 'B-1', 2, '11/1/2012'  UNION ALL
    SELECT 'B-2', 2, '8/1/2012'   UNION ALL
    SELECT 'B-3', 2, '5/1/2012'   UNION ALL
    SELECT 'B-4', 2, '4/1/2012'   UNION ALL
    SELECT 'C-1', 3, '10/1/2012'  UNION ALL
    SELECT 'C-2', 3, '10/15/2012' UNION ALL
    SELECT 'C-3', 3, '7/1/2012' ; 

What I would like to have is a query that gives me all the dates that are > 75 days from today and then sorted in descending order (e.g. the most recent date first).

So for my table, the output would look like this:

    Site1    Site2   Site3
     A-1      B-2     C-2
     A-5      B-3     C-3
     A-4      B-4

So I know how I can make the Site1, Site2 and Site3 queries separate like this

 SELECT SampleID FROM @myTable
     WHERE  DATEDIFF(DAY, SPDate, GETDATE()) > 75 AND
     site=1 ORDER BY SPDate DESC

How do I make these three queries into one table ?

Thanks !

  • 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-15T09:10:18+00:00Added an answer on June 15, 2026 at 9:10 am

    You can easily perform this using the PIVOT function. If you have only the 3 site values that you show above, then you can hard-code the values, similar to this:

    select [Site1], [Site2], [Site3]
    from
    (
      select SampleID, 
        'Site'+ cast(site as varchar(10)) SiteNumber
         ,row_number() over(partition by site order by spdate desc) rn
      from myTable 
      where DateDiff(day, spdate, getdate()) >75
    ) src
    pivot
    (
      max(SampleID)
      for SiteNumber in ([Site1], [Site2], [Site3])
    ) piv;
    

    See SQL Fiddle with Demo

    But is you have an unknown number of sites, then you can implement a dynamic sql version of this:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME('Site'+ site) 
                        from myTable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT ' + @cols + ' from 
                 (
                   select SampleID, 
                     ''Site''+ cast(site as varchar(10)) SiteNumber
                      ,row_number() over(partition by site order by spdate desc) rn
                   from myTable 
                   where DateDiff(day, spdate, getdate()) >75
                ) x
                pivot 
                (
                    max(SampleID)
                    for SiteNumber in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    Both generate the same result:

    | SITE1 | SITE2 |  SITE3 |
    --------------------------
    |   A-1 |   B-2 |    C-3 |
    |   A-5 |   B-3 | (null) |
    |   A-4 |   B-4 | (null) |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that looks like this: DECLARE @myTable TABLE (country varchar(max), code
I have query that looks like this declare @tmpTable Table(id int) insert into @tmpTable(id)
I have a table cell of type nvarchar(max) that typically looks like this: A03
I have a table that looks like this: id value AGA 0.211 AGA 0.433
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL
I have a table that looks like this: ID (pk,int) Col1 (nvarchar) Col2 (nvarchar)
I have a table that looks like this Id FirstName 5 Adam 6 Bob
I have a table that looks like this: table name: uno str_id | title
I have a table that looks like this Group Recipe Priority 0 A 400
I have a table that looks like this: nid vid tid 1 2 3

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.