Pivot by count in postgres or sql-server - sql-server

I have 2 sample tables, i need months group on rows and columns
rows group by #tnx table and columns month group by #act
and values filled with count(tnx_no)
create table #tnx(tnx_id int,tnx_date int,tnx_no)
insert into #tnx values(1,200140101,1),(1,200140101,2),(2,20130901,2),(2,20130802,2)
create table #act(act_id int,act_dt int)
insert into #act values(1,20140101),(1,20130809),(2,20140101),(2,20140203)
--i need this output sample output.
/*
-- the values are count(tnx_no)
0-6 monthns 0 1 2 3
months from #tnx 7-12 months 12 34 32 54
13-18 months 12 09 89 07
18+ months 11 100 190 190
0-6 months 7-12 months 13-18 months >18months
--months from #act
/*
The client app is in postgres,and sql server appreciate any help

Related

How to select records of same day as today from all months and years

I have sample data as below:
Date
Index
26-07-2022
26
26-06-2022
23
24-07-2022
12
19-06-2022
16
26-04-2022
01
26-05-2022
10
26-07-2022
12
I want to select data of latest day from each month. For example if today's date is 26-07-2022 then I want to select all records where date is 26th.
So my output should look like below:
Date
Index
26-07-2022
26
26-06-2022
23
26-04-2022
01
26-05-2022
10
26-07-2022
12
Do anybody know how can I achieve this. Thanks.
Assuming that the data type of the column [Date] is DATE, use the function DAY():
SELECT *
FROM tablename
WHERE DAY([Date]) = DAY(GETDATE())
AND [Date] <= GETDATE(); -- you may remove this if it is not needed
See the demo.

SQL query to calculate summation of row with previous row along with missing rows

This is my table
Month Year count of subscription per date
---- ---- ----------------------------
1 2010 10
2 2010 30
4 2010 20
7 2010 40
2 2011 60
Please need your help, I need to get the result like below table, summation second row with first row, same thing for another rows: Here some months are missed in above table got added with 0 as count of subscription column
Month Year count of subscription per date sum of subscription
---- --- ---------------------------- ---------------------
1 2010 10 10
2 2010 30 40
3 2010 0 40
4 2010 20 60
5 2010 0 60
6 2010 0 60
7 2010 40 100
8 2010 0 100
9 2010 0 100
10 2010 0 100
11 2010 0 100
12 2010 0 100
1 2011 0 100
2 2011 60 160
3 2011 0 160
and so on till
12 2011 0 160
You can try this way
create table #temp
(
[Month] int,
[year] int,
[count of subscription per date] int,
[sum of] int
)
declare #id int,#sumall int
set #id=1
set #sumall=0
while(#id<=12)
begin
if exists(select month from [your table] where month=#id)
begin
set #sumall=#sumall+(select count from [your table] where month=#id)
insert into #temp
select month,year,count,#sumall as summ from [your table] where month=#id
end
else
begin
insert into #temp
select #id,'2010',0,#sumall
end
set #id=#id+1
end
select * from #temp

Time Complexity of SQL Cursor

I am using CURSOR to implement the following in SQL Server, I am only iterating through the table - The time complexity will be O(n) I think (?). But everywhere I read about CURSOR, it says CURSOR is a bad practice. So is there a better way to implement the following ?
Existing Table
month value
1 92
4 20
9 92
New Table
month value
1 92
2 92
3 92
4 20
5 20
6 20
7 20
8 20
9 92
10 92
11 92
12 92
The use of cursor isn't (primarily) bad because it has poor time complexity, but because it is more error-prone and harder to read than a simple query. You are correct that iterating over a table via cursor is O(n).
On to your problem at hand. If you have the months (1..12) stored somewhere, say Months, then you can do it like this:
WITH matchingMonths AS (
SELECT m.month, MAX(mav.month) as matchedMonth
FROM Months m, MonthsAndValues mav
WHERE m.month >= mav.month
GROUP BY m.month
)
SELECT mm.month, mav.value
FROM matchingMonths mm
JOIN MonthsAndValues mav on mav.month = mm.matchedMonth
Without such a table Months, you could generate it on-the-fly:
WITH Months(month) AS (
SELECT 1
UNION ALL
SELECT month + 1 FROM Months WHERE month < 12
),
matchingMonths AS (
SELECT m.month, MAX(mav.month) as matchedMonth
FROM Months m, MonthsAndValues mav
WHERE m.month >= mav.month
GROUP BY m.month
)
SELECT mm.month, mav.value
FROM matchingMonths mm
JOIN MonthsAndValues mav on mav.month = mm.matchedMonth

How to run a 3 month sum of rows in SQL Server

I am trying to replicate this behavior in SQL Server 2014. I have total dollars being worked per month, but I want a new column that takes the dollars and sums it by the current and next 2 months and places the total in the current month row. How can I accomplish this with SQL server?
Date Dollars 3 MONTH SUM
1/1/2018 10 37
2/1/2018 12 32
3/1/2018 15 36
4/1/2018 5 36
5/1/2018 16 34
6/1/2018 15 23
7/1/2018 3 27
8/1/2018 5 40
9/1/2018 19 46
10/1/2018 16 27
11/1/2018 11 11
I tried:
SUM(dollars) OVER (ORDER BY date ROWS BETWEEN current row and 2 following) AS [3 month sum]
but the calculation wasn't accurate. Is this the proper way to do it and I am just not doing it correctly or is there a better method?
This seems to work, and look very simple to read
declare #Incomings table([Date] Datetime, Dollars int)
insert into #Incomings
values
('20180101', 10)
,('20180201', 12)
,('20180301', 15)
,('20180401', 5)
,('20180501', 16)
,('20180601', 15)
,('20180701', 3)
,('20180801', 5)
,('20180901', 19)
,('20181001', 16)
,('20181101', 11)
select * , (select SUM(Dollars) from #Incomings where [Date] between i.[Date] and DATEADD(MONTH,2,i.[Date]))
from #Incomings i

TSQL in sql server 2008 --writing stored procedure to merge records

Am wondering how to write a stored procedure to aggregate the rows in the data.
Sample data:
Start End id
1 2 01
2 3 01
3 0 01
3 4 02
4 7 02
In this I should merge the rows with the same id of the sequential rows and start attribute of beginning row is less than end value of the last row. Also should store them in a table
Output of sample input:
Start End id
1 3 01
3 0 01
3 7 02
The logic is failing with the procedure I wrote.
Assuming that your data is good (no gaps in start/end):
select MIN(Start) [Start],
MAX(End) [End],
id
from YourTable
group by id

Resources