Search This Blog

2009-08-27

Find missing and duplicate number from a sequence of numbers in SQL table

Sometimes it is important to know which values in a sequence are missing, either to find unused values so they can be used, or to find “holes” in the data. In this article I’ll show you how to find missing values.

CREATE TABLE [dbo].[test](
[custid] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[custid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

//---------------------------------------
Insert the following values in the custid.

custid
-------
1
2
3
4
6
7
8
9
10
15
16
17
18
19
20

To Find missing no the SQL query should be:

select m.custid +1 as start
from test as m
left outer join test as r on m.custid = r.custid - 1
where r.custid is null

Output-

Start
---------
5
11
21

Description:first replicate the table as m and r,then its finding the null value from r(where r.custid is null) and joining with the previous value of null i.e r.custid-1 with the m table.
first it finds 4 as 5 is not in the r table and join 4 with the row of m where m.custid contains 4,similarly for 10 and 20.then it returns m.custid+1.

To find the range of missing values:
select start, stop from (
select m.custid + 1 as start,
(select min(custid) - 1 from test as x where x.custid > m.custid) as stop
from test as m
left outer join test as r on m.custid = r.custid - 1
where r.custid is null
) as x
where stop is not null;

Start Stop
------------
5 5
11 14

you can rewrite this query (without subqueries) as follows.

select l.custid + 1 as start, min(fr.custid) - 1 as stop
from test as l
left outer join test as r on l.custid = r.custid - 1
left outer join test as fr on l.custid <> m.custid) as stop
from test as m
left outer join test as r on ascii(m.custid) = ascii(r.custid) - 1
where r.custid is null
) as x
where stop <> '';

How to find duplicate record in sql server:
select id, count(*) from sequence
group by id
having count(*) > 1;


Reference:http://www.xaprb.com/blog/2005/12/06/find-missing-numbers-in-a-sequence-with-sql/

2009-08-14

GridView with Fixed Header and Freeze Column

Code Snippent:
protected void GridLoad()
{
/*
//For DB Connection and all other operation in Grid
SqlConnection sqlCon = new SqlConnection();
DPE dpe = new DPE();
sqlCon = dpe.DatabaseConnection();
SqlCommand cmd1 = new SqlCommand("MDOPEDS.OUTDATESRESULTS", sqlCon);
cmd1.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
adp.Fill(ds);
grvOutdatesData.DataSource = ds.Tables[0];
grvOutdatesData.DataBind();

Session["OutdateDataset"] = ds.Tables[0];
int colcount = ds.Tables[0].Columns.Count - 1;
int rowcount = ds.Tables[0].Rows.Count - 1;
hdn1.Value = rowcount.ToString();
for (int d = 0; d <= rowcount; d++)
{
TextBox txt = new TextBox();
txt.ID = "txtOutdates" + d;
txt.Text = grvOutdatesData.Rows[d].Cells[colcount].Text.Trim();
txt.Width = 40;
txt.Height = 12;
txt.Attributes.Add("runat='server' class", "inputTextBox1");
grvOutdatesData.Rows[d].Cells[colcount].Controls.Add(txt);
}
*/

//Freezing rows and column

for (int c = 1; c <= colcount; c++)
{
grvOutdatesData.HeaderRow.Cells[c].Attributes.CssStyle.Add("Width", "45px");
//gvQNDGrid.HeaderRow.Cells[c].Attributes.CssStyle.Add("writing-mode", "tb-rl");//for vertical align of header
//gvQNDGrid.HeaderRow.Cells[c].Attributes.CssStyle.Add("filter", "flipv fliph");
grvOutdatesData.HeaderRow.Cells[c].Attributes.CssStyle.Add("position", "relative");
grvOutdatesData.HeaderRow.Cells[c].Attributes.CssStyle.Add("TOP", "expression(this.offsetParent.scrollTop)");
}
grvOutdatesData.HeaderRow.Cells[0].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");
grvOutdatesData.HeaderRow.Cells[1].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");
grvOutdatesData.HeaderRow.Cells[2].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");
grvOutdatesData.HeaderRow.Cells[3].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");




foreach (GridViewRow grv in grvOutdatesData.Rows)
{
grv.Cells[0].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[0].Attributes.CssStyle.Add("position", "relative");
grv.Cells[0].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");
grv.Cells[1].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[1].Attributes.CssStyle.Add("position", "relative");
grv.Cells[1].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");
grv.Cells[2].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[2].Attributes.CssStyle.Add("position", "relative");
grv.Cells[2].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");
grv.Cells[3].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[3].Attributes.CssStyle.Add("position", "relative");
grv.Cells[3].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");


for (int b = 0; b < colcount; b++)
{
if (grv.Cells[b].Text == " ")
{
grv.Cells[b].Attributes.CssStyle.Add("BACKGROUND-COLOR", "#f19c76");
}
}


}
}
Output: