How to find same value in same table without hardcoding in where clause?

shah

My table looks like this:

User  | Sale|  Year
-------------------------------
Kim   |  2  |  2019
Kim   |  2  |  2018
Kim   |  1  |  2017
Tim   |  3  |  2019
Tim   |  2  |  2018
Tim   |  1  |  2017
Jim   |  4  |  2019
Jim   |  3  |  2018
Jim   |  3  |  2017

There are many records in the table. I want to add the number of sales in last years for each user.

Result should be like this:

Name | Current Year | Previous Years(Sum)
Kim  |      2       |       3
Tim  |      3       |       3
Jim  |      4       |       6 

I have tried this so far but no luck

select 
User,
Sale as CurrentYearSale,
sum(case when Year < 2019 then cast(Sale as int) end) as PreviousYearsSale
from 
Test 
where Year =2019
group by User,Sale;

I cant hardcode the name

Update:

I am getting result like :

   User   CurrentYearSale  PreviousYear Sale
  ---------------------------------------------------
    Sam         3                    NULL
    Sam        NULL                  2
    Kim         4                    NULL
    Kim        NULL                   5
    Tim          2                   NULL
    Tim        NULL                    4 
mkRabbani

Try conditional aggregation as shown below-

SELECT [User],
SUM(CASE WHEN [Year] = 2019 THEN Sale ELSE 0 END) C_year,
SUM(CASE WHEN [Year] < 2019 THEN Sale ELSE 0 END) P_year
FROM your_table
GROUP BY  [User]

You can check DEMO HERE

Your given query should be written as below as well-

select [User],
sum(case when [Year] = 2019 then cast(Sale as int) ELSE 0 end) as   CurrentYearSale,
sum(case when [Year] < 2019 then cast(Sale as int) ELSE 0 end) as PreviousYearsSale
from Test 
--where Year =2019
-- As your are calculating sum for previous year
-- you do not need to filter data by Year = 2019
group by [User];

You can check the output of your query HERE

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mysql left join with double same-table relation where clause

Where clause followed by FirstOrDefault returning the same value

Where clause if there are multiple of the same ID

How to use same list twice in WHERE clause?

How to write a WHERE clause that uses the MIN() value of a column in the same the table?

How to join SQL statements with different where clause on different column of same table

alias value in where clause for the same query

Finding data where the value is the same in two columns from the same table

Two different queries on the same table with the same WHERE clause

two select queries with different where clause on same table

Subquery with the same WHERE clause as query

SQL Multiple Joins on same table with where clause

How to get a value of same column which is being search with where clause

Combining two SELECT queries from same table but different WHERE clause

Update where value is max from same table

MYSQL - Subquery on same table in WHERE clause of UPDATE statement

how to select with where clause in same table but the string that get pass to table is in a same textbox but has different id

Make two images same height without hardcoding?

WHERE clause with the same field name

How can i set Height of navigation <div> same as height of circle <div> without hardcoding any value

Using the same table on SELECT and WHERE clause

Sequeilize find highest value of table in where clause

PostgreSQL - How to find the same value in multiple table and update the table?

SQL Query where clause for multiple columns in same table

how to find the same value if their length are not the same?

Recursive join to the same table using where clause from additional table

How to update certain column in sql using select and where clause on the same table?

T-SQL - How to select two columns data of same table by grouping and using two different where clause

How to make all the items in the **LazyVerticalGrid** to the same height without hardcoding height?