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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:13:39+00:00 2026-06-02T17:13:39+00:00

I have an sql query where I check if a value is between a

  • 0

I have an sql query where I check if a value is between a max and a min value of a table. I’ve now implement this as follows:

SELECT spectrum_id, feature_table_id
FROM 'spectrum', 'feature' 
WHERE `spectrum`.msrun_msrun_id = 1
AND `feature`.msrun_msrun_id = 1
AND (SELECT min(rt) FROM `convexhull` WHERE `convexhull`.feature_feature_table_id =  `feature`.feature_table_id) <= scan_start_time 
AND scan_start_time <= (SELECT max(rt) FROM `convexhull` WHERE 'convexhull'.feature_feature_table_id = 'feature'.feature_table_id)
AND (SELECT min(mz) FROM `convexhull` WHERE `convexhull`.feature_feature_table_id = `feature`.feature_table_id) <= base_peak_mz 
AND base_peak_mz <= (SELECT max(mz) FROM `convexhull` WHERE `convexhull`.feature_feature_table_id = `feature`feature_table_id)

This is running very slowly, because I’m selecting from convexhull 4 times every time I run this query, so I tried to improve it using an inner join:

SELECT spectrum_id, feature_table_id 
FROM 'spectrum', 'feature'
INNER JOIN `convexhull` ON `convexhull`.feature_feature_table_id = `feature`.feature_table_id
WHERE `spectrum`.msrun_msrun_id = ? "+ 
AND `feature`.msrun_msrun_id = ? "+
AND min(`convexhull`.rt) <= scan_start_time "+
AND scan_start_time <= max(`convexhull`.rt) "+
AND min(`convexhull`.mz) <= base_peak_mz "+
AND base_peak_mz <= max(`convexhull`.mz)", spectrumFeature_InputValues)

However, the min() and max() statements can only be used after a select statement. How can I make the first query more efficient, so that I can get the min and max rt and mz without having to do 4 queries?

  • 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-02T17:13:40+00:00Added an answer on June 2, 2026 at 5:13 pm

    EDIT: had a few more mins and looked again and realised all the data comes from that one table so something like this should work

    SELECT 
        spectrum_id
        ,feature_table_id
    FROM 
        spectrum AS s
        INNER JOIN feature AS f
            on f.msrun_msrun_id = s.msrun_msrun_id
        INNER JOIN (select 
             feature_feature_table_id
             ,min(rt) AS rtMin
            ,max(rt) AS rtMax
            ,min(mz) AS mzMin
            ,max(mz) as mzMax
         FROM 
            convexhull
         GROUP BY 
             feature_feature_table_id
         ) AS t
         ON t.feature_feature_table_id = f.feature_table_id
    WHERE
        s.msrun_msrun_id = 1
        AND s.scan_start_time >= t.rtMin
        AND s.scan_start_time <= t.rtMax
        AND base_peak_mz >= t.mxMin
        AND base_peak_mz <= t.mzMax
    

    I think you want to select from the convexhull table and group by feature_feature_table_id getting the min and max rt within that grouping.

    you can then wrap that select in brackets give it a name (as t) and join to it.

    Hope this is enought to get you on the road.. if not create a sample schema here: http://sqlfiddle.com/

    and put in your query and i can modify it.

    as a side note, I think you wan to join these tables on a particular field rather than select from both with a where clause compare:

    SELECT spectrum_id, feature_table_id
    FROM 'spectrum', 'feature' 
    WHERE `spectrum`.msrun_msrun_id = 1
    AND `feature`.msrun_msrun_id = 1
    

    and:

    SELECT 
        spectrum_id
        ,feature_table_id
    FROM 
        spectrum AS s
        INNER JOIN feature AS f
            on f.msrun_msrun_id = s.msrun_msrun_id
    WHERE
        s.msrun_msrun_id = 1
    

    If i have got something wrong there let me know.

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

Sidebar

Related Questions

I have this SQL query: SELECT * FROM IMAGES WHERE IMAGENAME in ('IMG1', 'IMG2',
I have this SQL query select case when AllowanceId is null then 2 else
I have SQL query, which is working nice on Oracle and MSSQL. Now I'm
i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
I have an XML value and an XPath query like this: XML: <Firms> <Firm
As I had written in title, I have SQL query, run on Oracle DB,
I have an SQL query that takes the following form: UPDATE foo SET flag=true
I have a SQL query (MS Access) and I need to add two columns,
I have a sql query that runs super fast, around one second, when not
I have a SQL query that takes a very long time to run on

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.