EXTREME Overclocking Forums
Home | Reviews | Forums | Downloads | $ EXTREME Deals $ | RealTime Pricing | Free Magazines | Gear | Folding Stats Newsletter | Contact Us


Go Back   EXTREME Overclocking Forums > Software Discussion > Programming, DBMS, HTML, Web Related
Register Forum Rules FAQ Search Today's Posts Mark Forums Read

Welcome Guest Visitor! Please Register, It's Free and Fun To Participate!
The EXTREME Overclocking Forums are a place for people to learn how to overclock and tweak their PC's components like the CPU, memory (RAM), or video card in order to gain the maximum performance out of their system. There are lots of discussions about new processors, graphics cards, cooling products, power supplies, cases, and so much more!

You are currently viewing our boards as a "guest" which gives you limited access to view most discussions. You need to register before you can post: click the register link to proceed. Before you register, please read the forum rules. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own pictures, and access many other special features. Registration is fast, simple, and absolutely free! To start viewing messages, select the forum that you want to visit from the selection below.

After you have registered and read the forum rules, you can check out the FAQ for more information on using the forum. We hope you enjoy your stay here!

Note To Spammers: We do not allow unsolicited advertising! Spam is usually reported & deleted within minutes of it being posted, so don't waste your time (or ours)!


Please Register to Post a Reply
 
Thread Tools
Old 06-27-2012, 01:11 AM   #1
zlander79
Waldeinsamkeit
zlander79's Avatar
Senior Member
 
Posts: 745
Last Seen: 12-21-2012
iTrader: 0 / 0%
Another SQL Question

With the database I am working with, there are two objects with a one to many relationship (each gene can have a bunch of binding sites). I want to make a query that will only return the highest score (a column in binding site) binding site for each gene. I'm not sure how to go about this though. Any Ideas?
United States  Offline
    Register to Reply to This Post
Old 06-27-2012, 07:08 AM   #2
Vertigo Boost
i you.clone();
Vertigo Boost's Avatar
Senior Member
 
Posts: 1,462
Last Seen: Yesterday
Age: 27
From: Mooresville, NC
iTrader: 23 / 100%
Shot in the dark, but try returning with MAX?

http://www.techonthenet.com/sql/max.php
United States  Offline
    Register to Reply to This Post
Old 06-27-2012, 07:28 AM   #3
OS-Wiz
Old School
OS-Wiz's Avatar
Senior Member
 
Posts: 1,898
Last Seen: Yesterday
From: St. Louis MO USA
iTrader: 5 / 100%
MAX might work, but that's based on the complexity of the query. Can you show us the two table schemas please? And then state, in words, exactly what you need. What version of SQL (Oracle, MySQL, SQL Server, whatever). More than likely you'll first have to get the MAX highest score, then join that result with the main query.

Something like (Oracle example):

WITH DATA AS MAX_SCORE
SELECT GENE, MAX(SCORE)
FROM BINDING
SELECT GENE.A, SCORE.B
FROM GENE A, MAX_SCORE B
WHERE GENE.A = GENE.B

Last edited by OS-Wiz : 06-27-2012 at 07:37 AM. Reason: Added sample query
United States  Offline
    Register to Reply to This Post
Old 06-27-2012, 07:40 AM   #4
zlander79
Waldeinsamkeit
zlander79's Avatar
Senior Member
 
Posts: 745
Last Seen: 12-21-2012
iTrader: 0 / 0%
Code:
select `g`.`id`          AS `ID`,  
       `g`.`Name`        AS `NAME`, 
       `c`.`cog`         AS `COG`, 
       `o`.`ortho`       AS `ORTHO`, 
       `os`.`site`       AS `OPSITEID`, 
       `s`.`Sequence`    AS `SITE`, 
       max(`s`.`Score`)  AS `SCORE`, 
       `op`.`id`         AS `OPID`, 
       `op`.`prime_gene` AS `OPPRMGN`, 
       `go`.`ortho`      AS `ORTHO` 
 
from(((((((`METAGENO`.`GENE` `g`) 
      join `METAGENO`.`ORTHO` `o`) 
      join `METAGENO`.`GENE_COG` `c`) 
      join `METAGENO`.`OPERON_SITE` `os`) 
      join `METAGENO`.`OPERON` `op`) 
      join `METAGENO`.`GENE_ORTHO` `go`) 
      join `METAGENO`.`SITE` `s`) 
      where( 
                (`op`.`id`     = `g`.`Operon`) 
            and (`os`.`operon` = `g`.`Operon`) 
            and (`go`.`gene`   = `g`.`id`) 
            and (`c`.`gene`    = `g`.`id`) 
            and (`s`.`id`      = `os`.`site`)) GROUP BY `op`.`id`
