Feb
7
2022
ID/date_start/payment
1 2022-02-07 10:18:19.000 1.00
2 2022-02-07 10:18:19.000 2.00
3 2022-02-07 10:18:19.000 3.00
4 2022-02-07 10:18:19.000 4.00
5 2022-02-07 10:18:19.000 5.00
6 2022-02-06 10:18:19.000 6.00
7 2022-02-06 10:18:19.000 7.00
8 2022-02-06 10:18:19.000 8.00
9 2022-02-06 10:18:19.000 9.00
10 2022-02-05 10:18:19.000 10.00
SELECT a.date_start, a.payment,
(SELECT SUM(payment) as payment
FROM (SELECT x.date_start, SUM(x.payment) as payment FROM tmp_test as x GROUP BY x.date_start) b
WHERE b.date_start <= a.date_start
) AS b
FROM (SELECT y.date_start,SUM(y.payment) as payment FROM tmp_test as y GROUP BY y.date_start) a
date_start/payment/b
2022-02-05 10:18:19.000 10.00 10.00
2022-02-06 10:18:19.000 30.00 40.00
2022-02-07 10:18:19.000 15.00 55.00
see test data @ https://stackoverflow.com/questions/860966/calculate-a-running-total-in-sql-server/10309947
no comments | posted in MSSQL Database Development
Sep
9
2021
declare @schema varchar(50)
declare @table varchar(50)
declare @query nvarchar(500)
set @schema = 'dbo'
set @table = 'ACTY'
set @query = 'SELECT * FROM [DB_ONE].[' + @schema + '].[' + @table + '] '
EXEC sp_executesql @query
source: https://stackoverflow.com/questions/2838490/a-table-name-as-a-variable
no comments | posted in MSSQL Database Development
Apr
17
2021
select * from tbl_events_dates
where
1 =
(
CASE WHEN
tbl_events_dates.dateStart < CAST(cast(year(GETDATE()) as varchar)+'-09-01' AS datetime)
THEN
CASE WHEN
tbl_events_dates.dateStart >= CAST(cast(year(DATEADD(year,-1,GETDATE())) as varchar)+'-09-01' AS datetime)
and
tbl_events_dates.dateStart <CAST(cast(year(GETDATE()) as varchar)+'-09-01' AS datetime)
THEN 1 else 0 end
ELSE
CASE WHEN
tbl_events_dates.dateStart <=CAST(cast(year(GETDATE()) as varchar)+'-09-01' AS datetime)
and
tbl_events_dates.dateStart >= CAST(cast(year(DATEADD(year,1,GETDATE())) as varchar)+'-09-01' AS datetime)
THEN 1 else 0 end
END
)
no comments | posted in MSSQL Database Development
Mar
9
2021
SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0) AS StartOfMonthDate
SELECT EOMONTH(getdate()) AS EndOfMonthDate
no comments | posted in MSSQL Database Development
Feb
8
2021
SELECT *
FROM tableName
ORDER BY (CASE WHEN dateColumn < GETDATE()
THEN 1
ELSE 0
END) DESC, dateColumn ASC
source: https://stackoverflow.com/questions/12365246/order-by-date-with-past-dates-after-upcoming-dates
no comments | posted in MSSQL Database Development
Nov
4
2019
SELECT min(CONVERT(NVARCHAR(max), my_ntext_filed)) AS my_agg_ntext_filed FROM tbl_test GROUP BY id
no comments | posted in MSSQL Database Development
Sep
20
2019
SELECT *
FROM sys.dm_db_resource_stats
order by end_time desc;
no comments | posted in MSSQL Database Development
Sep
18
2019
SELECT sqltext.TEXT,
req.session_id,
req.status,
req.command,
req.cpu_time,
req.total_elapsed_time
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext
no comments | posted in MSSQL Database Development
Aug
28
2019
select 'set identity_insert ['+s.name+'].['+o.name+'] off'
from sys.objects o
inner join sys.schemas s on s.schema_id=o.schema_id
where o.[type]='U'
and exists(select 1 from sys.columns where object_id=o.object_id and is_identity=1)
Then copy & paste the resulting SQL into another query window and run
source: https://stackoverflow.com/questions/10116759/set-identity-insert-off-for-all-tables
no comments | posted in MSSQL Database Development
Mar
16
2019
SELECT
t.NAME AS TableName,
i.name as indexName,
p.[Rows],
sum(a.total_pages) as TotalPages,
sum(a.used_pages) as UsedPages,
sum(a.data_pages) as DataPages,
(sum(a.total_pages) * 8 ) / 1024 as TotalSpaceMB,
(sum(a.used_pages) * 8 ) / 1024 as UsedSpaceMB,
(sum(a.data_pages) * 8 ) / 1024 as DataSpaceMB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
WHERE
t.NAME NOT LIKE 'dt%' AND
i.OBJECT_ID > 255 AND
i.index_id <= 1
GROUP BY
t.NAME, i.object_id, i.index_id, i.name, p.[Rows]
ORDER BY
object_name(i.object_id)
https://stackoverflow.com/questions/1443704/query-to-list-number-of-records-in-each-table-in-a-database
no comments | posted in MSSQL Database Development