MS SQL Development
 
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 ForumsDatabasesMS SQL Development

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 August 15th, 2012, 10:29 PM
maluktuk maluktuk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 5 maluktuk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 31 sec
Reputation Power: 0
Working with Variables

I'm trying my hand at using variables, and unfortunately I think I'm missing a key principle somewhere. The following query worked before I tried to declare, set, and then use variables to calculate a margin percentage. Is there a glaring error in this that, once corrected, will make this work as desired?

Thanks in advance for the help.

Code:
DECLARE @i1 decimal(10,2)
DECLARE @i2 decimal(10,2)

SET @i1 = ct.ordamt
SET @i2 = (ct.ordamt - ct.costamt)

SELECT COUNT (ct.ordno) AS "# of Orders"
     , SUM (ct.ordamt) AS "Total"
     , SUM (ct.ordamt - ct.costamt) AS "Margin"
     , @i2 / @i1 AS "Margin %"
   FROM ordrhist AS ct
WHERE
ct.orddate = curdate() - 0 AND SUBSTRING(ct.status,1,1)<>'C'AND SUBSTRING(ct.status,2,1)='O' AND ct.hold <> 'Y' AND ct.complete <> 'I'

Reply With Quote
  #2  
Old August 16th, 2012, 04:09 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,352 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 9 h 53 m 26 sec
Reputation Power: 390
I assume that you expect that when using

Code:
, @i2 / @i1


it should be substituted by

Code:
ct.ordamt/(ct.ordamt - ct.costamt)


when executing the select statement. Variables does not work that way. Variables can only hold values, e.g. strings and integer, not a column reference. I think that attempting to use variables that way, if that is your intention, only makes the code less readable.

Reply With Quote
  #3  
Old August 16th, 2012, 08:32 AM
maluktuk maluktuk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 5 maluktuk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 31 sec
Reputation Power: 0
Quote:
Originally Posted by swampBoogie
I assume that you expect that when using

Code:
, @i2 / @i1


it should be substituted by

Code:
ct.ordamt/(ct.ordamt - ct.costamt)


when executing the select statement. Variables does not work that way. Variables can only hold values, e.g. strings and integer, not a column reference. I think that attempting to use variables that way, if that is your intention, only makes the code less readable.


SwampBoogie,

You assumed correctly. Thanks for letting me know about variables -- I'm definitely new to this and am grateful to learn everything I can. I had originally tried something like that before but I kept getting an error. This is what I was trying:

Code:
SELECT COUNT (ct.ordno) AS "# of Orders"
     , SUM (ct.ordamt) AS "Total"
     , SUM (ct.ordamt - ct.costamt) AS "Margin"
     , (ct.ordamt - ct.costamt) / ct.ordamt AS "Margin %"
   FROM ordrhist AS ct
WHERE
ct.orddate = curdate() - 0 AND SUBSTRING(ct.status,1,1)<>'C'AND SUBSTRING(ct.status,2,1)='O' AND ct.hold <> 'Y' AND ct.complete <> 'I'


If I leave out the third SELECT statement, everything works fine. It's when I try to calculate a margin percentage that I get an error. Any thoughts?

Reply With Quote
  #4  
Old August 16th, 2012, 09:09 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,442 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 15 m 59 sec
Reputation Power: 4141
the problem is, when you assign a column alias to an expression (like your two SUMs), you are not allowed to reference those column aliases in the same SELECT clause

two workarounds --

1. repeat the expressions
Code:
SELECT COUNT(*) AS "# of Orders"
     , SUM(ct.ordamt) AS "Total"
     , SUM(ct.ordamt - ct.costamt) AS "Margin"
     , ( SUM(ct.ordamt) -
         SUM(ct.ordamt - ct.costamt) ) / 
         SUM(ct.ordamt) AS "Margin %"
  FROM ordrhist AS ct
 WHERE ct.orddate = curdate() - 0 
   AND SUBSTRING(ct.status,1,1)<>'C'
   AND SUBSTRING(ct.status,2,1)='O' 
   AND ct.hold <> 'Y' 
   AND ct.complete <> 'I' 

2. push the query down into a subquery as a derived table
Code:
SELECT "# of Orders"
     , "Total"
     , "Margin"
     , ( "Total" - "Margin" ) / "Total" AS "Margin %"
  FROM ( SELECT COUNT(*) AS "# of Orders"
              , SUM(ct.ordamt) AS "Total"
              , SUM(ct.ordamt - ct.costamt) AS "Margin"
           FROM ordrhist AS ct
          WHERE ct.orddate = curdate() - 0 
            AND SUBSTRING(ct.status,1,1)<>'C'
            AND SUBSTRING(ct.status,2,1)='O' 
            AND ct.hold <> 'Y' 
            AND ct.complete <> 'I' ) AS dt
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #5  
Old August 16th, 2012, 10:00 PM
maluktuk maluktuk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 5 maluktuk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 31 sec
Reputation Power: 0
Quote:
Originally Posted by r937
the problem is, when you assign a column alias to an expression (like your two SUMs), you are not allowed to reference those column aliases in the same SELECT clause

two workarounds --

1. repeat the expressions
Code:
SELECT COUNT(*) AS "# of Orders"
     , SUM(ct.ordamt) AS "Total"
     , SUM(ct.ordamt - ct.costamt) AS "Margin"
     , ( SUM(ct.ordamt) -
         SUM(ct.ordamt - ct.costamt) ) / 
         SUM(ct.ordamt) AS "Margin %"
  FROM ordrhist AS ct
 WHERE ct.orddate = curdate() - 0 
   AND SUBSTRING(ct.status,1,1)<>'C'
   AND SUBSTRING(ct.status,2,1)='O' 
   AND ct.hold <> 'Y' 
   AND ct.complete <> 'I' 

2. push the query down into a subquery as a derived table
Code:
SELECT "# of Orders"
     , "Total"
     , "Margin"
     , ( "Total" - "Margin" ) / "Total" AS "Margin %"
  FROM ( SELECT COUNT(*) AS "# of Orders"
              , SUM(ct.ordamt) AS "Total"
              , SUM(ct.ordamt - ct.costamt) AS "Margin"
           FROM ordrhist AS ct
          WHERE ct.orddate = curdate() - 0 
            AND SUBSTRING(ct.status,1,1)<>'C'
            AND SUBSTRING(ct.status,2,1)='O' 
            AND ct.hold <> 'Y' 
            AND ct.complete <> 'I' ) AS dt


r937,

Thanks again for the help. The first one returned an error (I suspect an issue with the program I'm using), but the second one worked like a charm! That's actually what I had in mind in my original request but did such a poor job of verbalizing. I'm hoping to pick up more of this SQL lingo.

Is there a simple way to add a thousand separator to the "Total" and "Margin" amounts and limit the "Margin %" to two decimal points?

PS - I tried finding your book through the link in your signature? Is that still available, and would it be a good resource for a newb like me?

Reply With Quote
  #6  
Old August 17th, 2012, 03:34 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,442 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 15 m 59 sec
Reputation Power: 4141
Quote:
Originally Posted by maluktuk
Is there a simple way to add a thousand separator to the "Total" and "Margin" amounts and limit the "Margin %" to two decimal points?
FORMAT() -- if you're on SQL Server 2012

otherwise, it's better to do this in the front-end application

Quote:
Originally Posted by maluktuk
PS - I tried finding your book through the link in your signature? Is that still available, and would it be a good resource for a newb like me?
yes it's still available, and yes, you might benefit from it

the reason there's no link is too long a story to go into now

you can get it from amazon or from http://www.sitepoint.com/books/sql1/

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Working with Variables

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