简体   繁体   中英

Sum of two columns with join and group by another column

I need help with my T-SQL query. What I'm trying to do is the following:

In the result I want one column grouped by Store no. in the second column I want the sum of net Amount as Sales and the third column I want it to show sales from last year for each store no.

+-------+--------+------------+
| Store | Sales  | Last Year  |
+-------+--------+------------+
|  401  | 20000  |   19000    |
|  402  | 25000  |   21000    |
|  403  | 10000  |   15000    |
+-------+--------+------------+

Note: Sales from this year is in a table called "Trans_ Sales Entry" and the sales from last year is archived in a table called "Archived Sales Entry"

I also don't want to type in the date manually in the query, i would rather want a variable for example ("Today" and "Today"-365)

Is this the right way to do it, or are there better ways?

SELECT
   "Company$Trans_ Sales Entry"."Store No_" AS "Store",
   SUM("Company$Trans_ Sales Entry"."Net Amount"*-1) AS "Sales",
   SUM("Company$Archived Sales Entry"."Net Amount"*-1) AS "Last Year"
FROM "Company$Trans_ Sales Entry"
JOIN "Company$Archived Sales Entry" ON
  "Company$Trans_ Sales Entry"."Store No_"="Company$Archived Sales Entry"."Store No_"
WHERE 
  "Company$Trans_ Sales Entry"."Date"='2014-05-16' AND
  "Company$Archived Sales Entry"."Date"='2013-05-16'
GROUP BY
  "Company$Trans_ Sales Entry"."Store No_",
  "Company$Archived Sales Entry"."Store No_"

When I execute this query the numbers I get in "Sales" and "Last Year" are way too high. Something is wrong in my query... Hope someone can help!

If I execute this the sales figures is correct but then I have not included the Last year column:

 Select 
     [Company$Trans_ Sales Entry].[Store No_] As Store, 
     Sum([Company$Trans_ Sales Entry].[Net Amount]) As Sales 
 From 
     [Company$Trans_ Sales Entry] 
 Where 
     [Company$Trans_ Sales Entry].Date = '2014-05-16' 
 Group By 
     [Company$Trans_ Sales Entry].[Store No_]

There are multiple rows per store in both the trans and the archived tables that is why I need to sum all rows and group them per store in both tables.

I suspect you have more than 1 row per store in both the trans and the archived tables. If so, then your query is doomed to multiply results, as the engine first joins the tables creating a cartesian result of all trans rows and all archived rows of the same store, and then SUMs the values. So if you have 2 trans rows and 3 archived rows for one store, you will get 6 rows before the aggregation... and after aggregation and sum, "Sales" will be 3 times what you expect and "Last year" will be twice what you expect (for that store).

This query should solve your problem.

SELECT
  "Store No_" AS "Store",
  SUM("Net Amount"*-1) AS "Sales",
  (SELECT SUM("Net Amount"*-1)
    FROM "Company$Archived Sales Entry"
    WHERE
      "Store No_"="Company$Trans_ Sales Entry"."Store No_" AND
      "Date"='2013-05-16'

  )  AS "Last Year"
FROM "Company$Trans_ Sales Entry"
WHERE "Date"='2014-05-16'
GROUP BY "Store No_"

It aggregates and sums the Trans table on the store, adding for each store a column calculated as sum on the Archived table... a SUBSELECT.

Another approach is to first aggregate and sum the 2 tables, and then JOIN the result on the store:

SELECT t.Store, Sales, "Last year"
FROM (SELECT "Store No_" AS "Store", SUM("Net Amount"*-1) AS "Sales"
      FROM "Company$Trans_ Sales Entry"
      WHERE "Date"='2014-05-16'
      GROUP BY "Store No_") t
JOIN (SELECT "Store No_" AS "Store", SUM("Net Amount"*-1) AS "Last year"
      FROM "Company$Archived Sales Entry"
      WHERE "Date"='2013-05-16'
      GROUP BY "Store No_") a
ON a.Store=t.Store
ORDER BY t.Store

Notice that there is a difference in results if the Trans table and the Archived table do not contain all the same stores. The first will list all stores found in the Trans table. The second will list only stores found in both tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM