Find non existing time intervall when doing join to another table - sql

I have a question that I dont know If there is solution to in sql. Made an small example
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Events](
[Id] [int] IDENTITY(1,1) NOT NULL,
[EventTime] [datetime] NOT NULL,
[Event] [nvarchar](50) NOT NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Logging] Script Date: 2020-07-07 14:35:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Logging](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FromTime] [datetime] NOT NULL,
[ToTime] [datetime] NOT NULL,
[Status] [nvarchar](50) NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Events] ON
GO
INSERT [dbo].[Events] ([Id], [EventTime], [Event]) VALUES (1, CAST(N'2020-07-07T14:14:00.000' AS DateTime), N'str')
GO
INSERT [dbo].[Events] ([Id], [EventTime], [Event]) VALUES (2, CAST(N'2020-07-07T07:01:00.000' AS DateTime), N'testcall')
GO
INSERT [dbo].[Events] ([Id], [EventTime], [Event]) VALUES (3, CAST(N'2020-07-07T15:22:00.000' AS DateTime), N'ipfail')
GO
SET IDENTITY_INSERT [dbo].[Events] OFF
GO
SET IDENTITY_INSERT [dbo].[Logging] ON
GO
INSERT [dbo].[Logging] ([Id], [FromTime], [ToTime], [Status]) VALUES (1, CAST(N'2020-07-07T01:00:00.000' AS DateTime), CAST(N'2020-07-07T13:00:00.000' AS DateTime), N'All well')
GO
INSERT [dbo].[Logging] ([Id], [FromTime], [ToTime], [Status]) VALUES (2, CAST(N'2020-07-07T13:01:00.000' AS DateTime), CAST(N'2020-07-07T15:00:00.000' AS DateTime), N'All well')
GO
INSERT [dbo].[Logging] ([Id], [FromTime], [ToTime], [Status]) VALUES (3, CAST(N'2020-07-07T15:33:00.000' AS DateTime), CAST(N'2020-07-07T20:00:00.000' AS DateTime), N'All well')
GO
INSERT [dbo].[Logging] ([Id], [FromTime], [ToTime], [Status]) VALUES (4, CAST(N'2020-07-07T20:01:00.000' AS DateTime), CAST(N'2020-07-07T23:00:00.000' AS DateTime), N'All well')
GO
SET IDENTITY_INSERT [dbo].[Logging] OFF
GO
When doing an inner join of this table I want to find the Event in Event table that doesn't have a matching timeframe in Logging. The timeframe that's missing is 15:00 to 15:33 and the event that happens that happens is the Event with ID 3. Everything is measured in minutes. Is this possible? Or have to do it some other way?

If I understand correctly, you want not exists:
select l.*
from logging l
where not exists (select 1
from events e
where e.eventtime >= l.fromtime and
e.eventtime <= l.totime
);
However, this returns two timeframes -- ones with ids 3 and 4.
Here is a db<>fiddle.

Related

SQL Counting non numeric values from a column

