If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > a small SQL help regrading a criteria

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-07-07, 21:03
mackenzo mackenzo is offline
Registered User
 
Join Date: Oct 2007
Location: Toronto, Ontario
Posts: 11
a small SQL help regrading a criteria

i want the ordline.qty * product.prodprice AS ordercost

where ordercost = > 150
but it does not work. Every time i type: where ordercost => 150, it would give me an error.
this is my full query:

select customer.custno, customer.custfirstname + " " + customer.custlastname as custfullname, ordertbl.ordno, ordertbl.orddate, employee.empno, employee.empfirstname + " " + employee.emplastname as empfullname, product.prodno, product.prodname, ordline.qty * product.prodprice AS ordercost from (((ordertbl inner join customer on ordertbl.custno = customer.custno) inner join employee on employee.empno = ordertbl.empno) inner join ordline on ordline.ordno=ordertbl.ordno) inner join product on product.prodno=ordline.prodno where (ordertbl.orddate = datevalue('01/23/2007')) and (making ordercost = > 150) ..................

Thanks for help in advance
Reply With Quote
  #2 (permalink)  
Old 10-07-07, 21:06
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
this has to be either microsoft access or sql server, it doesn't look very much like ANSI SQL

in any case...

one solution is to wrap the query in another SELECT

select * from ( select customer.custno, customer.custfirstname + " " + customer.custlastname as custfullname, ordertbl.ordno, ordertbl.orddate, employee.empno, employee.empfirstname + " " + employee.emplastname as empfullname, product.prodno, product.prodname, ordline.qty * product.prodprice AS ordercost from (((ordertbl inner join customer on ordertbl.custno = customer.custno) inner join employee on employee.empno = ordertbl.empno) inner join ordline on ordline.ordno=ordertbl.ordno) inner join product on product.prodno=ordline.prodno ) as d where orddate = datevalue('01/23/2007') and ordercost = > 150
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-07-07, 21:10
mackenzo mackenzo is offline
Registered User
 
Join Date: Oct 2007
Location: Toronto, Ontario
Posts: 11
sorry
im not sure where to ask
im using this in sql server and access
Reply With Quote
  #4 (permalink)  
Old 10-07-07, 21:12
mackenzo mackenzo is offline
Registered User
 
Join Date: Oct 2007
Location: Toronto, Ontario
Posts: 11
doesn't the star calls out all the rows of the tables?
i'll try
Reply With Quote
  #5 (permalink)  
Old 10-07-07, 21:14
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by mackenzo
doesn't the star calls out all the rows of the tables?
i'll try
no, the star "calls out" all the columns

in this case it's all the columns of the derived table, show here in blue --

select * from ( select customer.custno, customer.custfirstname + " " + customer.custlastname as custfullname, ordertbl.ordno, ordertbl.orddate, employee.empno, employee.empfirstname + " " + employee.emplastname as empfullname, product.prodno, product.prodname, ordline.qty * product.prodprice AS ordercost from (((ordertbl inner join customer on ordertbl.custno = customer.custno) inner join employee on employee.empno = ordertbl.empno) inner join ordline on ordline.ordno=ordertbl.ordno) inner join product on product.prodno=ordline.prodno ) as d where orddate = datevalue('01/23/2007') and ordercost = > 150
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 10-07-07, 21:14
mackenzo mackenzo is offline
Registered User
 
Join Date: Oct 2007
Location: Toronto, Ontario
Posts: 11
hey it works!
why does the * makes it work?
Reply With Quote
  #7 (permalink)  
Old 10-07-07, 21:16
mackenzo mackenzo is offline
Registered User
 
Join Date: Oct 2007
Location: Toronto, Ontario
Posts: 11
nm
you have explained.

select * from ( select customer.custno, customer.custfirstname + " " + customer.custlastname as custfullname, ordertbl.ordno, ordertbl.orddate, employee.empno, employee.empfirstname + " " + employee.emplastname as empfullname, product.prodno, product.prodname, ordline.qty * product.prodprice AS ordercost from (((ordertbl inner join customer on ordertbl.custno = customer.custno) inner join employee on employee.empno = ordertbl.empno) inner join ordline on ordline.ordno=ordertbl.ordno) inner join product on product.prodno=ordline.prodno ) where orddate = datevalue('01/23/2007') and ordercost = > 150

u had "as d" as typos =p
__________________
Reply With Quote
  #8 (permalink)  
Old 10-07-07, 21:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
it works because of the derived table, which uses column names as defined in its SELECT

the expression with the column alias ordercost becomes an actual column in the derived table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 10-07-07, 21:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
as d was not a typo
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 10-07-07, 21:18
mackenzo mackenzo is offline
Registered User
 
Join Date: Oct 2007
Location: Toronto, Ontario
Posts: 11
i took the as d out and it works too
and what's as d
sorry for being a noob
Reply With Quote
  #11 (permalink)  
Old 10-08-07, 14:32
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by mackenzo
i took the as d out and it works too
and what's as d
This sets "d" as the alias name (actually: table name) for the temporary "table" inside the parentheses.

According to standard SQL, every table (or view or nested table expression) must have a name. By having the "AS d" after the definition, it's as if you created a view, viz:
Code:
CREATE VIEW d (custno, custfullname, ordno, orddate, empno,
               empfullname, prodno, prodname, ordercost)
AS
  SELECT customer.custno,
         customer.custfirstname || ' ' || customer.custlastname,
         ordertbl.ordno,
         ordertbl.orddate,
         employee.empno,
         employee.empfirstname + " " + employee.emplastname,
         product.prodno,
         product.prodname,
         ordline.qty * product.prodprice
  FROM   ordertbl inner join customer on ordertbl.custno = customer.custno
          inner join employee on employee.empno = ordertbl.empno
          inner join ordline on ordline.ordno=ordertbl.ordno
          inner join product on product.prodno=ordline.prodno
Now this view can be interrogated:
Code:
SELECT *
FROM   d
WHERE  orddate = datevalue('01/23/2007')
  and  ordercost => 150
Rudy's query is exactly this, except for the fact that no view with the name "d" is ever created, it's just temporarily available for the scope of the current query. Such a "view" is often called a "nested table expression". It's to be written as
Code:
SELECT d.whatever
FROM   (SELECT whatever, ... -- the NTE
       ) AS d
WHERE  ...
Note the "d.whatever", where the table name "d" is used. That's why the "AS d" is needed: it's really the name of the NTE !
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #12 (permalink)  
Old 10-08-07, 15:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
thanks peter, i was away from the computer all day, but your answer was much better than mine would've been

and i call it a derived table instead of nested table expression
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #13 (permalink)  
Old 10-08-07, 15:45
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by r937
i call it a derived table instead of nested table expression
NTE is the DB2 terminology.
I'm probably a bit biased...
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On