SQL Server : how to detect unregarded values from a range of integers - c#

I have a problem, in example: I have a range of ids of integers (from 1 to 1000), this range supposed to be ids in a SQL Server table, and I want to detect which numbers of this range are not in the table, and sorry for my bad english, thank you

another simpler option would be to use the following query
SELECT number
FROM master..spt_values
WHERE number BETWEEN 1 AND 1000
AND NOT EXISTS ( SELECT 1
FROM Your_Table t --<-- your table where you are checking
WHERE t.ID = number) -- the missing values
GROUP BY number
The above solution is only good if you are looking for around 1000 values. For more values you would need to modify it little bit, something like
-- Select the maximum number or IDs you want to check
DECLARE #Max_Num INT = 10000;
;WITH CTE AS
(
SELECT TOP (#Max_Num) ROW_NUMBER() OVER ( ORDER BY (SELECT NULL)) numbers
FROM master..spt_values v1 cross join master..spt_values v2
)
SELECT c.numbers
FROM CTE c
WHERE NOT EXISTS (SELECT 1
FROM Your_table t
WHERE t.ID = c.numbers)

One way to find "holes" is to generate a list of all possible values and then look for the ones that aren't there. If you can survive with a list of a missing value and then the number of subsequent values following it, you can do this with another method.
SQL Server 2012+ supports lead() and lag(). The following gets almost everything, except for initial missing values:
select t.id + 1 as missingid,
(coalesce(t.nextid, 1000) - t.id - 1) as nummissing
from (select t.*, lead(t.id) over (order by t.id) as nextid
from table t
t.id between 1 and 1000
) t
where t.nextid > t.id + 1 or
(t.nextid is null and t.id <> 1000)
You can get these with a little piece of special logic:
select (case when t.previd is null then 1
else t.id + 1
end) as missingid,
(case when t.previd is null then t.id - 1
else (coalesce(t.nextid, 1000) - t.id - 1)
end) as nummissing
from (select t.*, lead(t.id) over (order by t.id) as nextid,
lag(t.id) over (order by t.id) as previd
from table t
where t.id between 1 and 1000 and
) t
where (t.nextid > t.id + 1 or
(t.nextid is null and t.id <> 1000)
(t.previd is null and t.id <> 1)
)

This sounds like one of the many scenarios where it is helpful to have a numbers table:
SELECT *
FROM lkp_Numbers a
WHERE NOT EXISTS (SELECT 1
FROM YourTable b
WHERE a.num = b.Id)
AND num <= 1000
I use this to create a numbers table:
DROP TABLE lkp_Numbers
DECLARE #RunDate datetime
SET #RunDate=GETDATE()
SELECT TOP 1000 IDENTITY(int,1,1) AS Num
INTO lkp_Numbers
FROM sys.objects s1, sys.objects s2, sys.objects s3
ALTER TABLE lkp_Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Num)
That method for creating a numbers table was found here: What is the best way to create and populate a numbers table?

Related

DISTINCT query not working its give me repeated columns on output