This is what I was trying last night but I wasn't having any success. When I leave out the "GROUP BY `op`.`id` I just get a single entry for the highest score. I need the highest one per op.id.
United States  Offline
    Register to Reply to This Post
Old 06-27-2012, 05:13 PM   #5
OS-Wiz
Old School
OS-Wiz's Avatar
Senior Member
 
Posts: 1,898
Last Seen: Yesterday
From: St. Louis MO USA
iTrader: 5 / 100%
Your select is retrieving only those that match the where clause AND have the single highest score. In other words, if there where five entries for a match to your where clause, the select max(`s`.`Score`) is grabbing just the one of the five matched with the highest score. Can you use GROUP BY `op`.`id` HAVING MAX (`s`.`Score`) in your version of SQL syntax and leave out the select max(`s`.`Score`), just use `s`.`Score`?
United States  Offline
    Register to Reply to This Post
Old 06-29-2012, 12:38 PM   #6
zlander79
Waldeinsamkeit
zlander79's Avatar
Senior Member
 
Posts: 745
Last Seen: 12-21-2012
iTrader: 0 / 0%
Here is what I'm doing now, I will also run your suggestion and see what comes up.

Code:
select `g`.`id`       AS `ID`,  
       `g`.`Name`     AS `GENE`, 
       `o`.`id`       AS `OPID`, 
       `os`.`site`    AS `BSID`, 
       `s`.`Sequence` AS `SITE`, 
       `s`.`Score`    AS `SCORE` 
 
    from((((  
              `METAGENO`.`GENE`        `g` ) 
         join `METAGENO`.`OPERON`      `o` ) 
         join `METAGENO`.`OPERON_SITE` `os`) 
         join `METAGENO`.`SITE`        `s` ) 
      
        where( 
              (`o` .`id`     = `g` .`Operon`) 
           and(`os`.`operon` = `o` .`id`    ) 
           and(`s` .`id`     = `os`.`site`   
           and(`s` .`Score`  = (select max(`s2`.`Score`) 
                                    from(`METAGENO`.`SITE` `s2`) 
                                    where(`s2`.`id` = `os`.`site`))))) GROUP BY `o`.`id`
Additional Comment:

