TSQL Count Over a Window - tsql

Couldn't find a question/answer that was quite what I needed and couldn't find examples. I would like to use a window function.
I have the following schema that holds stored procs and the tables and columns used in those procs:
CREATE TABLE [dbo].[ProcedureDependencies](
[DatabaseName] [varchar](256) NOT NULL,
[ProcedureId] [int] NOT NULL,
[ProcedureSchemaName] [varchar](256) NOT NULL,
[ProcedureName] [varchar](256) NOT NULL,
[TableSchemaName] [varchar](256) NOT NULL,
[TableName] [varchar](256) NOT NULL,
[FieldName] [varchar](256) NOT NULL)
I want to count the number of times a table name shows up for different procedures.
I have been attempting variations of the following:
select
DatabaseName,
TableName,
count(tablename) over (partition by DatabaseName,ProcedureName) cnt
from ProcedureDependencies
order by cnt desc
However, I'm getting bad results. For instance, in the script that follow I want....
databasename tablename cnt
db1 tbl1 3
db1 tbl2 1
db1 tbl3 1
but I'm getting....
databasename tablename cnt
db1 tbl1 3
db1 tbl2 3
db1 tbl3 3
db1 tbl1 1
The script:
drop table #tmprmd;
create table #tmprmd (
DatabaseName varchar(max),
TableName varchar(max),
ProcedureName varchar(max),
FieldName varchar(max));
Insert Into #tmprmd
Values ('db1', 'tbl1', 'proc1', 'field1'),
('db1', 'tbl1', 'proc1', 'field2'),
('db1', 'tbl2', 'proc1', 'field1'),
('db1', 'tbl1', 'proc2', 'field1'),
('db1', 'tbl3', 'proc1', 'field1'),
('db1', 'tbl1', 'proc3', 'field1');
with
dist as (
select
--distinct
databasename,
procedurename,
tablename
from #tmprmd--ProcedureDependencies
)
select
distinct
DatabaseName,
TableName,
count(tablename) over (partition by DatabaseName,procedurename) cnt
from dist
order by cnt desc

I think you are making this harder than it needs to be
drop table #tmprmd;
create table #tmprmd (
DatabaseName varchar(max),
TableName varchar(max),
ProcedureName varchar(max),
FieldName varchar(max));
Insert Into #tmprmd
Values ('db1', 'tbl1', 'proc1', 'field1'),
('db1', 'tbl1', 'proc1', 'field2'),
('db1', 'tbl2', 'proc1', 'field1'),
('db1', 'tbl3', 'proc1', 'field1'),
('db1', 'tbl1', 'proc2', 'field1'),
('db1', 'tbl1', 'proc3', 'field1');
select dist.DatabaseName, dist.TableName, count(distinct(procedurename))
from #tmprmd as dist
group by dist.DatabaseName, dist.TableNameName

IF OBJECT_ID('Tempdb..#tmprmd') IS NOT NULL
DROP TABLE #tmprmd
CREATE TABLE #tmprmd
(
DatabaseName VARCHAR(MAX) ,
TableName VARCHAR(MAX) ,
ProcedureName VARCHAR(MAX) ,
FieldName VARCHAR(MAX)
);
INSERT INTO #tmprmd
VALUES ( 'db1', 'tbl1', 'proc1', 'field1' ),
( 'db1', 'tbl1', 'proc1', 'field2' ),
( 'db1', 'tbl2', 'proc1', 'field1' ),
( 'db1', 'tbl1', 'proc2', 'field1' ),
( 'db1', 'tbl3', 'proc1', 'field1' ),
( 'db1', 'tbl1', 'proc3', 'field1' );
----------------------------------------------------------
-- variant 1
WITH cte
AS ( SELECT DISTINCT
T.DatabaseName ,
T.TableName ,
COUNT(*) OVER ( PARTITION BY T.DatabaseName, T.ProcedureName, T.TableName ) cnt
FROM #tmprmd AS T
)
SELECT DISTINCT
DatabaseName ,
TableName ,
SUM(cte.cnt) OVER ( PARTITION BY DatabaseName, TableName ) cnt
FROM cte
----------------------------------------------------------
--variant 2
SELECT DISTINCT dist.DatabaseName,
dist.TableName,
MAX(cnt) OVER (PARTITION BY dist.DatabaseName, dist.TableName) cnt
FROM ( SELECT DISTINCT T.DatabaseName,
T.TableName,
DENSE_RANK() OVER (PARTITION BY T.TableName order by T.ProcedureName ) cnt
FROM #tmprmd AS T
) dist
----------------------------------------------------------
--variant 3
SELECT DISTINCT dist.DatabaseName,
dist.TableName,
COUNT(cnt) OVER (PARTITION BY dist.DatabaseName, dist.TableName) cnt
FROM ( SELECT DISTINCT T.DatabaseName,
T.TableName,
DENSE_RANK() OVER (PARTITION BY T.TableName order by T.ProcedureName ) cnt
FROM #tmprmd AS T
) dist
----------------------------------------------------------
-- Variant 4, without using window function
SELECT T.DatabaseName,
T.TableName,
COUNT(DISTINCT T.ProcedureName ) cnt
FROM #tmprmd AS T
GROUP BY T.DatabaseName,T.TableName

