Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 16th, 2012, 09:34 AM
jrobb jrobb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 2 jrobb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 5 sec
Reputation Power: 0
Thumbs down SQL help: sub-queries from same table

Hi,
I used to be a member on this site, but could not remember my username. oh well.

I've written these queries a long time ago, and now I am trying to do something new for a different calculation.


My database is setup like this:
Code:
TI
TIID,SN,Date,Temp

Data
TIID,Freq,Voltage1,Voltage2,Gain, ...


The queries I have setup that run currently just go through and select some data based on defined conditions:

ie.
Code:
SELECT ti.TIID, TI.SN, Data.Freq, Data.Voltage1, Data.Voltage2, Data.Gain
FROM TI,Data
WHERE
TI.TIID= 2 AND
TI.TIID=Data.TIID and
Data.Freq= 6000 and
Data.Voltage1 = 10 and
Data.Voltage2 = 15;


That could probably be done quite a bit better as-is (and I welcome suggestions).

What I am trying to do now would be the equivalent of having 2 sets of conditions, and subtracting one from another.

maybe like this, but my SQL skills are very weak and out of practice:
Code:
SELECT (a.Gain - b.Gain) as newGain, a.TIID, a.SN, a.Freq, a.Voltage1, a.Voltage2 FROM
(
//copy in first query
  SELECT ti.TIID, TI.SN, Data.Freq, Data.Voltage1, Data.Voltage2, Data.Gain
  FROM TI,Data
  WHERE
  TI.TIID= 2 AND
  TI.TIID=Data.TIID and
  Data.Freq= 6000 and
  Data.Voltage1 = 10 and
  Data.Voltage2 = 15
) AS A JOIN
(
//same again as first query, with altered conditions
  SELECT ti.TIID, TI.SN, Data.Freq, Data.Voltage1, Data.Voltage2, Data.Gain
  FROM TI,Data
  WHERE
  TI.TIID= 2 AND
  TI.TIID=Data.TIID and
  Data.Freq= 6000 and
  Data.Voltage1 = 10 and
  Data.Voltage2 = 0
) AS B ON
(
//stuff that's the same
  A.TIID= B.TIID and 
  A.SN = B.SN
  A.Temp = B.Temp
  A.Freq = B.Freq and
  A.Voltage1 = B.Voltage1
)
;


I know that's probably terrible, but I'm struggling.
I need to return the newGain field, which is a subtraction of the gain fields from two separate sets of conditions. Only one or two fields will likely be different to differentiate them.

Can anyone offer assistance or suggestions?
Am I even on the right track here?

Thanks

Reply With Quote
  #2  
Old February 16th, 2012, 03:53 PM
jrobb jrobb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 2 jrobb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 5 sec
Reputation Power: 0
nevermind, got it figured out. join on one field, and i didn't even have the right field in there.

appears to be working now.

Reply With Quote
  #3  
Old March 6th, 2012, 10:47 PM
love4all1080 love4all1080 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 3 love4all1080 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 44 sec
Reputation Power: 0
no comment

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > SQL help: sub-queries from same table

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap