get rows and rows from one table SQL server - sql

i have comments table in sql server structured as
CREATE TABLE [dbo].[LS_Commentes](
[CommentId] [int] IDENTITY(1,1) NOT NULL,
[OwnerId] [uniqueidentifier] NULL,
[OwnerName] [nvarchar](50) NULL,
[Email] [nvarchar](250) NULL,
[Date] [nvarchar](15) NULL,
[ParentId] [int] NULL,
[CommentText] [nvarchar](400) NULL,
[ItemId] [int] NULL,
[upVotes] [int] NULL,
[downVotes] [int] NULL,
[isApproved] [bit] NULL,
CONSTRAINT [PK_LS_MsgCommentes] PRIMARY KEY CLUSTERED
(
[CommentId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
and i have sample data like this:
CommentId OwnerId OwnerName Email Date ParentId CommentText ItemId upVotes downVotes isApproved
1 NULL Test Commneter NULL 1/4/2013 NULL test 9 0 0 NULL
2 NULL Test Commneter NULL 1/4/2013 1 test NULL 0 0 NULL
3 NULL Test Commneter NULL 1/4/2013 1 test NULL 0 0 NULL
i want to write one query can get me all rows have itemid =9 and rows have parentid= comment id that selected (because itemid = 9)
look here i can solve it by adding item id 9 to the sub comments too but i just want to know if that could be solved without adding item id to comments and sub comments

I think the following query does what you want:
select *
from ls_comments c
where c.itemID = 9 or
c.parentID in (select c2.commentId from ls_comments c2 where c2.itemId = 9)

Would a recursive Common Table Expression give you the results you're after?
;with cte as
(
--Anchor
select
commentid,
ParentId
from
LS_Commentes
where
ItemId = 9
union all
--Recursive member
select
c.commentId,
c.ParentId
from
LS_Commentes c join cte on c.ParentId = cte.CommentId
)
select * from cte
If you want to include more columns in the results ensure that both parts (the Anchor and recursive member) have identical columns.
Explanation:
The anchor part (the first select) of a recursive query selects all rows where ItemId = 9, the second part uses the existing records in the result to include further records that satisfy it's criters (ParentId = cte.CommentId) this keeps going until nothing more is selected. And then the entire results must be selected at the end (after the CTEs definition)

I think it would be good with an embedded SQL query
SELECT *
FROM `LS_Commentes`
WHERE `ItemId` = '9'
AND `ParentID`= (SELECT `CommentID` FROM `LS_Commentes` WHERE `ItemId` = 9);

Related

Dynamic column create from child table

I have two table one for master and another child. Which is depicting below
Master table:
and scripts:
CREATE TABLE [dbo].[SET_HRShiftProfile](
[id] [smallint] NOT NULL,
[LocationID] [tinyint] NOT NULL,
[ShiftTypeID] [smallint] NOT NULL,
[ProfileName] [nvarchar](50) NULL,
[EmpTypeCode] [nvarchar](10) NOT NULL,
[IsActive] [bit] NOT NULL,
CONSTRAINT [PK_HR_ShiftProfile] 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
Child Table:
And script:
CREATE TABLE [dbo].[SET_HRShiftProfileDetail](
[id] [int] NOT NULL,
[LocationID] [tinyint] NOT NULL,
[ShiftprofileID] [smallint] NOT NULL,
[ShiftProfTypeCode] [nvarchar](10) NOT NULL,
[Start] [nvarchar](5) NULL,
[End] [nvarchar](5) NULL,
[ToleranceBefore] [nvarchar](5) NULL,
[ToleranceAfter] [nvarchar](5) NULL,
CONSTRAINT [PK_SET_HRShiftProfileDetail] 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
I need to final query result look like this, means all child table row will be column for master table
Any idea give me appreciate. Thanks.
Try this:
with pivoted as
(
select LocationID, ShiftprofileID, [IN], [OUT], [Break]
from (
select distinct LocationID, ShiftprofileID, [ShiftProfTypeCode]
from [dbo].[SET_HRShiftProfileDetail]
) as t
pivot (
count([ShiftProfTypeCode]) for [ShiftProfTypeCode] in ([IN], [OUT], [Break])
) as p
)
select p.*
, x.[IN], x.[OUT], x.[Break]
from [dbo].[SET_HRShiftProfile] as p
left join pivoted x on x.ShiftprofileID = p.id
and x.locationid = p.locationid
Results:
id LocationID ShiftTypeID ProfileName EmpTypeCode IsActive IN OUT Break
1003 1 1001 Day-Summar REG 1 1 1 1
1006 1 1005 Say-Winter REG 1 1 1 0
Since you said nothing about what data you like to get into cells, I have placed there the count of rows with such value (as column name) from the child table.
Hope it will help you to solve your problem
LATER EDIT
Solution without using PIVOT:
with pivoted as
(
select LocationID
, ShiftprofileID
, sum(case when [ShiftProfTypeCode] = 'OUT' then 1 else 0 end) [OUT]
, sum(case when [ShiftProfTypeCode] = 'IN' then 1 else 0 end) [IN]
, sum(case when [ShiftProfTypeCode] = 'Break' then 1 else 0 end) [Break]
from [dbo].[SET_HRShiftProfileDetail]
group by LocationID, ShiftprofileID
)
select p.*
, x.[IN], x.[OUT], x.[Break]
from [dbo].[SET_HRShiftProfile] as p
left join pivoted x on x.ShiftprofileID = p.id
and x.locationid = p.locationid
Results:
id LocationID ShiftTypeID ProfileName EmpTypeCode IsActive IN OUT Break
1003 1 1001 Day-Summar REG 1 1 1 1
1006 1 1005 Say-Winter REG 1 1 1 0

Sub queries vs joins when counting children from same table and occurrences in other table

CREATE TABLE [dbo].[Comment]
(
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserId] [int] NOT NULL,
[Comment] [nvarchar](1024) NOT NULL,
[Created] [datetime] NOT NULL CONSTRAINT [DF_Comment_Created] DEFAULT (getdate()),
[ContentId] [int] NOT NULL,
[ParentId] [int] NULL,
CONSTRAINT [PK_Comment] 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]
CREATE TABLE [dbo].[CommentLike]
(
[CommentId] [int] NOT NULL,
[UserId] [int] NOT NULL,
CONSTRAINT [PK_CommentLike] PRIMARY KEY CLUSTERED
(
[CommentId] ASC,
[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[User]
(
[Id] [int] IDENTITY(1,1) NOT NULL,
[Username] [nvarchar](64) NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
[Email] [nvarchar](255) NOT NULL,
CONSTRAINT [PK_User] 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]
There is a non clustered index on Comment (ContentId, ParentId).
In short the Comment table contains comments and sub comments via Id and ParentId. The CommentLike table contains likes (one like per user) for comments and sub comments.
The Comment table contains about 8000 rows and the CommentLike table 1800.
I´ve created a query that lists comments (only top level comments), sub comment count, like count and also a value that indicates if supplied user likes each comment. The whole query is filtered on ContentId in the where clause (simply a unique integer value that represents an id in another system)
I have one version that uses sub queries and the other joins (on sub queries).
Sub query version:
select
c.Id,
c.Comment,
c.Created,
c.ContentId,
(select count(Id) from Comment where ParentId = c.Id) as SubComments,
(select count(UserId) from CommentLike where CommentId = c.Id) as Likes,
(select count(UserId) from CommentLike where CommentId = c.Id and UserId = #currentUserId) as CurrentUserIsLiking
from Comment c
where c.ContentId = #contentId and c.ParentId is null
group by
c.Id, c.Comment, c.Created, c.ContentId
Join version:
select
c.Id,
c.Comment,
c.Created,
c.ContentId,
isnull(c2.SubComments, 0) as SubComments,
isnull(cl.Likes, 0) as Likes,
isnull(cl.CurrentUserIsLiking, 0) as CurrentUserIsLiking
from Comment c
left join
(
select
ParentId,
count(Id) as SubComments
from Comment
group by ParentId
) as c2
on c.Id = c2.ParentId
left join
(
select
CommentId,
count(UserId) as Likes,
count(case when UserId = #currentUserId then 1 else null end) as CurrentUserIsLiking
from CommentLike
group by CommentId
) as cl
on c.Id = cl.CommentId
where c.ContentId = #contentId and c.ParentId is null
group by
c.Id, c.Comment, c.Created, c.ContentId,
c2.SubComments, cl.Likes, cl.CurrentUserIsLiking
On average both versions run below 600ms but the sub query version always seems to run about 20% faster than the join version.
The question:
No matter how many rows the tables contain the sub query version is always faster than the join version. I've always thought that, performance wise, joins are better than sub queries, is that not true in this case? Since performance is important I wonder if there is any optimization that can be done to either of the versions to make that specific version outperform the other?
Why are you aggregating in the outer query for the join version?
select c.*,
p.SubComments,
coalesce(cl.Likes, 0) as Likes,
coalesce(cl.CurrentUserIsLiking, 0) as CurrentUserIsLiking
from Comment c left join
(select ParentId, count(*) as SubComments
from Comment
group by ParentId
) p
on p.ParentId = c.Id
(select CommentId, count(UserId) as Likes,
sum(case when UserId = #currentUserId then 1 else 0
end) as CurrentUserIsLiking
from CommentLike
group by CommentId
) cl
on c.Id = cl.CommentId
where c.ContentId = #contentId and c.ParentId is null;
Because the outer query is limiting the comments, it is quite reasonable that the version using correlated subqueries would be faster.

SQL Descending ordered LEFT JOIN subquery issue

I have the following query.
SELECT r1.*,
r2.vlag54,
r2.vlag55
FROM [rxmon].[dbo].[a] AS r1
LEFT JOIN [rxmon].[dbo].[b] AS r2
ON r2.artikelnummer = r1.drug_id
LEFT JOIN (SELECT *
FROM [rxmon].[dbo].[c]) AS r3
ON r3.pid = r1.patient_id
WHERE r3.obx_id = 20937
AND Cast(r3.obx_datetime AS DATE) = Cast(Getdate() - 1 AS DATE)
AND r1.patient_id = 7092425
AND obx_value < CASE
WHEN r2.vlag54 = 1 THEN 30
WHEN r2.vlag55 = 1 THEN 50
END
AND r2.vlag54 = CASE
WHEN r3.obx_value < 30 THEN 1
ELSE 0
END
AND r2.vlag55 = CASE
WHEN r3.obx_value BETWEEN 30 AND 50 THEN 1
ELSE 0
END
ORDER BY obx_datetime DESC;
The problem is that table C can contain multiple records based on de PID join. This generates the same records because of the multiple records on table C.
The table C needs to e joined as the latest record only so just 1 of C. That way the table A record will not be repeated.
I tried TOP 1 and order by but that can't be used in subquery.
-- TABLE A
CREATE TABLE [dbo].[A]
[EVS_MO_ID] [bigint] NOT NULL,
[DRUG_ID] [varchar](50) NOT NULL,
[ATC_CODE] [varchar](15) NULL,
[DRUG_NAME] [varchar](1024) NULL,
[PATIENT_ID] [varchar](50) NOT NULL,
[PATIENT_LOCATION] [varchar](10) NULL,
[MO_DATE] [datetime2](7) NOT NULL,
[MO_START_DATE] [datetime2](7) NOT NULL,
[MO_STOP_DATE] [datetime2](7) NULL,
[ROUTE] [varchar](50) NULL,
[MEDICATION_CONTAINER] [smallint] NULL,
[PRESCRIBING_DOCTOR_NAME] [varchar](50) NULL,
[PRESCRIBING_DOCTOR_SURNAME] [varchar](50) NULL,
[MO_ACTIVE] [bit] NOT NULL,
CONSTRAINT [PK_MedicationOrders] PRIMARY KEY CLUSTERED
(
[EVS_MO_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = ON, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
INSERT INTO [dbo].[A]
VALUES
(5411409,'97941689', 'B01AB06','NADROPARINE 0.8ML','7092425','ANBC', '2015-12-15 20:58:06.2030000',
'2015-12-16 00:00:00.0000000', '', 'IV', 1, 'GEORGE','LAST', 1);
-- TABLE B
CREATE TABLE [dbo].[B](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ARTIKELNUMMER] [varchar](50) NOT NULL,
[VLAG54] [bit] NULL,
[VLAG55] [bit] NULL CONSTRAINT [DF_Table_1_VLAG50] DEFAULT ((0)),
[VLAG100] [bit] NULL CONSTRAINT [DF_ArtikelVlaggen_VLAG100] DEFAULT ((0)),
CONSTRAINT [PK_B] 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]
INSERT INTO [dbo].[B]
([ARTIKELNUMMER]
,[VLAG54]
,[VLAG55]
,[VLAG100])
VALUES
('97941689', 1,0,1);
-- TABLE C
CREATE TABLE [dbo].[C](
[ID] [int] IDENTITY(1,1) NOT NULL,
[OBX_DATETIME] [datetime2](7) NOT NULL,
[PID] [int] NOT NULL,
[DEPARTMENT] [varchar](8) NOT NULL,
[OBX_ID] [int] NOT NULL,
[OBX_VALUE] [decimal](5, 2) NOT NULL,
[OBX_UNITS] [varchar](10) NULL,
[REF_RANGE] [varchar](40) NULL,
[FLAG] [varchar](2) NULL,
CONSTRAINT [PK_C] 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]
INSERT INTO [dbo].[C]
([OBX_DATETIME]
,[PID]
,[DEPARTMENT]
,[OBX_ID]
,[OBX_VALUE]
,[OBX_UNITS]
,[REF_RANGE]
,[FLAG])
VALUES
('2015-12-15 14:01:00.0000000',7092425, '8NAH', 20937, 27.00, 'mL/min', '> 60', 'L');
INSERT INTO [dbo].[C]
([OBX_DATETIME]
,[PID]
,[DEPARTMENT]
,[OBX_ID]
,[OBX_VALUE]
,[OBX_UNITS]
,[REF_RANGE]
,[FLAG])
VALUES
('2015-12-15 06:30:00.0000000',7092425, '6ZPA', 20937, 28.00, 'mL/min', '> 60', 'L');
This will order them by OBX_DATETIME and take only the first one:
...
LEFT JOIN (
SELECT pid, obx_id, obx_datetime, obx_value
, n = ROW_NUMBER() over(PARTITION BY pid ORDER BY obx_datetime desc)
FROM [rxmon].[dbo].[c]
) AS r3
ON r3.pid = r1.patient_id and r3.n = 1
...
If OBX_DATETIME are inserted incrementaly (newer date only), you can order by ID instead.
This SQL Fiddle with your query and sample data/tables returns 2 rows: http://sqlfiddle.com/#!3/df36c/2/0
This SQL Fiddle with the new subquery returns 1 row: http://sqlfiddle.com/#!3/df36c/1/0
You are using a LEFT JOIN on r3 but have also have r3 in your WHERE clause with equal operator:
WHERE r3.obx_id = 20937
AND Cast(r3.obx_datetime AS DATE) = Cast(Getdate() - 1 AS DATE)
It will remove NULL value from the left join on r3. Perhaps you should also move it to the sub query or use INNER JOIN.
You should also avoind using the DB name in your query unless this query is run from another DB on the same server. This will be fine:
SELECT ... FROM [dbo].[a] AS r1 ...
Using SELECT * is also a bad habit. You should list only the columns your code will use.
try this.... #Shift
SELECT r1.*,
r2.vlag54,
r2.vlag55
FROM [dbo].[a] AS r1
LEFT JOIN [dbo].[b] AS r2
ON r2.artikelnummer = r1.drug_id
LEFT JOIN (
SELECT
ROW_NUMBER() OVER (PARTITION BY pid ORDER BY id DESC) RN,
c.*
FROM C
) r3
ON r3.pid = r1.patient_id AND r3.RN = 1
WHERE r3.obx_id = 20937
AND Cast(r3.obx_datetime AS DATE) = Cast(Getdate() - 1 AS DATE)
AND r1.patient_id = 7092425
AND obx_value < CASE
WHEN r2.vlag54 = 1 THEN 30
WHEN r2.vlag55 = 1 THEN 50
END
AND r2.vlag54 = CASE
WHEN r3.obx_value < 30 THEN 1
ELSE 0
END
AND r2.vlag55 = CASE
WHEN r3.obx_value BETWEEN 30 AND 50 THEN 1
ELSE 0
END
ORDER BY obx_datetime DESC;

How to determine size of continious range for given criteria?

I have a positions table in SQL Server 2008R2 (definition below).
In the system boxes there are positions.
I have a requirement to find a box, which has X free positions remaining. However, the X positions must be continuous (left to right, top to bottom i.e. ascending PositionID).
It has been simple to construct a query that finds a box with X positions free. I now have the problem of determining if the positions are continuous.
Any suggestions on a TSQL based solution?
Table Definition
` CREATE TABLE [dbo].[Position](
[PositionID] [int] IDENTITY(1,1) NOT NULL,
[BoxID] [int] NOT NULL,
[pRow] [int] NOT NULL,
[pColumn] [int] NOT NULL,
[pRowLetter] [char](1) NOT NULL,
[pColumnLetter] [char](1) NOT NULL,
[SampleID] [int] NULL,
[ChangeReason] [nvarchar](4000) NOT NULL,
[LastUserID] [int] NOT NULL,
[TTSID] [bigint] NULL,
CONSTRAINT [PK_Position] PRIMARY KEY CLUSTERED
(
[PositionID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]`
Edit
http://pastebin.com/V8DLiucN - pastebin link with sample positions for 1 box (all positions empty in sample data)
Edit 2
A 'free' position is one with SampleID = null
DECLARE #AvailableSlots INT
SET #AvailableSlots = 25
;WITH OrderedSet AS (
SELECT
BoxID,
PositionID,
Row_Number() OVER (PARTITION BY BoxID ORDER BY PositionID) AS rn
FROM
Position
WHERE
SampleID IS NULL
)
SELECT
BoxID,
COUNT(*) AS AvailableSlots,
MIN(PositionID) AS StartingPosition,
MAX(PositionID) AS EndingPosition
FROM
OrderedSet
GROUP BY
PositionID - rn,
BoxID
HAVING
COUNT(*) >= #AvailableSlots
The trick is the PositionID - rn (row number) in the GROUP BY statement. This works to group together continuous sets... and from there it's easy to just do a HAVING to limit the results to the BoxIDs that have the required amount of free slots.

How can i remove duplicates from this Sql Server Table?

I have a single table in the Sql Server 2008 r2 DB. Every few seconds I import data into this table. At one point, the import was failing, so it was constantly importing the same data, creating duplicates. (basically, if the import read 20 lines, imported 19 and failed on 20 .. then those 19 were not in a transaction .. and thus got inserted).
Anyways, I'm trying to figure out how I can I remove all the duplicates and just the first (original) inserted row?
Here's the table schema - and please note that there's a few nullable fields.
CREATE TABLE [dbo].[LogEntries](
[LogEntryId] [int] IDENTITY(1,1) NOT NULL,
[GameFileId] [int] NOT NULL,
[CreatedOn] [datetimeoffset](7) NOT NULL,
[EventTypeId] [tinyint] NOT NULL,
[Message] [nvarchar](max) NULL,
[Code] [int] NULL,
[Violation] [nvarchar](100) NULL,
[ClientName] [nvarchar](100) NULL,
[ClientGuid] [nvarchar](50) NULL,
[ClientGuidReversed] [nvarchar](50) NULL,
[ClientIpAndPort] [nvarchar](50) NULL,
CONSTRAINT [PK_LogEntries] PRIMARY KEY CLUSTERED
(
[LogEntryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Cheers :)
UPDATE: What are Duplicates (in this case?)
Damn sorry. forgot to define a duplicate.
The LogEntryId is unique, so ignore that piece of info (it's not imported). all the rest of the data is imported. Here's two rows of data that are identical.
6459749 39 2010-11-05 00:00:25.0000000 +11:00 6 Violation (MULTIHACK) #70805 70805 MULTIHACK angelb aeda202c22ed41f7301d0673647c55d8 8d55c7463760d1037f14de22c202adea 220.246.157.194:57133
6459766 39 2010-11-05 00:00:25.0000000 +11:00 6 Violation (MULTIHACK) #70805 70805 MULTIHACK angelb aeda202c22ed41f7301d0673647c55d8 8d55c7463760d1037f14de22c202adea 220.246.157.194:57133
and to compare this to the top 5 ordered by desc
6505931 40 2010-11-08 23:39:16.0000000 +11:00 4 NULL NULL NULL Zaphrolio 69ae1bfea616c244e5c223e51d5ceb8e e8bec5d15e322c5e442c616aefb1ea96 175.38.209.80:10000
6505930 39 2010-11-08 23:39:04.0000000 +11:00 3 NULL NULL NULL imBakedAsBro 8cf1b3b6a389229fa4adeec07dc087ce ec780cd70ceeda4af922983a6b3b1fc8 110.175.83.45:10000
6505929 39 2010-11-08 23:39:03.0000000 +11:00 2 NULL NULL NULL imBakedAsBro NULL NULL 110.175.83.45:10000
6505928 80 2010-11-08 23:39:04.0000000 +11:00 4 NULL NULL NULL Asmo74 5ccf5ee85a6cf08da563bdcbfe75351d d15357efbcdb365ad80fc6a58ee5fcc5 61.68.212.231:50273
6505927 80 2010-11-08 23:39:03.0000000 +11:00 4 NULL NULL NULL McJellyfish c48218542918bec900a331a81e0a9d05 50d9a0e18a133a009ceb81924581284c 60.225.3.2:10000
with cte as (
select row_number() over (
partition by
[GameFileId]
, [CreatedOn]
, [EventTypeId]
, [Message]
, [Code]
, [Violation]
, [ClientName]
, [ClientGuid]
, [ClientGuidReversed]
, [ClientIpAndPort]
order by [LogEntryId]) as rn
from LogEntries)
delete from cte
where rn > 1;
in the abscence of any other information, UNION ALL is normally a good trick to blat out dupes
select * from table
union
select * from table
Edited to reflect typo in comment...