Related

Issue with PK violation on insert

I have a scenario where almost all of the tables have issues with the PK value as follows. This results is a database error or the violation of the PK insert.
When using the DBCC CheckIdent it displays an inconsistency between the next value and the current one.
Can anyone have a reason for the mismatch happening on several tables?
Since this database is then replicate, I'm afraid this error will propagate across the environment.
I adapted this script to fix it, but really trying to figure out the root of the problem.
/** Version 3.0 **/
if object_id('tempdb..#temp') is not null
drop table #temp
;
with cte as (
SELECT
distinct
A.TABLE_CATALOG AS CATALOG,
A.TABLE_SCHEMA AS "SCHEMA",
A.TABLE_NAME AS "TABLE",
B.COLUMN_NAME AS "COLUMN",
IDENT_SEED (A.TABLE_NAME) AS Seed,
IDENT_INCR (A.TABLE_NAME) AS Increment,
IDENT_CURRENT (A.TABLE_NAME) AS Curr_Value
, DBPS.row_count AS NumberOfRows
FROM INFORMATION_SCHEMA.TABLES A
inner join INFORMATION_SCHEMA.COLUMNS B on b.TABLE_NAME = a.TABLE_NAME and b.TABLE_SCHEMA = a.TABLE_SCHEMA
inner join sys.identity_columns IC on OBJECT_NAME (IC.object_id) = a.TABLE_NAME
inner join sys.dm_db_partition_stats DBPS ON DBPS.object_id =IC.object_id
inner join sys.indexes as IDX ON DBPS.index_id =IDX.index_id
WHERE A.TABLE_CATALOG = B.TABLE_CATALOG AND
A.TABLE_SCHEMA = B.TABLE_SCHEMA AND
A.TABLE_NAME = B.TABLE_NAME AND
COLUMNPROPERTY (OBJECT_ID (B.TABLE_NAME), B.COLUMN_NAME, 'IsIdentity') = 1 AND
OBJECTPROPERTY (OBJECT_ID (A.TABLE_NAME), 'TableHasIdentity') = 1 AND
A.TABLE_TYPE = 'BASE TABLE'
)
select 'DBCC CHECKIDENT ('''+A.[SCHEMA]+'.'+a.[TABLE]+''', reseed)' command
, ROW_NUMBER() OVER(ORDER BY a.[SCHEMA], a.[TABLE] asc) AS ID
, A.Curr_Value
, a.[TABLE]
into #temp
from cte A
ORDER BY A.[SCHEMA], A.[TABLE]
declare #i int = 1, #count int = (select max(ID) from #temp)
declare #text varchar(max) = ''
select #COUNT= count(1) FROM #temp
WHILE #I <= #COUNT
BEGIN
SET #text = (SELECT command from #temp where ID=#I)
EXEC (#text + ';')
print #text
select Curr_Value OldValue, ident_current([TABLE]) FixValue, [TABLE] from #temp where ID=#I
SET #I = #I + 1
SET #text='';
END
go
maybe someone or something with enough permissions made a mistake by reseeding?
As simple as this:
create table testid (
id int not null identity (1,1) primary key,
data varchar (3)
)
insert into testid (data) values ('abc'),('cde')
DBCC CHECKIDENT ('testid', RESEED, 1)
insert into testid (data) values ('bad')

SQL - How to roll up results into 1 row

If I have a table:
ID NAME
1 Red
2 Blue
3 Green
How can I return a query so that my result is:
Col1 Col2 Col3
Red Blue Green
Would I just do an inner join on itself or would I need a pivot table?
Yes, you can do it with join, eg:
select t1.name col1, t2.name col2, t3.name col3
from yourtable t1
join yourtable t2 on t2.id=2
join yourtable t3 on t3.id=3
where t1.id=1;
Or you can simply do it with embedded select statements, like:
In MySQL:
select
(select name from yourtable where id=1) col1,
(select name from yourtable where id=2) col2,
(select name from yourtable where id=3) col3;
In Oracle:
select
(select name from yourtable where id=1) col1,
(select name from yourtable where id=2) col2,
(select name from yourtable where id=3) col3
from dual;
Of course in that query the number of cols is fixed, you must edit it if you add more rows to roll up.
you can use dynamic SQL with PIVOT:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT ',' + QUOTENAME(id)
from yourtable
group by ColumnName, id
order by id
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = N'SELECT ' + #cols + N' from
(
select id, ColumnName
from yourtable
) x
pivot
(
max(ColumnName)
for id in (' + #cols + N')
) p '
exec sp_executesql #query;

PostgreSQL grouping

I would like to group values according to values in over columns.
This is an example:
I would like to get the output:
{{-30,-50,20},{-20,30,60},{-30,NULL or other value, 20}}
I managed to arrive to:
SELECT array_agg("val")
FROM my_table
WHERE "t_id" = 1
GROUP BY "m_id";
{{-30,-50,20},{-20,30,60},{-30,20}}
What would be the best approach?
create table my_table (
t_id int,
m_id int,
s_id int,
val int
);
insert into my_table (t_id, m_id, s_id, val) values
(1,1,1,-30),
(1,1,2,-50),
(1,1,3,20),
(1,2,1,-20),
(1,2,2,30),
(1,2,3,60),
(1,3,1,-30),
(1,3,3,20);
select array_agg(val order by s_id)
from
my_table t
right join
(
(
select distinct t_id, m_id
from my_table
) a
cross join
(
select distinct s_id
from my_table
) b
) s using (t_id, m_id, s_id)
where t_id = 1
group by m_id
order by m_id
;
array_agg
---------------
{-30,-50,20}
{-20,30,60}
{-30,NULL,20}

getting distinct rows based on two column values

I am trying to get distinct rows from a temporary table and output them to an aspx page. I am trying to use the value of one column and get the last entry made into that column.
I have been trying to use inner join and max(). However i have been unsuccessful.
Here is the code i have been trying to do it with.
Declare #TempTable table (
viewIcon nvarchar(10),
tenderType nvarchar(20),
diaryIcon int,
customerName nvarchar(100),
projectName nvarchar(100),
diaryEntry nvarchar(max),
diaryDate nvarchar(20),
pid nvarchar(20)
)
insert into #TempTable(
viewIcon,
tenderType,
diaryIcon,
customerName,
projectName,
diaryEntry ,
diaryDate ,
pid
)
select p.viewicon,
p.[Tender Type],
1 diaryicon,
c.[Customer Name],
co.[Last Project],
d.Action,
co.[Diary Date],
p.PID
From Projects2 p Inner Join
(select distinct Pno, max(convert(date,[date of next call],103)) maxdate from ProjectDiary group by Pno
) td on p.PID = td.Pno
Inner Join contacts3 co on co.[Customer Number] = p.[Customer Number]
Inner Join Customers3 c on p.[Customer Number] = c.[Customer Number]
Inner Join ProjectDiary d on td.Pno = d.Pno
Where CONVERT(Date, co.[Diary Date], 103) BETWEEN GETDATE()-120 AND GETDATE()-60
DECLARE #contactsTable TABLE
(pid nvarchar(200),
diaryDate date)
insert into #contactsTable (t.pid, t.diarydate)
select distinct pid as pid, MAX(CONVERT(DATE, diaryDate, 103)) as diaryDate from # TempTable t group by pid
DECLARE #tempContacts TABLE
(pid nvarchar(200))
insert into #tempContacts(pid)
select pid from #contactsTable
DECLARE #tempDiaryDate TABLE (diaryDate date)
insert into #tempDiaryDate(diaryDate)
select distinct MAX(CONVERT(DATE, diaryDate, 103)) from #TempTable
select t.* from #TempTable t inner join (select distinct customerName, M AX(CONVERT(DATE, diaryDate, 103)) AS diaryDate from #TempTable group by customerName) tt on t t.customerName=t.customerName
where t.pid not in
(select Pno from ProjectDiary where convert(date,[Date Of Next Call],103) > GETDATE())
and t.viewIcon <> '098'
and t.viewIcon <> '163'
and t.viewIcon <> '119'
and t.pid in (select distinct pid from #tempContacts)
and CONVERT(DATE, t.diaryDate, 103) in (select distinct CONVERT(DATE, diaryDate, 103) f rom #tempDiaryDate)
order by CONVERT(DATE, tt.diaryDate, 103)
I am trying to get all the distinct customerName's using the max date to determine which record it uses.
Use a subquery. Without going through your entire sql statement, the general idea is:
Select [Stuff]
From table t
Where date = (Select Max(Date) from table
where customer = t.customer)

t-sql WITH on WITH

I have to make query on WITH query, something like
; WITH #table1
(
SELECT id, x from ... WHERE....
UNION ALL
SELECT id, x from ... WHERE...
)
WITH #table2
(
SELECT DISTINCT tbl_x.*,ROW_NUMBER() OVER (order by id) as RowNumber
WHERE id in ( SELECT id from #table1)
)
SELECT * FROM #table2 WHERE RowNumber > ... and ...
So I have to use WITH on WITH and then SELECT on second WITH, How I can do that?
You can define multiple CTEs after the WITH keyword by separating each CTE with a comma.
WITH T1 AS
(
SELECT id, x from ... WHERE....
UNION ALL
SELECT id, x from ... WHERE...
)
, T2 AS
(
SELECT DISTINCT tbl_x.*, ROW_NUMBER() OVER (order by id) as RowNumber
WHERE id in ( SELECT id from T1 )
)
SELECT * FROM T2 WHERE RowNumber > ... and ...
https://web.archive.org/web/20210927200924/http://www.4guysfromrolla.com/webtech/071906-1.shtml