Home > Net >  Can you explain the meaning of a minus in a sql select statement?
Can you explain the meaning of a minus in a sql select statement?

Time:09-03

I am working with SQL and i found this snippet, my question is: what does it mean those minus symbols (-) inside the select statement, i know is a kind of some trick, but i cant find information online about how it is used, please any insight would be welcome.

I am refering exactly to:

SELECT  - sum(st.sales)  AS sales
        - sum(st.orders) AS orders
        - sum(st.aov)    AS aov 

It seems to be related to ledger tables, if you have any documenation, blog or pdf please give me the link to check it. The full sql looks like:

INSERT INTO sales_test
WITH source_query AS --find the existing values in the ledger table and invert them
     (
                SELECT
                           st.og_date
                         , st.merchant
                         , st.store_name
                         , st.country
                         , st.kam
                         , st.class
                         , st.origin
                         , - sum(st.sales)  AS sales
                         , - sum(st.orders) AS orders
                         , - sum(st.aov)    AS aov 
                         , et.source_file_name
                         , et.source_file_timestamp
                FROM
                           sales_test st
                           INNER JOIN
                                      ext_sales_test et
                                      ON
                                                 city_hash(et.og_date, et.merchant, et.store_name, et.country, et.kam, et.class, et.origin) = city_hash(st.og_date, st.merchant, st.store_name, st.country, st.kam, st.class, st.origin)
                                                 AND st.og_date   = et.og_date
                                                 AND st.merchant   = et.merchant
                GROUP BY
                           st.og_date
                         , st.merchant
                         , st.store_name
                         , st.country
                         , st.kam
                         , st.class
                         , st.origin
                         , et.source_file_name
                         , et.source_file_timestamp
     )
   , union_query AS --if we union the incomming data with the inverted existing data, we get the difference that needs to be ledgered
     (
            SELECT *
            FROM
                   source_query
            UNION ALL
            SELECT *
            FROM
                   ext_sales_text
     )

CodePudding user response:

It makes the numeric value negative(if numeric value is negative, - - is positive), in your case it first performs the sum and then it makes it negative or positive:

As an example:

USE tempdb;  
GO  
DECLARE @Num1 INT;  
SET @Num1 = 5;  
SELECT @Num1 AS VariableValue, -@Num1 AS NegativeValue;  
GO  

Result set:

VariableValue NegativeValue  
------------- -------------  
5             -5  
  
(1 row(s) affected)  

Further info here

  •  Tags:  
  • sql
  • Related