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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:45:11+00:00 2026-06-03T14:45:11+00:00

I have a table with data like this. Id PId Device Status Type Created_Date

  • 0

I have a table with data like this.

Id       PId         Device             Status          Type      Created_Date
===      ===         ======             ======          ====      ============
1         2           1                  today          High      2012-04-12 08:11:51.583
2         2           4                  today          Medium    2012-04-02 01:39:52.393
3         3           5                  today          Medium    2012-04-02 01:10:02.443
4         2           6                  today          High      2012-04-02 01:05:25.063
5         2           3                  today          High      2012-04-02 01:03:08.360
6         2           7                  today          High      2012-04-02 01:02:57.093
7         2           2                  today          High      2012-04-02 00:22:37.807

Now, I want the records of Device 6 and 7 always on the top of the record set with descending order of created date. And records with device except 6 and 7 order by Type and Created Date Descending after the records of device type 6 and 7.

So the desired result is like below :

Id       PId         Device             Status          Type      Created_Date
===      ===         ======             ======          ====      ============
4         2           6                  today          High      2012-04-02 01:05:25.063
6         2           7                  today          High      2012-04-02 01:02:57.093
1         2           1                  today          High      2012-04-12 08:11:51.583
5         2           3                  today          High      2012-04-02 01:03:08.360
7         2           2                  today          High      2012-04-02 00:22:37.807
2         2           4                  today          Medium    2012-04-02 01:39:52.393

I have used the query as below :

select * from TblAlert where PId=2 and ( Device=6 OR Device=7) and ( Status='Today' or Status=0)
UNION
Select * from TblAlert Where  PId=2 and ( Device<>6 OR Device<>7)and (Status='Today' or Status=0)
order by Type,Created_Date desc 

but its not working as it is applying order by clause on whole record set.

Can anybody help me regarding this please?

  • 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-03T14:45:13+00:00Added an answer on June 3, 2026 at 2:45 pm

    Solved it my self by Using Table variable as

    DECLARE @Records TABLE
    (
       Id  int,
       PId int,
       Device int,
       Status Alert_Type varchar(10),
       Type Alert_Type varchar(5), 
       Created_Date DateTime
    ); 
    
    INSERT @Records 
        SELECT Id, PId, Device, Status,Type,Created_Date FROM 
            TblAlert 
        WHERE 
            PId =2
            AND Device IN (6,7) 
            AND ( Status='Today' or Status=0) 
        ORDER BY 
            Created_Date DESC; 
    
    INSERT @Records 
        SELECT Id, PId, Device, Status,Type,Created_Date FROM 
            TblAlert 
        WHERE 
            PId =2
            AND Device NOT IN (6,7) 
            AND ( Status='Today' or Status=0) 
        ORDER BY 
            Type, Created_Date DESC; 
    
    SELECT * FROM @Records;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I'm a bit confused on this. I have a data table structured like this:
I have a table like this... CustomerID DBColumnName Data 1 FirstName Joe 1 MiddleName
I like to have a query like this: select data from table where (x
I have a table data like this: <table class=tablesorter id=dea> <thead> <tr> <th class=header
i have a table data like this (int) (time) [mySQL datatype] Id Duration ------------------
I have a table with data like this ID | second_col 1 | 1
I have UserScores Table with data like this: Id userId Score 1 1 10
I have table one which looks like this. And I want to get data
I have table data which looks like this id | keyword | count |

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.