Buddy
i have one query of MSSQL.
that is like this..
SELECT DISTINCT resource.locationurl,
resource.resourcename,
resource.anwserid,
checktotal.total
FROM resource
INNER JOIN (SELECT Count(DISTINCT anwserid) AS total,
resourcename
FROM resource AS Resource_1
WHERE ( anwserid IN (SELECT Cast(value AS INT) AS Expr1
FROM dbo.Udf_split(#sCategoryID, ',')
AS
udf_Split_1) )
GROUP BY resourcename) AS checktotal
ON resource.resourcename = checktotal.resourcename
WHERE ( resource.anwserid IN (SELECT Cast(value AS INT) AS Expr1
FROM dbo.Udf_split(#sCategoryID, ',') AS
udf_Split_1)
)
AND ( checktotal.total = #Total )
ORDER BY resource.resourcename
I run this query but its give me repeated column of Resource.LocationURL.
you can check it live hear http://www.ite.org/visionzero/toolbox/default2.aspx
check in above link where you can fire select some category but result was not distinct..
i try most of my but now i am out of mind please help me with this.
You misunderstand what DISTINCT means when you are fetching more than one column.
If you run this query:
SELECT DISTINCT col1, col2 FROM table
You are selected every different combination. An acceptable result would be
value 1_1, value 2_1
value 1_1, value 2_2,
value 2_1, value_2_1
In this example, value 1_1 appears twice, but the two columns combined are unique.
My guess is that you are actually attempting to perform a grouping:
SELECT resource.locationurl,
resource.resourcename,
resource.anwserid,
Sum(checktotal.total)
FROM resource
INNER JOIN (SELECT Count(DISTINCT anwserid) AS total,
resourcename
FROM resource AS Resource_1
WHERE ( anwserid IN (SELECT Cast(value AS INT) AS Expr1
FROM dbo.Udf_split(#sCategoryID, ',')
AS
udf_Split_1) )
GROUP BY resourcename) AS checktotal
ON resource.resourcename = checktotal.resourcename
WHERE ( resource.anwserid IN (SELECT Cast(value AS INT) AS Expr1
FROM dbo.Udf_split(#sCategoryID, ',') AS
udf_Split_1)
)
AND ( checktotal.total = #Total )
GROUP BY resource.locationurl,
resource.resourcename,
resource.anwserid
First of all, the site you linked doesn't do anything.
Second, DISTINCTensures unique rows. It will not make the values in all the columns unique as well. Just think about it! How would it work? You have two rows with the same locationurl field, but with otherwise distinct elements. Which one do you not include?
Lastly, please take greater care in phrasing your questions.
as I see your query is select DISTINCT on multi columns,
so if any record has at least one col difference then it pass the DISTINCT condition
Ex:
record1 : locationurl | resourcename | anwserid | Sum(checktotal.total)
loc1 res1 1 100
record2 : locationurl | resourcename | anwserid | Sum(checktotal.total)
loc1 res1 2 100

Fetch alternate series of records in SQL Server

I want to get alternate series of records using SQL Server.
For example :
I want to skip first 10 records (1 to 10) in sequence and get other 10 records (11 to 20) after that I want to skip next 10 records (21 to 30) and get another next 10 records (31 to 40)
I have done for alternate rows as below...
SELECT ROW, EmployeeID
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY EmployeeID) AS ROW, *
FROM Employee) A
WHERE
ROW % 2 = 0
But in case of my requirement above logic will not work. Please help me to make above thing works..
Linq will also accepted
Thanks
SELECT ROW, EmployeeID
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY EmployeeID) AS ROW, *
FROM Employee) A
WHERE
((ROW - 1)/10) % 2 = 1
I have done above of your requirement like below (Example).
Generated 1200 number sequentially from 1 to 1200 like below
;WITH CTE
AS
(
SELECT 1 PERIOD_SID
UNION ALL
SELECT PERIOD_SID+1 FROM CTE WHERE PERIOD_SID<1200
)
SELECT * INTO #PERIOD1 FROM CTE
OPTION(MAXRECURSION 0)
After this i have find alternative numbers like you have mentioned in your query
SELECT PERIOD_SID FROM
(
SELECT CASE
WHEN PERIOD_SID%10 <> 0 THEN PERIOD_SID / 10
WHEN PERIOD_SID%10 = 0 THEN ( PERIOD_SID / 10 ) - 1
END RNO,
PERIOD_SID
FROM #PERIOD1 )A
WHERE RNO%2=0
so if you have sequential numbers in your query (If you dont have generate using rownumber) then apply above logic.
Your query need to convert like below.
SELECT EMPLOYEEID
FROM (SELECT Row_number()
OVER (
ORDER BY EMPLOYEEID)AS ROW,
*
FROM EMPLOYEE) A
WHERE ( CASE
WHEN RNO%10 <> 0 THEN RNO / 10
WHEN RNO%10 = 0 THEN ( RNO / 10 ) - 1
END )%2 = 0
may be you can try this
SELECT ROW, EmployeeID FROM(
SELECT ROW_NUMBER()OVER (ORDER BY EmployeeID)AS ROW,* FROM Employee)
A WHERE ((ROW - (ROW%10))/10) % 2 = 1
Don't if this works or not coz I dont have sql server to run. Please show the error/result after running this query.
Ask if any doubt.

SQL Server - DATEDIFF Function taking too long

I've done some extensive research and I've concluded that the DATEDIFF function is making my queries run very slow.
Below is the generated query by Entity Framework and it does look readable enough hopefully.
Here's the Linq that generates the T-SQL:
model.NewTotal1Week = ( from sdo in context.SubscriberDebitOrders
where
(
sdo.CampaignId == campaignId &&
( sdo.Status == ( Int32 ) DebitOrderStatus.New_Faulty ) &&
( SqlFunctions.DateDiff( "week", sdo.Collections.FirstOrDefault( c => c.TxnStatus == "U" ).ProcessDate, DateTime.Now ) <= 1 )
)
select sdo ).Count();
In the query below, I would like to get a COUNT of all Collections which fall within 1 week from the time they were Processed to today's date.
Is there anyone that can help me get rid of the DATEDIFF function? I've seen examples online but I couldn't adapt it to my scenario, forgive me I'm not very genius yet.
exec sp_executesql N'SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[SubscriberDebitOrder] AS [Extent1]
OUTER APPLY (SELECT TOP (1)
[Extent2].[ProcessDate] AS [ProcessDate]
FROM [dbo].[Collections] AS [Extent2]
WHERE ([Extent1].[Id] = [Extent2].[DebitOrderId]) AND (''U'' = [Extent2].[TxnStatus]) ) AS [Limit1]
WHERE ([Extent1].[CampaignId] = #p__linq__0) AND (3 = [Extent1].[Status]) AND ((DATEDIFF(week, [Limit1].[ProcessDate], SysDateTime())) <= 1)
) AS [GroupBy1]',N'#p__linq__0 int',#p__linq__0=3
go
Thanks in advance.
Its not the just DATEDIFF, any function on the column would cause query to do a SCAN on the underlying table/index
DATEDIFF(week, [Limit1].[ProcessDate], SysDateTime())) <=1
Above logic is fetching last week data? You can also write above without putting function around ProcessDate Column.
[Limit1].[ProcessDate] > SysDateTime()-7
This is your query:
SELECT GroupBy1.A1 AS C1
FROM (SELECT COUNT(1) AS[A1
FROM dbo.SubscriberDebitOrder AS Extent1 OUTER APPLY
(SELECT TOP (1) Extent2.ProcessDate
FROM [dbo].Collections Extent2
WHERE (Extent1.Id = Extent2.DebitOrderId AND
'U' = Extent2.TxnStatus
) AS [Limit1]
WHERE (Extent1.CampaignId = #p__linq__0) AND (3 = Extent1.Status) AND
(DATEDIFF(week, Limit1.ProcessDate, SysDateTime()) <= 1)
) GroupBy1;
As mentioned elsewhere, you should change the date logic and get rid of the outer query:
SELECT COUNT(1) AS A1
FROM dbo.SubscriberDebitOrder AS Extent1 OUTER APPLY
(SELECT TOP (1) Extent2.ProcessDate
FROM [dbo].Collections Extent2
WHERE (Extent1.Id = Extent2.DebitOrderId AND
'U' = Extent2.TxnStatus
) AS limit1
WHERE (Extent1.CampaignId = #p__linq__0) AND (3 = Extent1.Status) AND
Limit1.ProcessDate <= DATEADD(-1, week, GETDATE())
Very important note: This is not exactly equivalent to your query. Your original query counted the number of week boundaries between two dates. This depends on datefirst, but it woudld often be the number of Saturday or Sunday nights.
Based on your description, the above is more correct.
Next, you want indexes on Collections(DebitOrderId, TxnStatus, ProcessDate) and SubscriberDebitOrder(CampaignId, Status).

Efficient way to query each element of a list

I have to iterate through a collection of objects (let's say ID's), and execute a specific query for each of these objects. For example:
IEnumerable<int> ids = getIDs(); //[1,2,3,4...]
Right now I have this solution:
DBEntities db = new DBEntities();
var results =
from a in db.TABLEA
join b in db.TABLEB on a.id equals b.id
join c in db.TABLEC on b.oid equals c.oid
where ids.Contains(c.id)
select a;
but keep in mind that the list of IDs is smaller than the table where I am searching. That being said, the solution above seems inefficient, since I am looking for each record of my table against a smaller list, when I wanted the opposite. I also do not want to iterate through the list, and execute the query for one element at a time.
Ideally, I would want something like this:
DBEntities db = new DBEntities();
(some data structure) ids = getIDs();
var results =
from a in db.TABLEA
join b in db.TABLEB on a.id equals b.id
join c in db.TABLEC on b.oid equals c.oid
join i in ids on c.id equals i.id;
The (pseudo-)code above would iterate my elements of the list, in a single query, doing so in a single query and performing my filter by each element of the list.
Is this the way to do it? If so, what is the appropriate data structure to implement this solution? If not, which alternatives do I have?
Magnus's answer is true but not right :)
Technically you do have two options in newer versions of Entity Framework (and I discovered this by chance). Contains of course, but also Join.
Joining with a local sequence of primitive types has always been possible, but very quickly (after some tens of elements) raised a SqlException:
Some part of your SQL statement is nested too deeply. Rewrite the query or break it up into smaller queries.
EF tries to translate the local list to a temporary table in SQL. This is surprisingly non-trivial. It has to build the table by UNION-ing select statements that return 1 element each. This is what it used to look like with only 5 elements!
....
INNER JOIN (SELECT
[UnionAll3].[C1] AS [C1]
FROM (SELECT
[UnionAll2].[C1] AS [C1]
FROM (SELECT
[UnionAll1].[C1] AS [C1]
FROM (SELECT
1 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
UNION ALL
SELECT
2 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable2]) AS [UnionAll1]
UNION ALL
SELECT
3 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable3]) AS [UnionAll2]
UNION ALL
SELECT
4 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable4]) AS [UnionAll3]
UNION ALL
SELECT
5 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable5]) AS [UnionAll4] ON ....
As you see, if you look closely, the UNION statements are nested. The nesting level soon becomes too deep, which made this approach practically useless.
However, currently the SQL looks like this:
....
INNER JOIN (SELECT
1 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
UNION ALL
SELECT
2 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable2]
UNION ALL
SELECT
3 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable3]
UNION ALL
SELECT
4 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable4]
UNION ALL
SELECT
5 AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable5]) AS [UnionAll4] ON ....
Still not a beauty, but the nesting is replaced by chaining and the list can contain hundreds of elements.
But... (and this is why Magnus's answer is true), it doesn't perform well. A simple test with 2000 elements in the list took 2.5s with join and .25s with Contains. So there's still no practical case for joining with a local sequence.
If this is linq2Sql (or Linq2Entites) your only option is as in your example 1. You can not "join" a table with an in memory list. You have to use Contains. Which will be translated to an Where c.id IN(2,3,4,5,...) SQL query

Datatype for System.Version in sql server

What is the best way to store System.Version in SQL Server?
When I use varchar type, result of order by asc is:
1.0.0.0
11.0.0.0
12.0.0.0
2.0.0.0
you can use a varchar column
you could order like this
SELECT *
FROM t_version
ORDER BY CAST('/' + vid + '/' AS HIERARCHYID)
SQL fiddle is not working today , other wise I could have showed a demo
Please run this for testing
SELECT * FROM
( VALUES
( '1.0.0.0' ),
( '11.0.0.0' ),
('12.0.0.0'),
('2.0.0.0') ) AS vid ( vid )
ORDER BY CAST('/' + vid + '/' AS HIERARCHYID)
Just store it as a normal varchar, which is good for versions up to 4 parts using PARSENAME to split the string and order by 4 separate columns.
i.e.
ORDER BY PARSENAME(version,4),
PARSENAME(version,3),
PARSENAME(version,2),
PARSENAME(version,1)
To support ordering among mixed lengths versions (e.g. '1.2' vs '1.2.3.4'), a mapping to a decimal can be performed (as inline table valued functions).
create function Common.ufn_OrderableVersion(#pVersion nvarchar(100))
returns table
as
/*---------------------------------------------------------------------------------------------------------------------
Purpose: Provide a mapping from Versions of the form 'a.b.c.d', 'a.b.c, 'a.b', 'a', null to
an orderable decimal(25, 0)
Since Parsename() doesn't apply easily to mixed length comparisions (1.2 vs 1.2.3.4)
Test Cases:
select * from Common.ufn_OrderableVersion(null); -- null
select * from Common.ufn_OrderableVersion('0'); -- 1000000000000000000000000
select * from Common.ufn_OrderableVersion('1'); -- 1000001000000000000000000
select * from Common.ufn_OrderableVersion('1.2.3.4'); -- 1000001000002000003000004
select Version
from
(
select '1.3.5.3' as Version
union all
select '1.2.5.3' as Version
union all
select '1.1.5.3' as Version
union all
select '1.3.5.2' as Version
union all
select null as Version
union all
select '' as Version
union all
select '2' as Version
union all
select '1.2' as Version
union all
select '1' as Version
) v
order by (select Value from Common.ufn_OrderableVersion(Version))
Modified By Description
---------- -------------- ---------------------------------------------------------------------------------------
2015.08.24 crokusek Initial Version
---------------------------------------------------------------------------------------------------------------------*/
return
-- 25 = 1 + VersionPositions * MaxDigitsPerSegment
select convert(decimal(25,0), '1' +
stuff((select format(Value, '000000')
from
(
select convert(int, Value) as Value, RowNumber
-- Support empty string and partial versions. Null maps to null
from Common.ufn_SplitUsingXml(#pVersion + '.0.0.0.0', '.') -- pad right
where RowNumber <= 4 -- trim right
) as v
order by RowNumber
for xml path ('')
), 1, 0, '')
) as Value
go
Dependency:
create function Common.ufn_SplitUsingXml
(
#pList nvarchar(max),
#pDelimiter nvarchar(255)
)
returns table
as
/*---------------------------------------------------------------------------------------------------------------------
Purpose: Split an Identifier using XML as an inline table valued function.
Using the SQL Server CLR (C#) capability would be the most efficient way to support this.
Warnings: Will not work if the input contains special XML characters like '<', '>' or '&'.
Caller must add "option (maxrecursion 0)" for lists greater than 100 (it can't be added within the ufn)
Modified By Description
---------- -------------- ---------------------------------------------------------------------------------------
2015.08.24 inet http://sqlperformance.com/2012/07/t-sql-queries/split-strings
---------------------------------------------------------------------------------------------------------------------*/
return
(
select Value = y.i.value('(./text())[1]', 'nvarchar(4000)'),
row_number() over (order by (select null)) as RowNumber
from
(
select x = convert(XML, '<i>'
+ replace(#pList, #pDelimiter, '</i><i>')
+ '</i>').query('.')
) AS a cross apply x.nodes('i') AS y(i)
-- option (maxrecursion 0) must be added by caller for lists greater than 100
);
go
Comparison:
alter function Common.ufn_CompareVersions
(
#pVersionA nvarchar(100),
#pVersionB nvarchar(100)
)
returns table
as
/*---------------------------------------------------------------------------------------------------------------------
Purpose: Compare Version of the form 'A.B.C.D'.
Comparing versions of different lengths is also supported 'A.B'.
Test Cases:
select Result from Common.ufn_CompareVersions('1', null) -- 1
select Result from Common.ufn_CompareVersions(null, '1') -- -1
select Result from Common.ufn_CompareVersions('1', '1') -- 0
select Result from Common.ufn_CompareVersions('1', '2') -- -1
select Result from Common.ufn_CompareVersions('2', '1') -- 1
select Result from Common.ufn_CompareVersions('1', '1.2') -- -1
select Result from Common.ufn_CompareVersions('1.2', '1') -- 1
select Result from Common.ufn_CompareVersions('1.2.3.4', '1.2.3.4') -- 0
select Result from Common.ufn_CompareVersions('1.2.3', '1.2.3.4') -- -1
select Result from Common.ufn_CompareVersions('1.2.3.4', '1.2.3') -- 1
select Result from Common.ufn_CompareVersions('1.9.3.4', '1.2.3.4') -- 1
select Result from Common.ufn_CompareVersions('1.2.3.4', '1.9.3.4') -- -1
select Result from Common.ufn_CompareVersions('1.002', '1.2') -- 0
select Result from Common.ufn_CompareVersions('1.2', '1.2.0') -- 0
Modified By Description
---------- ----------- ------------------------------------------------------------------------------------------
2015.08.24 crokusek Initial Version
---------------------------------------------------------------------------------------------------------------------*/
return
with Compares as
(
select (select IsNull(Value, 0) from Common.ufn_OrderableVersion(#pVersionA)) as A,
(select IsNull(Value, 0) from Common.ufn_OrderableVersion(#pVersionB)) as B
)
select case when A > B then 1
when A < B then -1
else 0
end as Result
from Compares
go

Categories