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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:53:28+00:00 2026-05-27T00:53:28+00:00

I have a query that produces a result like this: The data is sorted

  • 0

I have a query that produces a result like this:

desired output

The data is sorted by date DESC, then by time DESC, but also the common ‘markers_name’ elements are chunked together. (They’re only chunked together for each date).

To do this I get the list of MAX(time) values for every combination of (markers_id, date) in conditions, then join that list to the row set I am getting from the present query, and use the MAX(time) values for sorting:

SELECT
  m.name AS markers_name,
  c.time AS conditions_time,
  c.date AS conditions_date,
  s.name AS station_name
FROM markers m
  INNER JOIN conditions c ON c.markers_id = m.id
  INNER JOIN station s    ON c.station_id = s.id
  INNER JOIN (
    SELECT
      markers_id,
      date,
      MAX(time) AS time
    FROM conditions
    GROUP BY
      markers_id,
      date
  ) mx ON c.markers_id = mx.markers_id AND c.date = mx.date
ORDER BY
  c.date DESC,
  mx.time DESC,
  m.name DESC,
  c.time DESC

Now I need to restrict user access to some of the rows.
The conditions table has a ‘private’ column. If ‘private’ is set to 1 only some people are allowed to see the query row. The people that can see it include the person that created the conditions report and that person’s contacts. The conditions table has a ‘user_id’, which contains the id of the person that created the conditions report. The contacts are obtained from a ‘contacts’ table which has two fields: ‘user_id’ and ‘friend_id’. I have the user_id of the person requesting the information.

To restate another way, I need to do something like this:

  1. Do the query above, but also check to see if c.private is set to one.
  2. If c.private is set to one use c.user_id to get that user’s ‘friend_id’s from the contacts table.
  3. If the user_id of the person requesting the information matches c.user_id or any of c.user_id’s friends return the query row. If not, don’t return that row or return something that indicates that the row is private (I could have a markers_name with the name “Private” in the database, for example.

If anyone has any ideas I would love to hear them. I struggled with this all day yesterday. Thank you.

  • 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-27T00:53:29+00:00Added an answer on May 27, 2026 at 12:53 am

    Possibly just adding a filter like this would be enough:

    …
    WHERE c.Private <> 1
       OR c.user_id = @user_id
       OR c.user_id IN (
         SELECT friend_id
         FROM contacts WHERE user_id = @user_id
       )
    

    where @user_id is the ID of the person requesting the info.

    This would prohibit the private rows from appearing in the output where inappropriate.

    If, however, you’d rather like them to be there but be marked as Private (for being dealt with later in the client or something like that), you could use that filter as a column in your SELECT clause:

    SELECT
      NOT (
        c.Private <> 1
        OR c.user_id = @user_id
        OR c.user_id IN (
          SELECT friend_id
          FROM contacts WHERE user_id = @user_id
        )
      ) AS IsPrivate,
      …
    FROM …
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that produces a result like this: The data is sorted
I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
I have a query that looks like this: public IList<Post> FetchLatestOrders(int pageIndex, int recordCount)
I have the following LINQ conditional where clause query that produces a result of
I have a select query that currently produces the following results: Description Code Price
Dear all, I have a select query that currently produces the following results: DoctorName
Here is a silly question. Lets say I have a query that produces for
I have a table of results that looks like this: score_id, uid, af_id, level,
I have 1 table Products that looks like this: ID Product Shop Color 01
Possible Duplicate: Insert line into a query result (sum) I have a report that

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.