Quote:
Originally Posted by OS-Wiz View Post
Your select is retrieving only those that match the where clause AND have the single highest score. In other words, if there where five entries for a match to your where clause, the select max(`s`.`Score`) is grabbing just the one of the five matched with the highest score. Can you use GROUP BY `op`.`id` HAVING MAX (`s`.`Score`) in your version of SQL syntax and leave out the select max(`s`.`Score`), just use `s`.`Score`?
I get an error on the operator GROUP with:
Code:
select `g`.`id`       AS `ID`,  
       `g`.`Name`     AS `GENE`, 
       `o`.`id`       AS `OPID`, 
       `os`.`site`    AS `BSID`, 
       `s`.`Sequence` AS `SITE`, 
       `s`.`Score`    AS `SCORE` 
 
    from((((  
              `METAGENO`.`GENE`        `g` ) 
         join `METAGENO`.`OPERON`      `o` ) 
         join `METAGENO`.`OPERON_SITE` `os`) 
         join `METAGENO`.`SITE`        `s` ) 
      
        where( 
              (`o` .`id`     = `g` .`Operon`) 
           and(`os`.`operon` = `o` .`id`    ) 
           and(`s` .`id`     = `os`.`site`  ) GROUP BY `o`.`id` HAVING MAX(`s`.`Score`)
Additional Comment:

Forgot a paren.

Last edited by zlander79 : 06-29-2012 at 12:38 PM. Reason: Automerged Doublepost
United States  Offline
    Register to Reply to This Post
Old 06-29-2012, 01:38 PM   #7
OS-Wiz
Old School
OS-Wiz's Avatar
Senior Member
 
Posts: 1,898
Last Seen: Yesterday
From: St. Louis MO USA
iTrader: 5 / 100%
That should have solved your issue. yes?
United States  Offline
    Register to Reply to This Post
Old 06-29-2012, 02:12 PM   #8
zlander79
Waldeinsamkeit
zlander79's Avatar
Senior Member
 
Posts: 745
Last Seen: 12-21-2012
iTrader: 0 / 0%
Quote:
Originally Posted by OS-Wiz View Post
That should have solved your issue. yes?
Yeah, it works. Performance wise is there an advantage of one over the other?
United States  Offline
    Register to Reply to This Post
Old 06-29-2012, 07:01 PM   #9
OS-Wiz
Old School
OS-Wiz's Avatar
Senior Member
 
Posts: 1,898
Last Seen: Yesterday
From: St. Louis MO USA
iTrader: 5 / 100%
Quote:
Originally Posted by zlander79 View Post
Yeah, it works. Performance wise is there an advantage of one over the other?
I can only tell you that in Oracle, using the HAVING clause will perform better. Sub-Selects require extra work and a nested-loop join internally. If you have something similar to Oracle's EXPLAIN query, run it and it will show you which would be better for you.
United States  Offline
    Register to Reply to This Post
Old 07-06-2012, 08:13 AM   #10
zlander79
Waldeinsamkeit
zlander79's Avatar
Senior Member
 
Posts: 745
Last Seen: 12-21-2012
iTrader: 0 / 0%
I keep getting a DDL Statement Error on this one:
Code:
CREATE NEW ALGORITHM=UNDEFINED VIEW `METAGENO`.`COMP_REPORT_TEST` AS
 
SELECT 
 
#Table: GENE 
#Symbol: g 
#Column         Output Name 
`g`.`id`     AS `MH_gene_id`, 
`g`.`Name`   AS `MH_name`, 
`g`.`Operon` AS `Operon`, 
 
#Table: FILE 
#Symbol: f 
#Column       Output Name 
`f`.`Path` AS `MH_file`, 
 
#Table: SCAFFOLD 
#Symbol: sc 
`sc`.`Name` AS `MH_scaffold`, 
 
#Table: OPERON 
#Symbol: o 
#Column             Output Name 
`o`.`prime_gene` AS `Operon_Prime_Gene`, 
 
#Table: OPERON SITE 
#Symbol: os 
#Column             Output Name 
`os`.`site`      AS `Site`, 
`os`.`site_type` AS `Site_Type`, 
 
#Table: SITE 
#Symbol: s 
#Column           Output Name 
`s`.`Score`    AS `Site_Score`, 
`s`.`Sequence` AS `Site_Sequence`,  
 
#Table: MOTIF 
#Symbol: m 
#Column       Output Name 
`m`.`name` AS `Motif`, 
 
#Table: GENE_COGPROT 
#Symbol: gcp 
#Column                Output Name 
`gcp`.`eval_coef`  AS `E_coef`, 
`gcp`.`eval_power` AS `E_power`, 
 
#Table: REF_GENE_INFO 
#Symbol: rgi 
#Column                Output Name 
`rgi`.`gi`          AS `NCBI_gi`, 
`rgi`.`accession`   AS `NCBI_Accession`, 
`rgi`.`description` AS `NCBI_Description`, 
`rgi`.`product`     AS `NCBI_Product`, 
 
#Table: COGPROT_COG 
#Symbol: cpc 
#Column             Output Name 
`cpc`.`cog`     AS `COG`, 
`cpc`.`cogprot` AS `COG_Protein`, 
`cpc`.`cog_org` AS `COG_Protein_organism`, 
 
#Table: COG 
#Symbol: c 
#Column       Output Name  
`c`.`Name` AS `COG_Description`, 
 
#Table: COG_FUNCTIONAL_GROUP 
#Symbol: cfg 
#Column         Output Name 
`cfg`.`id`   AS `COG_Group`, 
`cfg`.`Desc` AS `COG_Group_Description`, 
 
#Table: COG_ORG 
#Symbol: co 
#Column         Output Name 
`co`.`taxid` AS `Tax_id`, 
`co`.`name`  AS `Organism_Name` 
 
 
from ((((((((((((( 
          `GENE`                 `g`   
     join `FILE`                 `f`  )  
     join `SCAFFOLD`             `sc` )  
     join `OPERON`               `o`  )  
     join `OPERON_SITE`          `os` )  
     join `SITE`                 `s`  )  
     join `MOTIF`                `m`  )  
     join `GENE_COGPROT`         `gcp`)  
     join `COGPROT_COG`          `cpc`)  
     join `COG`                  `c`  )  
     join `COG_GROUP`            `cg` )  
     join `COG_FUNCTIONAL_GROUP` `cfg`)  
     join `REF_GENE_INFO`        `rgi`)  
     join `COG_ORG`              `co` ) 
 
     where ( 
              (`g`  . `Operon`   = `o`  . `id`     )  
          and (`o`  . `File`     = `f`  . `id`     )  
          and (`o`  . `Scaffold` = `sc` . `id`     )  
          and (`o`  . `id`       = `os` . `operon` )  
          and (`os` . `site`     = `s`  . `id`     )  
          and (`s`  . `Motif`    = `m`  . `id`     )  
          and (`g`  . `id`       = `gcp`. `gene`   )  
          and (`gcp`. `cogprot`  = `cpc`. `gi`     )  
          and (`cpc`. `gi`       = `rgi`. `gi`     )  
          and (`cpc`. `cog`      = `c`  . `id`     )  
          and (`cpc`. `cog`      = `c`  . `id`     )  
          and (`c`  . `id`       = `cg` . `COG`    )  
          and (`cg` . `GROUP`    = `cfg`. `id`     )  
          and (`cpc`. `cog_org`  = `co` . `cog_org`)) 
   
     GROUP BY `o`.`id` HAVING MAX(`s`.`Score`)
United States  Offline
    Register to Reply to This Post
Old 07-06-2012, 08:39 AM   #11
OS-Wiz
Old School
OS-Wiz's Avatar
Senior Member
 
Posts: 1,898
Last Seen: Yesterday
From: St. Louis MO USA
iTrader: 5 / 100%
Sorry, can't help, not familiar with that version of SQL.
United States  Offline
    Register to Reply to This Post
Old 07-11-2012, 01:22 PM   #12
sysigy
Abandon All Hope!
sysigy's Avatar
Senior Member
 
Posts: 912
Last Seen: 05-24-2013
Age: 31
iTrader: 0 / 0%
What version of SQL are you using

I think there is a syntax error with your HAVING clause... I don't recognise the version of SQL you're using but if it was M$ SQL then the HAVING would need to be a logical test like:
Code:
HAVING [SOMETHING] = MAX('s'.'score')
That's just a guess because in M$ SQL, HAVING MAX('s'.'score') would cause a syntax error as there is no test being applied to the result of MAX()

Last edited by sysigy : 07-11-2012 at 01:37 PM.
United Kingdom  sysigy Folds For EOC!  Offline
    Register to Reply to This Post
Old 07-11-2012, 03:41 PM   #13
zlander79
Waldeinsamkeit
zlander79's Avatar
Senior Member
 
Posts: 745
Last Seen: 12-21-2012
iTrader: 0 / 0%
MySQL, whatever the latest version is. The thing is, if I take out the CREATE VIEW thing, and run the SQL as a query, I have no issues. As soon as I attempt to engineer a view, I get problems. Of course, this being MySQL Workbench there is nothing in the way of sytax/error highlighting, it just says something is wrong.
United States  Offline
    Register to Reply to This Post
Old 07-11-2012, 05:19 PM   #14
OS-Wiz
Old School
OS-Wiz's Avatar
Senior Member
 
Posts: 1,898
Last Seen: Yesterday
From: St. Louis MO USA
iTrader: 5 / 100%
Can you use the simple form of create view:

Code:
Create or replace view `METAGENO`.`COMP_REPORT_TEST` AS
select `g`.`id`       AS `ID`,  
       `g`.`Name`     AS `GENE`, 
       `o`.`id`       AS `OPID`, 
       `os`.`site`    AS `BSID`, 
       `s`.`Sequence` AS `SITE`, 
       `s`.`Score`    AS `SCORE` 
    from((((  
              `METAGENO`.`GENE`        `g` ) 
         join `METAGENO`.`OPERON`      `o` ) 
         join `METAGENO`.`OPERON_SITE` `os`) 
         join `METAGENO`.`SITE`        `s` ) 
        where( 
              (`o` .`id`     = `g` .`Operon`) 
           and(`os`.`operon` = `o` .`id`    ) 
           and(`s` .`id`     = `os`.`site`  ) 
        GROUP BY `o`.`id` HAVING MAX(`s`.`Score`)
I used this reference: http://dev.mysql.com/doc/refman/5.0/en/create-view.html

Last edited by OS-Wiz : 07-11-2012 at 05:29 PM.
United States  Offline
    Register to Reply to This Post
Old 07-11-2012, 10:00 PM   #15
jeremy
moo!
jeremy's Avatar
Senior Member
 
Posts: 819
Last Seen: 05-03-2013
Age: 25
From: near philadelphi
iTrader: 3 / 100%
Since the problem is probably in the view syntax (I've never used it)..
My hunch though if you're getting DDL errors is probably something related to the DDL fields. (I know im captain obvious lol)

This might not help you at all but I tried to clean the original query (just my preference, its easier to debug imo (less parens and backticks)..

Also capitalized field names scare me

Code:
SELECT g.id as ID,
g.Name as GENE,
o.id as OPID,
os.site as BSID,
s.Sequence as SITE,
s.Score as SCORE
FROM METAGENO.GENE g
JOIN METAGENO.OPERON o ON o.id = g.Operon
JOIN METAGENO.OPERTON_SITE os ON os.operon = o.id
JOIN METAGENO.SITE s ON s.id = os.site
GROUP BY o.id
HAVING MAX(s.Score);
United States  Offline
    Register to Reply to This Post
Sponsored Links:
Please Register to Post a Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

All times are GMT -6. The time now is 01:23 AM.

Copyright ©2000 - 2011, Jelsoft Enterprises Ltd.
Powered by vBulletin
Copyright ©2000 - 2011, EXTREME Overclocking