Note that there are some explanatory texts on larger screens.

plurals
  1. POVariable Declarations utilizing the WHERE clause
    primarykey
    data
    text
    <p>So I am relatively new to Microsoft's TSQL, and I am trying to write a simple script that bases an IF statement on whether the summation of column TransactionTotal is greater than or less than the summation of RepairCost for the prior 7 days.</p> <pre><code>USE MyBusinessDatabase DECLARE @TotalSales money DECLARE @TotalServices money DECLARE @WeekPast date SELECT @WeekPast = DATEADD(day, -7, GetDate()) </code></pre> <p>Now, the following Select/Where statements are where my problem is. The Select statements, which define my TotalSales and TotalServices variables, work fine until I add the Where statements. These LOOK right to me from what I've learned, but my IF statement ceases to work when I add them to the code.</p> <pre><code>SELECT @TotalSales = SUM(TransactionTotal) FROM Sales WHERE TransactionDate &lt;&gt; NULL AND PaymentDate &gt; @WeekPast SELECT @TotalServices = SUM(PaymentTotal) FROM ServiceInvoices WHERE PaymentDate &lt;&gt; NULL AND PaymentDate &gt; @WeekPast IF @TotalSales &gt; @TotalServices PRINT 'Total Sales &gt; Total Services' IF @TotalSales &lt; @TotalServices PRINT 'Total Sales &lt; Total Services' IF @TotalSales = @TotalServices PRINT 'Total Sales = Total Services' </code></pre> <p>Thanks a LOT and let me know if I can help clarify anything! PS As a big fan of my data structures C/C++ classes I find this SQL database class horribly redundant and boring and I am glad to get rid of it! I feel like I could enjoy it, but I really struggled with the class due to it being an afterthought with my algorithm and math courses eating my time.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload