Мы должны сравнить продажи по данным на основе параметра от и до даты в последние три месяца.
Create table Net_sales
(terminalid varchar(14),
Region varchar(20),
City varchar(50),
ICC_TRNS_COUNT int,
ICC_AMount money,
Trns_Date datetime)
--SELECT * FROM Net_sales
insert into Net_sales values ('INMAA031000000','SOUTH','CHENNAI',1,5000,'08/01/2019')
insert into Net_sales values ('INMAA031000000','SOUTH','CHENNAI',1,4000,'08/02/2019')
insert into Net_sales values ('INMAA031000000','SOUTH','CHENNAI',1,200,'08/04/2019')
insert into Net_sales values ('INAMD03900030G','WEST','Gujarat',1,52000,'08/01/2019')
insert into Net_sales values ('INAMD03900030G','WEST','Gujarat',1,40700,'08/02/2019')
insert into Net_sales values ('INAMD03900030G','WEST','Gujarat',1,2200,'08/04/2019')
insert into Net_sales values ('INAMD03900030G','WEST','Gujarat',1,52000,'09/01/2019')
insert into Net_sales values ('INAMD03900030G','WEST','Gujarat',1,47000,'09/02/2019')
insert into Net_sales values ('INAMD03900030G','WEST','Gujarat',1,2200,'09/10/2019')
insert into Net_sales values ('INAMD03900030G','WEST','Gujarat',1,52000,'10/01/2019')
insert into Net_sales values ('INAMD03900030G','WEST','Gujarat',1,70000,'10/02/2019')
insert into Net_sales values ('INAMD03900030G','WEST','Gujarat',1,3200,'10/10/2019')
-------------------
DECLARE @FROMDATE DATETIME
DECLARE @TODAY DATETIME
SET @FROMDATE='08/01/2019'
SET @TODAY='10/10/2019'
SELECT
TERMINALID ,
CITY,
REGION,
SUM(ICC_AMOUNT) ICASHDMRAMOUNT
FROM NET_SALES WHERE CONVERT(VARCHAR,TRNS_DATE,101) BETWEEN CONVERT(VARCHAR,@FROMDATE,101) AND CONVERT(VARCHAR,@TODAY,101)
GROUP BY TERMINALID ,
CITY,
REGION
ORDER BY TERMINALID
нам нужно ожидать и результат Ожидаемый результат
TERMINALID CITY REGION ICASHDMRAMOUNT_AUG ICASHDMRAMOUNT_SEP ICASHDMRAMOUNT_OCT
INAMD030001024 Gujarat WEST 94200 94200 94200
INAMD03900030G Gujarat WEST 94900 101200 125200
INMAA031000000 CHENNAI SOUTH 5000 0 0