PARTIAL SUM in table - tsql

I have followed table:
2016-01 111.00 100.00
2016-01 111.00 11.00
2016-02 222.00 100.00
2016-02 222.00 100.00
2016-02 222.00 22.00
2016-01 333.00 200.00
2016-01 333.00 133.00
I need:
2016-01 444.00
2016-02 222.00
How I can receive this data? Sum by period, but only distinct value?

USE SUM() WITH DISTINCT
SELECT DateColName ,SUM(DISTINCT ColName)
FROM TABLENAME
GROUP BY DateColName

SUM() + DISTINCT + GROUP BY are your friends.
Recreate your test case:
sqlite> CREATE TABLE money(month TEXT, q1 FLOAT, q2 FLOAT);
sqlite> INSERT INTO money VALUES('2016-01','111.00','100.00');
sqlite> INSERT INTO money VALUES('2016-01','111.00','11.00');
sqlite> INSERT INTO money VALUES('2016-02','222.00','100.00');
sqlite> INSERT INTO money VALUES('2016-02','222.00','100.00');
sqlite> INSERT INTO money VALUES('2016-02','222.00','22.00');
sqlite> INSERT INTO money VALUES('2016-01','333.00','200.00');
sqlite> INSERT INTO money VALUES('2016-01','333.00','133.00');
Check the data is same as yours:
sqlite> SELECT * FROM money;
2016-01|111.0|100.0
2016-01|111.0|11.0
2016-02|222.0|100.0
2016-02|222.0|100.0
2016-02|222.0|22.0
2016-01|333.0|200.0
2016-01|333.0|133.0
Run the query:
sqlite> SELECT month, SUM(DISTINCT q1) FROM money GROUP BY month;
2016-01|444.0
2016-02|222.0

;with cte
as
(select
date,col1,col2
,row_number() over (partition by date,col1 order by date) as rownum
)
select
date,sum(col1)
from cte
where rownum=1
group by date

Related

Cumulative Sum of 2 Columns