I have a table named tasks inside is
id
tasks
tasks_status
task_date
The tasks statuses are "done","pending","forwarded","cancelled".
How can I query the table of counting all the done, all the pending, all the forwarded and all cancelled.
I tried doing all the tweaks from COUNT but to no avail. It's been 2 hours of looking for a way. Please help
My goal is to specifically count only those DONE, I can get the count of all the specifics but just would need to show the total count of done (for example)
Group by is better but if you just want the ones specified as 'Done' the below should work.
SELECT COUNT(*) FROM TASTS WHERE TASKS_STATUS='done'
Select what you want with count and Group Them.
SELECTS tasks_status, count(tasks_status) FROM status GROUP BY tasks_status;
select tasks_status, count(*) from tasks group by tasks_status
http://sqlfiddle.com/#!17/1e27e/4/0
If you want to get all counts then you need to use group by but if you want to get count of just one item then group by is not required.
Following is query to generate scenario
CREATE TABLE [dbo].[tasks](
[id] [int] IDENTITY(1,1) NOT NULL,
[tasks] [varchar](50) NULL,
[tasks_status] [varchar](50) NULL,
[task_date] [date] NULL,
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[tasks] ON
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (1, N'task1', N'pending', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (2, N'task2', N'forwarded', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (3, N'task3', N'pending', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (4, N'task4', N'done', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (5, N'task5', N'done', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (6, N'task6', N'cancelled', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (7, N'task7', N'done', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (8, N'task8', N'forwarded', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (9, N'task9', N'pending', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (10, N'task10', N'done', CAST(N'2022-09-30' AS Date))
GO
SET IDENTITY_INSERT [dbo].[tasks] OFF
GO
You can use group by if you want to get count of all unique tasks_status or some of tasks_status using following query
For All
SELECT tasks_status, count(tasks_status) FROM dbo.tasks GROUP BY tasks_status;
For done and pending
SELECT tasks_status, count(tasks_status) FROM dbo.tasks
WHERE tasks_status IN ('done','pending')
GROUP BY tasks_status
if you want to get count for done only then you can use either of the following. Use group by if you want to use other columns in select, else if you want to get just count and no other column then just 2nd query without group by
SELECT tasks_status, count(tasks_status) FROM dbo.tasks
WHERE tasks_status = 'done'
GROUP BY tasks_status
or
SELECT COUNT(tasks_status) FROM dbo.tasks WHERE tasks_status='done'

Generate missing rows using JOIN / CROSS JOIN

I have 3 tables which are used to fill dynamic from value.
The table is represented by schema below.
User can add as many rows as possible, and the rows are shown to end user by template from FormField table.
The data that is saved in FormValues are only for not null values and any values that missing are not saved.
Now the problem is I have to generate report as expected below.
I have tried various combination of join / cross join, but none of them works as expected.
I am able to achieve this in C# via loops, but not able to do the same via SQL Server.
Script attached for DB
USE [SampleDb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[FormTemplate](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_FormTemplate] PRIMARY KEY CLUSTERED
(
[Id] ASC
)
) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[FormField](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FormId] [int] NOT NULL,
[FieldName] [nvarchar](50) NULL,
[FieldType] [nvarchar](50) NULL,
CONSTRAINT [PK_FormField] PRIMARY KEY CLUSTERED
(
[Id] ASC
)
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[FormTemplate] Script Date: 3/2/2020 10:10:22 PM ******/
/****** Object: Table [dbo].[FormValue] Script Date: 3/2/2020 10:10:22 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[FormValue](
[FormId] [int] NOT NULL,
[FieldId] [int] NOT NULL,
[RowIndex] [int] NOT NULL,
[FormValue] [nvarchar](50) NULL,
CONSTRAINT [PK_FormValue] PRIMARY KEY CLUSTERED
(
[FormId] ASC,
[FieldId] ASC,
[RowIndex] ASC
)
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[FormField] ON
GO
INSERT [dbo].[FormField] ([Id], [FormId], [FieldName], [FieldType]) VALUES (1, 1, N'FirstName', N'string')
GO
INSERT [dbo].[FormField] ([Id], [FormId], [FieldName], [FieldType]) VALUES (2, 1, N'LastName', N'string')
GO
INSERT [dbo].[FormField] ([Id], [FormId], [FieldName], [FieldType]) VALUES (3, 1, N'Place', N'string')
GO
INSERT [dbo].[FormField] ([Id], [FormId], [FieldName], [FieldType]) VALUES (4, 1, N'age', N'int')
GO
INSERT [dbo].[FormField] ([Id], [FormId], [FieldName], [FieldType]) VALUES (5, 1, N'dob', N'date')
GO
SET IDENTITY_INSERT [dbo].[FormField] OFF
GO
SET IDENTITY_INSERT [dbo].[FormTemplate] ON
GO
INSERT [dbo].[FormTemplate] ([Id], [Name]) VALUES (1, N'Sample')
GO
SET IDENTITY_INSERT [dbo].[FormTemplate] OFF
GO
INSERT [dbo].[FormValue] ([FormId], [FieldId], [RowIndex], [FormValue]) VALUES (1, 1, 1, N'fname1')
GO
INSERT [dbo].[FormValue] ([FormId], [FieldId], [RowIndex], [FormValue]) VALUES (1, 2, 1, N'lname1')
GO
INSERT [dbo].[FormValue] ([FormId], [FieldId], [RowIndex], [FormValue]) VALUES (1, 2, 3, N'lname3')
GO
INSERT [dbo].[FormValue] ([FormId], [FieldId], [RowIndex], [FormValue]) VALUES (1, 4, 5, N'20')
GO
INSERT [dbo].[FormValue] ([FormId], [FieldId], [RowIndex], [FormValue]) VALUES (1, 5, 3, N'10/10/2020')
GO
This answers the original version of the question -- and then some. But I don't update answers to conform to evolving questions.
You seem to want cross joins on three "tables". The third needs to generate the rowindex values:
select f.id as formtemplateid, ff.id as formfieldid, v.rowindex,
fv.value
from formtemplate f cross join
formfield ff cross join
(values (1), (2), (3), (4), (5)) v(rowindex) left join
formvalues fv
on fv.formtemplateid = f.id and
fv.formfieldid = ff.id and
fv.rowindex = v.rowindex
order by f.id, ff.id, v.rowindex;
EDIT:
You can generate up to 100 numbers using a recursive CTE by doing:
with n as (
select 1 as n
union all
select n + 1
from n
where n < 10 -- "10" is however many you want
)
select n.n
from n;

Excluding rows from second table but including in comparison

I have two tables with the same data in it I need to compare one column here is the ddl for testing
USE [DifferencesDB]
GO
/****** Object: Table [dbo].[FirstTable] Script Date: 11/06/2019 15:27:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[FirstTable](
[Id] [int] NULL,
[Column1] [nchar](10) NULL,
[Column2] [nchar](10) NULL,
[Column4] [decimal](18, 0) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[FirstTable] ([Id], [Column1], [Column2], [Column4]) VALUES (1, N'test ', N'test ', 30)
GO
INSERT [dbo].[FirstTable] ([Id], [Column1], [Column2], [Column4]) VALUES (2, N'test2 ', N'test3 ', 18)
GO
CREATE TABLE [dbo].[SecondTable](
[Id] [int] NULL,
[Column1] [nchar](10) NULL,
[Column2] [nchar](10) NULL,
[Column4] [decimal](18, 5) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[SecondTable] ([Id], [Column1], [Column2], [Column4]) VALUES (1, N'test ', N'test ', 13.56895)
GO
INSERT [dbo].[SecondTable] ([Id], [Column1], [Column2], [Column4]) VALUES (2, N'test2 ', N'test3 ', 18.456 )
GO
As you can see here what I want to do is compare to the two tables and the Data in Column 4 which is a decimal.
/****** Script for SelectTopNRows command from SSMS ******/
SELECT ABS(T0.Column4 -T1.Column4) as 'Difference'
FROM [FirstTable] T0, [SecondTable] T1
where T0.Id =T1.ID
This works but it gives me back tones of extra rows this gets me the difference I require so how do I exclude the rows from the other table.
So, for example, it should say the difference is I would like the data to be returned from the second table but also the difference column as well.
.456
Edit
TO show what is happening on live data.
Please see my new query here
SELECT GoodData_Lines.Qty,
Invalid_Lines.Qty,
abs(GoodData_Lines.Qty- Invalid_Lines.Qty) AS 'Difference'
FROM GoodData_Lines
left OUTER JOIN Invalid_MCSSOPLines
ON GoodData_Lines.LineID = Invalid_Lines.LineID
Not exactly sure what the question was, but based on how I understand it, will this do?
SELECT T1.*, ABS(T0.Column4 -T1.Column4) as 'Difference'
FROM [FirstTable] T0 right join [SecondTable] T1
on T0.Id =T1.ID
This can be a solution?
[EDIT] following your update
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[GoodData_Lines](
[LineID] [int] NULL,
[Column1] [nchar](10) NULL,
[Column2] [nchar](10) NULL,
[Qty] [decimal](18, 0) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[GoodData_Lines] ([LineID], [Column1], [Column2], [Qty]) VALUES (1, N'test ', N'test ', 30)
GO
INSERT [dbo].[GoodData_Lines] ([LineID], [Column1], [Column2], [Qty]) VALUES (2, N'test2 ', N'test3 ', 18)
GO
CREATE TABLE [dbo].[Invalid_MCSSOPLines](
[LineID] [int] NULL,
[Column1] [nchar](10) NULL,
[Column2] [nchar](10) NULL,
[Qty] [decimal](18, 5) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Invalid_MCSSOPLines] ([LineID], [Column1], [Column2], [Qty]) VALUES (1, N'test ', N'test ', 13.56895)
GO
INSERT [dbo].[Invalid_MCSSOPLines] ([LineID], [Column1], [Column2], [Qty]) VALUES (2, N'test2 ', N'test3 ', 18.456 )
GO
SELECT GoodData_Lines.Qty,
isnull( [Invalid_MCSSOPLines].Qty,0) invalid_qty,
abs(GoodData_Lines.Qty - isnull([Invalid_MCSSOPLines].Qty,0)) AS 'Difference'
FROM GoodData_Lines
left OUTER JOIN Invalid_MCSSOPLines
ON GoodData_Lines.LineID = [Invalid_MCSSOPLines].LineID
-- eventually drop table if no more needed
-- DROP TABLE [GoodData_Lines]
-- DROP TABLE [Invalid_MCSSOPLines]

Grouping with partition and over in TSql

I have a simple table
CREATE TABLE [dbo].[Tooling](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Status] [int] NOT NULL,
[DateFinished] [datetime] NULL,
[Tooling] [nvarchar](50) NULL,
[Updated] [datetime] NULL,
) ON [PRIMARY]
with following values
SET IDENTITY_INSERT [dbo].[Tooling] ON
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (1, N'Large', 0, NULL, NULL, CAST(N'2015-05-05 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (2, N'Large', 1, NULL, N'1', CAST(N'2015-05-10 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (3, N'Small', 0, NULL, N'2', CAST(N'2015-05-11 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (4, N'Large', 2, NULL, N'1', CAST(N'2015-05-12 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (5, N'Large', 2, NULL, N'2', CAST(N'2015-05-12 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (6, N'Large', 1, NULL, N'1', CAST(N'2015-05-13 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (7, N'Large', 1, NULL, N'2', CAST(N'2015-05-14 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (8, N'Small', 1, CAST(N'2015-05-15 00:00:00.000' AS DateTime), N'2', CAST(N'2015-05-15 00:00:00.000' AS DateTime))
GO
SET IDENTITY_INSERT [dbo].[Tooling] OFF
I want to create a view of the table that looks like its an entirely own table.
SELECT Id, Name, Status, DateFinished
FROM Tooling t order by t.id
If i run that I would like that the records with id 5 and 7 should be excluded since they dont change in the selected set from the previous row.
I had an idea to solve this by using, ROW_NUMBER() over partition
And by using group by but thats seems to incorrect(Could not to get it to work at all)
How do I group it when the should be grouped on that the value change and not the value it contains?
Any suggestions to solve this?
My end goal is also try to convert this to linq to entities...
OK, supposing it's ordered by id
select *
from (
select *, rng = row_number() over (partition by grp order by id)
from (
select *, grp = row_number() over (order by id) - row_number() over (partition by Name, Status, DateFinished order by id)
from tooling ) g
) gn
where rng = 1
order by id

Difference between TOP X and Row_Number()

Recently I've faced weird issue. I have two simple queries where one of them uses TOP X and other one does the same by using ROW_NUMBER and then selects items with rowNumber between 1 and X, and both of them ordered by same column, but the result is completely different.
For example, let's say we have a simple DB as below with some dummy data:
CREATE TABLE [dbo].[Test](
[Id] [int] IDENTITY(1,1) NOT NULL,
[NDate] [datetime] NOT NULL,
CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
SET IDENTITY_INSERT [dbo].[Test] ON
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (1, '2011-08-24 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (2, '2011-08-24 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (3, '2011-08-24 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (4, '2011-08-24 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (5, '2011-08-21 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (6, '2011-08-21 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (7, '2011-08-21 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (8, '2011-08-21 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (9, '2011-08-21 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (10, '2011-08-21 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (11, '2011-08-21 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (12, '2011-08-21 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (13, '2011-08-21 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (14, '2011-08-21 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (15, '2011-08-21 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (16, '2011-08-21 00:00:00.000')
SET IDENTITY_INSERT [dbo].[Test] OFF
Now if we perform below queries, we will get different results. If we use TOP 10, we will get below result:
SELECT TOP 10 [Id],[NDate] FROM [Test] order by NDate desc
RESULT=>
Id NDate
4 2011-08-24 00:00:00.000
3 2011-08-24 00:00:00.000
2 2011-08-24 00:00:00.000
1 2011-08-24 00:00:00.000
11 2011-08-21 00:00:00.000
10 2011-08-21 00:00:00.000
9 2011-08-21 00:00:00.000
8 2011-08-21 00:00:00.000
7 2011-08-21 00:00:00.000
6 2011-08-21 00:00:00.000
select id,NDate from (
select ROW_NUMBER() over (order by NDate DESC) as RNumber, Id,NDate
from Test) as t
where RNumber between 1 and 10
RESULT=>
id NDate
1 2011-08-24 00:00:00.000
2 2011-08-24 00:00:00.000
3 2011-08-24 00:00:00.000
4 2011-08-24 00:00:00.000
5 2011-08-21 00:00:00.000
6 2011-08-21 00:00:00.000
7 2011-08-21 00:00:00.000
8 2011-08-21 00:00:00.000
9 2011-08-21 00:00:00.000
10 2011-08-21 00:00:00.000
The issue is, When you are using LINQ to SQL, and you want to do pagination, generated query for selecting first page will be TOP X, while for the other pages will be using ROW_NUMBER, and the result is, some items will never appear in the listing.
You need to implement a secondary sorting.
Example:
select id,NDate from (
select ROW_NUMBER() over (order by NDate DESC, id) as RNumber, Id,NDate -- Note: NDate DESC, id
from Test) as t
where RNumber between 1 and 10
Example:
SELECT TOP 10 [Id],[NDate] FROM [Test] order by NDate desc, ID -- Note: NDate DESC, id
Otherwise, you might as well expect random records per unique NDate.
If you want the same records for each query, you have to specify that secondary sort column.
I agree with #hamlin11, but putting it more simply :-)
In the first example you are sorting by
order by NDate desc
/* to get same results as example 2, change this to
order by NDate desc, id
*/
and in the second by
order by NDate DESC, id
In the second example you are sorting by Id, that is why the ID column is in order, you haven't done this in the first example.
It might be better to use data like this, so you can see more clearly what is happening:
/****** Object: Table [dbo].[Test] Script Date: 08/27/2011 07:56:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Test](
[Id] [int] IDENTITY(1,1) NOT NULL,
[NDate] [datetime] NOT NULL,
CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Test] ON
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (1, '2011-08-10 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (2, '2011-08-11 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (3, '2011-08-12 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (4, '2011-08-13 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (5, '2011-08-14 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (6, '2011-08-15 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (7, '2011-08-16 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (8, '2011-08-31 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (9, '2011-08-30 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (10, '2011-08-29 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (11, '2011-08-28 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (12, '2011-08-27 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (13, '2011-08-26 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (14, '2011-08-25 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (15, '2011-08-24 00:00:00.000')
INSERT [dbo].[Test] ([Id], [NDate]) VALUES (16, '2011-08-23 00:00:00.000')
SET IDENTITY_INSERT [dbo].[Test] OFF