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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:40:40+00:00 2026-06-15T11:40:40+00:00

I currently have this query using PIVOT generating a table like this: USER |

  • 0

I currently have this query using PIVOT generating a table like this:

  USER  |  DEC  |  NOV  |  OCT
---------------------------------
  bob   |   3   |   5   |   2
  jon   |   7   |   0   |   1 
  tim   |   4   |   2   |   6

What I would like to do but it looks like a stretch is to ORDER BY the results by the DEC value descending.

This is the query:

with Mth (st, nd) as ( 
  select DATEADD (M, datediff (m, 0,'2012-09-01'), 0), 
         DATEADD (M, DATEDIFF (m, 0, '2012-09-01') + 1, 0)   
  union all 
  select DATEADD (m, 1, st), 
         DATEADD (m, 1, nd) 
  from Mth 
  where nd <= DATEADD (m, datediff (m, 0, getdate()), 0)
) 
select * 
from 
( 
  select MONTH(Mth.st) Month, 
      U.USER, 
      COUNT(S.QRY_ID) Searches 
  FROM Mth 
  LEFT JOIN SEARCHES S 
    on Mth.st <= S.CREATED 
    and Mth.nd > S.CREATED 
  LEFT JOIN MEMBERS U 
    on U.AID = S.AID 
  GROUP BY YEAR(Mth.st), MONTH(Mth.st), U.HOLDER_LOGIN
) src 
pivot 
( 
  sum(searches) 
  for month in ([12],[11],[10]) 
) piv

Doing piv ORDER BY piv.Searches gives an error so is it possible to specify the column?

  • 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-15T11:40:41+00:00Added an answer on June 15, 2026 at 11:40 am

    Try this:

    with Mth (st, nd) as ( 
      select DATEADD (M, datediff (m, 0,'2012-09-01'), 0), 
             DATEADD (M, DATEDIFF (m, 0, '2012-09-01') + 1, 0)   
      union all 
      select DATEADD (m, 1, st), 
             DATEADD (m, 1, nd) 
      from Mth 
      where nd <= DATEADD (m, datediff (m, 0, getdate()), 0)
    ), Pivoted
    AS
    (     
        select * 
        from 
        ( 
          select MONTH(Mth.st) Month, 
              U.USER, 
              COUNT(S.QRY_ID) Searches 
          FROM Mth 
          LEFT JOIN SEARCHES S 
            on Mth.st <= S.CREATED 
            and Mth.nd > S.CREATED 
          LEFT JOIN MEMBERS U 
            on U.AID = S.AID 
          GROUP BY YEAR(Mth.st), MONTH(Mth.st), U.HOLDER_LOGIN
        ) src 
        pivot 
        ( 
          sum(searches) 
          for month in ([12],[11],[10]) 
        ) piv
    )
    SELECT * 
    FROM Pivoted
    ORDER BY Dec
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking to do a bit of refactoring... Using NHibernate I have this query currently
I currently have a query that looks like this: SELECT NON EMPTY ([Measures].[TOTAL]) ON
Currently I have an mysql table like this: +---------+----------+------+-----+---------+----------------+ | Field | Type |
If I have a query like this: String Category = HttpContext.Current.Request.QueryString[Product].ToString(); IQueryable<ItemFile> pressReleases =
I currently have database setup like so: CREATE TABLE `article` ( `id` int(11) NOT
I have this query (shown below) which currently uses temporary and filesort in order
I currently have a CSV that I have built using StringBuilder in C#. This
I have a table that looks like this. | path_id | step | point_id
I am currently sending a query variable to a php file like this: query
I currently have this code: $query = query(SELECT SUM(ER_read) FROM tblEread WHERE ER_ID='$sensor' AND

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.