I am using Postgresql 11.
I have 2 tables - txn_table and summary_table
create table txn_table(id int, txn_date timestamp, amount decimal);
create table summary_table(id int, txn_date date, day_Total decimal, Cumulative_Total decimal);
Sample data for txn_table is
insert into txn_table values (1, '2020-05-28 10:05:05', 100.00);
insert into txn_table values(2, '2020-05-28 11:45:10', 200.00);
insert into txn_table values(3, '2020-05-29 10:05:05', 300.00);
insert into txn_table values(4, '2020-05-29 12:10:01', 400.00);
I want to insert the data in summary_table like below
day_total contains sum of particular day
Cumulative_total contains cumulative sum of day_total
| id | txn_date | day_total | cumulative_total |
| --- | ------------------------ | --------- | ---------------- |
| 1 | 2020-05-28 | 300 | 300 |
| 2 | 2020-05-29 | 700 | 1000 |
I guess this select could solve your problem:
insert into summary_table
select row_number() over w as id
, txn_date
, day_total
, sum(day_total) over w as cumulative_total
from (
select txn_date::date as txn_date, sum(amount) as day_total
from txn_table
group by txn_date::date
) d
window w as (order by txn_date)
order by txn_date
(I worked it out in my head, didn't try. Perhaps prepare db fiddle or CTE with sample input.)
do following:
Add unique constraint to txn_date column of summary_table.
create table summary_table(id SERIAL PRIMARY KEY, txn_date date unique, day_total decimal, cumulative_total decimal);
if you want to ignore the duplicates. use below mentioned query
insert into summary_table(txn_date,day_total,cumulative_total)
(select txn_date, day_total, sum(day_total) over (order by txn_date) as cumulative_total from (
select txn_date::date as txn_date, sum(amount) as day_total
from txn_table
group by txn_date::date
) d
order by txn_date)
on conflict(txn_date)
do nothing;
if you want to update the duplicate values in summary_table. use below mentioned query
insert into summary_table(txn_date,day_total,cumulative_total)
(select txn_date, day_total, sum(day_total) over (order by txn_date) as cumulative_total from (
select txn_date::date as txn_date, sum(amount) as day_total
from txn_table
group by txn_date::date
) d
order by txn_date)
on conflict(txn_date)
do update set day_total=EXCLUDED.day_total,
cumulative_total=EXCLUDED.cumulative_total;

Postgresql select date range between two tables

I have two tables that have date fields in them. I want to select data from table 1 where the date is +/- 1 day from any date in table 2.
try something like this :
select * from table1,table2
where table1.date BETWEEN (table2.date - '1 day'::interval)
AND (table2.date + '1 day'::interval)
and ...
If only +/- 1 day, you could use a workaround like this:
select col1, col2, ...
from table1
where date_col in (select distinct date_col
from table2
union all
select distinct (date_col - '1 day'::interval)
from table2
union all
select distinct (date_col + '1 day'::interval)
from table2
);
This has quite good peformance because the subquery only be calculated one time and will be cache for comparing

SQL Server : group by with corresponding row values

I need to write a T-SQL group by query for a table with multiple dates and seq columns:
DROP TABLE #temp
CREATE TABLE #temp(
id char(1),
dt DateTime,
seq int)
Insert into #temp values('A','2015-03-31 10:00:00',1)
Insert into #temp values('A','2015-08-31 10:00:00',2)
Insert into #temp values('A','2015-03-31 10:00:00',5)
Insert into #temp values('B','2015-09-01 10:00:00',1)
Insert into #temp values('B','2015-09-01 10:00:00',2)
I want the results to contains only the items A,B with their latest date and the corresponding seq number, like:
id MaxDate CorrespondentSeq
A 2015-08-31 10:00:00.000 2
B 2015-09-01 10:00:00.000 2
I am trying with (the obviously wrong!):
select id, max(dt) as MaxDate, max(seq) as CorrespondentSeq
from #temp
group by id
which returns:
id MaxDate CorrespondentSeq
A 2015-08-31 10:00:00.000 5 <-- 5 is wrong
B 2015-09-01 10:00:00.000 2
How can I achieve that?
EDIT
The dt datetime column has duplicated values (exactly same date!)
I am using SQL Server 2005
You can use a ranking subselect to get only the highest ranked entries for an id:
select id, dt, seq
from (
select id, dt, seq, rank() over (partition by id order by dt desc, seq desc) as r
from #temp
) ranked
where r=1;
SELECT ID, DT, SEQ
FROM (
SELECT ID, DT, SEQ, Row_Number()
OVER (PARTITION BY id ORDER BY dt DESC, seq DESC) AS row_number
FROM temp
) cte
WHERE row_number = 1;
Demo : http://www.sqlfiddle.com/#!3/3e3d5/5
With trial and errors maybe I have found a solution, but I'm not completely sure this is correct:
select A.id, B.dt, max(B.seq)
from (select id, max(dt) as maxDt
from #temp
group by id) as A
inner join #temp as B on A.id = B.id AND A.maxDt = B.dt
group by A.id, B.dt
Select id, dt, seq
From #temp t
where dt = (Select Max(dt) from #temp
Where id = t.Id)
If there are duplicate rows, then you also need to specify what the query processor should use to determine which of the duplicates to return. Say you want the lowest value of seq,
Then you could write:
Select id, dt, seq
From #temp t
where dt = (Select Max(dt) from #temp
Where id = t.Id)
and seq = (Select Min(Seq) from #temp
where id = t.Id
and dt = t.dt)

Remove Duplicates

I have a table like below:
SuppID AreaID SuppNo SupName SupPrice
------------------------------------------------
1 3 526 ANC 100
1 3 985 JTT 200
3 4 100 HIK 300
In the above table, for same SuppID(1) and same AreaID(3), different SuppNo are there (526 & 985) in two different rows.
In this scenario , I'd like to make those two rows into a single row with SuppNo field as blank.
Also my output result should display rows with all the columns.
Any Help?
This should get you started:
DECLARE #TABLE TABLE (SuppID INT, AreaID INT, SuppNo VARCHAR(5), SupName VARCHAR(5), SupPrice INT)
INSERT INTO #TABLE
SELECT 1,3,'526','ANC',100 UNION
SELECT 1,3,'985','JTT',200 UNION
SELECT 3,4,'100','HIK',300
-- select data before updates
SELECT * FROM #TABLE
-- add a row count by AreaID/SuppID
;WITH T1 AS
(
SELECT *
,SUM(1) OVER(PARTITION BY AREAID,SUPPID) AS ROWCNT
FROM #TABLE
)
-- set the SuppNo blank on rows that have more than 1 match
UPDATE T1 SET SuppNo='' WHERE ROWCNT>1
-- add a row # by AreaID/SuppID
;WITH T2 AS
(
SELECT *
,ROW_NUMBER() OVER(PARTITION BY AREAID,SUPPID ORDER BY AREAID,SUPPID) AS ROWID
FROM #TABLE
)
-- delete duplicate rows
DELETE
FROM T2
WHERE ROWID>1
-- select data after updates
SELECT * FROM #TABLE

display unique row from two tables

I have two tables (one for quarter one, one for quarter two), each of which contains employees who have bonus in that quarter. Every employee has a unique id in the company.
I want to get all employees who has bonus in either q1 or q2. No duplicate employee is needed. Both Id, and Amount are required.
Below is my solution, I want to find out if there is a better solution.
declare #q1 table (
EmployeeID int identity(1,1) primary key not null,
amount int
)
declare #q2 table (
EmployeeID int identity(1,1) primary key not null,
amount int
)
insert into #q1
(amount)
select 1
insert into #q1
(amount)
select 2
select * from #q1
insert into #q2
(amount)
select 1
insert into #q2
(amount)
select 11
insert into #q2
(amount)
select 22
select * from #q2
My Solution:
;with both as
(
select EmployeeID
from #q1
union
select EmployeeID
from #q2
)
select a.EmployeeID, a.amount
from #q1 as a
where a.EmployeeID in (select EmployeeID from both)
union all
select b.EmployeeID, b.amount
from #q2 as b
where b.EmployeeID in (select EmployeeID from both) and b.EmployeeID NOT in (select EmployeeID from #q1)
Result:
EmployeeID, Amount
1 1
2 2
3 22
SELECT EmployeeID, Name, SUM(amount) AS TotalBonus
FROM
(SELECT EmployeeID, Name, amount
from #q1
UNION ALL
SELECT EmployeeID, Name, amount
from #q2) AS all
GROUP BY EmployeeID, Name
The subselect UNIONS both tables together. The GROUP BY gives you one row per employee and the SUM means that if someone got lucky in both qs then you get the total. I'm guessing that's the right thing for you.
try this one:
SELECT EmployeeID
FROM EmployeeList
WHERE EmployeeID IN
(SELECT EmployeeID From QuarterOne
UNION
SELECT EmployeeID From QuarterTwo)
OR by using JOIN
SELECT EmployeeID
FROM EmployeeList a INNER JOIN QuarterTwo b
ON a.EmployeeID = b.EmployeeID
INNER JOIN QuarterTwo c
ON a.EmployeeID = c.EmployeeID
This will return all EmployeeID that has record in either quarter.
Try:
SELECT DISTINCT q1.EmployeeID --- Same as q2.EmployeeID thanks to the join
, q1.EmployeeName -- Not defined in OP source.
FROM #q1 AS q1
CROSS JOIN #q2 AS q2
WHERE q1.amount IS NOT NULL
OR q2.amount IS NOT NULL