enter image description hereWe have the below table data format .. we have some recipe need output current recipe before one recipe data which is all sorting datetime
There is same recipe for B & D but i need recipe (B) set data only ..
declare #tm_variab table (Timecreated datetime,Recipe_Name varchar(80))
insert into #tm_variab select '2022-10-18 16:50:47' , 'TR1674FSHY'
insert into #tm_variab select '2022-10-18 16:50:42' , 'TR1674FSHY'
insert into #tm_variab select '2022-10-18 16:50:37' , 'TR1674FSHY'
insert into #tm_variab select '2022-10-18 16:50:32' , 'TR1674FSHY'
insert into #tm_variab select '2022-10-18 16:50:27' , 'TR1674FSHY' ----- current recipe (A)
insert into #tm_variab select '2022-10-18 16:50:22' , 'TR1674FSHY'
insert into #tm_variab select '2022-10-18 16:50:17' , 'TR1674FSHY'
insert into #tm_variab select '2022-10-18 16:50:12' , 'TR1674FSHY'
insert into #tm_variab select '2022-10-18 16:50:07' , 'TR1674FSkk'
insert into #tm_variab select '2022-10-18 16:50:07' , 'TR1674FSkk' --- recipe (B)
insert into #tm_variab select '2022-10-18 16:50:07' , 'TR1674FSkk'
insert into #tm_variab select '2022-10-18 16:50:07' , 'TR1674FSkk'
insert into #tm_variab select '2022-10-18 16:49:47' , 'TR19556ECDRE'
insert into #tm_variab select '2022-10-18 16:49:42' , 'TR19556ECDRE'
insert into #tm_variab select '2022-10-18 16:49:37' , 'TR19556ECDRE'
insert into #tm_variab select '2022-10-18 16:49:32' , 'TR19556ECDRE' ---- recipe(c)
insert into #tm_variab select '2022-10-18 16:49:27' , 'TR19556ECDRE'
insert into #tm_variab select '2022-10-18 16:49:22' , 'TR19556ECDRE'
insert into #tm_variab select '2022-10-18 16:49:17' , 'TR19556ECDRE'
insert into #tm_variab select '2022-10-18 16:48:07' , 'TR1674FSkk' --- recipe (D)
insert into #tm_variab select '2022-10-18 16:48:07' , 'TR1674FSkk'
;WITH CTE AS
(
SELECT
Timecreated
,Recipe_Name
, ROW_NUMBER() OVER(PARTITION BY Recipe_Name ORDER BY Timecreated DESC) AS rn
FROM #tm_variab
)
, newfinal as (
SELECT TOP(3)
Timecreated,
Recipe_Name
--, ROW_NUMBER() OVER(order BY Timecreated) AS rnii
FROM CTE WHERE rn=1
--ORDER BY Timecreated DESC
)
select Timecreated, Recipe_Name
,ROW_NUMBER() OVER(order BY Timecreated) AS rnii
into #final
From newfinal
order by Timecreated desc
select * from #final mm inner join #tm_variab kk on kk.Recipe_Name=mm.Recipe_Name and kk.Recipe_Name ='TR1674FSkk'
drop table #final
There is same recipe for B & D but i need recipe (B) set data only ..
I think you're looking for gaps in your data based on Timecreated.
Here's a query to detect these gaps:
SELECT tm.*
FROM #tm_variab tm
INNER JOIN (
SELECT *,rn = ROW_NUMBER() OVER(ORDER BY Timecreated DESC)
FROM (
SELECT *,LAG(Recipe_Name) OVER (order by Timecreated ) AS PRECEDENT
FROM #tm_variab
)a
WHERE Recipe_Name <> PRECEDENT
)a ON a.Recipe_Name = tm.Recipe_Name AND a.Timecreated = tm.Timecreated AND rn = 2
Related
In the result of my select I see 2 lines that's OK, but I want to see all columns like:
'Anne','Bauer','m1'
'Thomas','Neben','m3'
If (OBJECT_ID('tempdb..##test') Is Not Null)
Begin
Drop Table ##test
End
CREATE TABLE ##test (
givenname varchar(50),
surname varchar(50),
rann varchar(50)
);
INSERT INTO ##test VALUES ('Anne','Bauer','m1');
INSERT INTO ##test VALUES ('Klaus','Griebe','m2');
INSERT INTO ##test VALUES ('Thomas','Neben','m2');
INSERT INTO ##test VALUES ('Thomas','Neben','m3');
SELECT count(rann) as coun, rann
FROM ##test
group by rann
having count(rann) = 1
order by coun desc
You can try this:
select givenname,surname,rann from (
select *,count(*) over (partition by rann) rn from ##test
)t where rn=1
just use partition by with count
Select count(rann) over (partition by rann) [count], rann from ##test
I'm trying to get only a single row per Appointment Number in a table storing a history of appointments. It works fine with a few rows but then gets slower? Is this the best way to do this kind of check and I'm just missing some indexes or is there a better way?
DECLARE #temptable TABLE
(
id INT PRIMARY KEY NOT NULL
, ApptNumber INT NOT NULL
, ApptDate DATE NOT NULL
, Notes VARCHAR(50) NULL
)
INSERT INTO #temptable VALUES (1,1,'01-DEC-2018','First Appointment')
INSERT INTO #temptable VALUES (2,1,'01-DEC-2018','')
INSERT INTO #temptable VALUES (3,1,'01-DEC-2018','Rescheduled')
INSERT INTO #temptable VALUES (4,2,'02-DEC-2018','Second Appointment')
INSERT INTO #temptable VALUES (5,2,'02-DEC-2018','Cancelled')
INSERT INTO #temptable VALUES (6,3,'03-DEC-2018','Third Appointment')
INSERT INTO #temptable VALUES (7,4,'04-DEC-2018','Fourth Appointment')
SELECT * FROM #temptable
SELECT MAX(id) FROM #temptable GROUP BY ApptNumber
SELECT tt.* FROM #temptable tt
INNER JOIN (SELECT MAX(id) [Id] FROM #temptable GROUP BY ApptNumber) appts ON appts.Id = tt.id
Solution 1:
select * from (
SELECT f1.*, row_number() over(partition by ApptNumber order by id desc ) rang FROM #temptable f1
) tmp where rang=1
Solution 2:
with tmp as (
select ApptNumber, max(ID) MaxID
from #temptable
group by ApptNumber
)
select f1.* from #temptable f1 inner join tmp f2 on f1.ID=f2.MaxID
Solution 3:
select distinct f3.* from #temptable f1
cross apply
(
select top 1 * from #temptable f2
where f1.ApptNumber=f2.ApptNumber
order by f2.ID desc
) f3
Window function
SELECT tt.*
FROM (
SELECT *, row_number() over (partition by ApptNumber order by id desc) as rn
) tt
where tt.rn = 1
I have Data Table Example:
Declare #tb1 Table(ItemId int , GroupDuplicateId int, GroupType nvarchar(10))
Insert Into #tb1 Values(1,1,'IN'),(1,1,'VN'),(1,2,'IN'),(1,3,'IN'),(1,2,'VN'),(1,3,'VN'),(1,3,'SK')
Insert Into #tb1 Values(2,1,'IN'),(2,1,'VN')
select * from #tb1
Order by ItemId, GroupDuplicateId, GroupType
Now, I want get Items in 1 one group .
Example: ItemId 1 have GroupDuplicateId: 1, 2 ,3 . We need one of thems.
ItemId 2 satistify because, only 1 group.
Declare #tbResult1 Table(ItemId int , GroupDuplicateId int, GroupType nvarchar(10))
Insert Into #tbResult1 Values(1,1,'IN'),(1,1,'VN')
Insert Into #tbResult1 Values(2,1,'IN'),(2,1,'VN')
select * from #tbResult1
--2. The same 1. But, we need Group 3. It's have 3 Group Type: IN, VN, SK.
Insert Into #tbResult2 Values(1,3,'IN'),(1,3,'VN'),(1,3,'SK') -- Have max in Group
Insert Into #tbResult2 Values(2,1,'IN'),(2,1,'VN')
select * from #tbResult2
Thanks for your help.
Is this what you are looking for .?
Declare #tb1 Table(ItemId int , GroupDuplicateId int, GroupType nvarchar(10))
Insert Into #tb1 Values(1,1,'IN'),(1,1,'VN'),(1,2,'IN'),(1,3,'IN'),(1,2,'VN'),(1,3,'VN'),(1,3,'SK')
Insert Into #tb1 Values(2,1,'IN'),(2,1,'VN')
/*
select * from #tb1
Order by ItemId, GroupDuplicateId, GroupType */
;with cte as (
select *,row_number()over(partition by itemid,GroupType order by itemid,GroupDuplicateId)rn from #tb1
)
select * from cte where rn=1
How can I select only information that start with a #?
I have a table with 5 columns and in one of the columns i.e. the comments column there is information like:
#2345 Changed by Mark
Paul changed ticket number #5923
Someone changed ticket number #5823 and #9333
#3555 is missing from the list, can only see #5789, #9000 and #4568
In the sample of 4 rows above, I want my select statement to return only ticket numbers as shown below:
comments
#2345
#5923
#5823, #9333
#5789, #9000, #4568
Someone said regular expressions can do the work for me but I am fresh graduate and have never seen such before. Can you help me please??
Select the required fields from your database table using SQL, then perform regex operations on the result in another language such as c++, php etc. when outputting your result to the client
First Create Function To extract #numeric values
CREATE FUNCTION dbo.udf_GetNumeric
(
#strAlphaNumeric VARCHAR(256))
RETURNS VARCHAR(256
)
AS
BEGIN
DECLARE #intAlpha INT
SET #intAlpha = PATINDEX('%[^#0-9]%', #strAlphaNumeric)
BEGIN
WHILE #intAlpha > 0
BEGIN
SET #strAlphaNumeric = STUFF(#strAlphaNumeric, #intAlpha, 1, '' )
SET #intAlpha = PATINDEX('%[^#0-9]%', #strAlphaNumeric )
END
END
RETURN ISNULL(#strAlphaNumeric,0)
END
GO
By Using Below Code we can Extract the numeric values
Declare #TempTable TABLE(ID Int Identity,Value varchar(1000))
INSERT INTO #TempTable
SELECT '#2345 Changed by Mark'
UNION ALL SELECT 'Paul changed ticket number #5923'
UNION ALL SELECT 'Someone changed ticket number #5823 and #9333'
UNION ALL SELECT '#3555 is missing from the list, can only see #5789, #9000 and #4568'
SELECT ID,
RIGHT(LTRIM(REPLACE(Value,'#',' ,#')),LEN(RTRIM(LTRIM(REPLACE(Value,'#',' ,#'))))-1)AS Value FROM
(
SELECT ID,dbo.udf_GetNumeric(Value) AS Value From #TempTable
)Dt
If you need to extract Numeric values Instead of pre-fixed wirh '#' Symbol
SELECT ID,
REPLACE(RIGHT(LTRIM(REPLACE(Value,'#',' ,#')),LEN(RTRIM(LTRIM(REPLACE(Value,'#',' ,#'))))-1),'#',' ')AS Value FROM
(
SELECT ID,dbo.udf_GetNumeric(Value) AS Value From #TempTable
)Dt
OutPut
ID Value
----------
1 #2345
2 #5923
3 #5823 ,#9333
4 #3555 ,#5789 ,#9000 ,#4568
IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
Drop table #TempTable
CREATE TABLE [dbo].#TempTable(
[ID] [int] NOT NULL,
[Value] [varchar](1000) NULL
) ON [PRIMARY]
INSERT [dbo].#TempTable ([ID], [Value]) VALUES (1, N'#2345 Changed by Mark #111111,767677,33333,#5656 vbvb')
GO
INSERT [dbo].#TempTable ([ID], [Value]) VALUES (2, N'Paul changed ticket number #5923,5555555 464646 #010101,5555544rrr,wwww AND #4 ')
GO
INSERT [dbo].#TempTable ([ID], [Value]) VALUES (3, N'Someone changed ticket number #5823 and #9333,7777')
GO
INSERT [dbo].#TempTable ([ID], [Value]) VALUES (4, N'#3555 is missing from the list, can only see #5789, #9000 and #4568')
GO
;WITH cte
AS
(
SELECT ID,Split.a.value('.', 'VARCHAR(1000)') AS TablesData
FROM (
SELECT ID,CAST('<S>' + REPLACE(Value, ' ', '</S><S>') + '</S>' AS XML) AS TablesData
FROM #TempTable
) AS A
CROSS APPLY TablesData.nodes('/S') AS Split(a)
)
,Final
AS
(
SELECT ID,TablesData FROM
(
SELECT ID, Split.a.value('.', 'VARCHAR(1000)') AS TablesData,CHARINDEX('#',Split.a.value('.', 'VARCHAR(1000)')) AS Data2
FROM (
SELECT ID,CAST('<S>' + REPLACE(TablesData, ',', '</S><S>') + '</S>' AS XML) AS TablesData
FROM cte
) AS A
CROSS APPLY TablesData.nodes('/S') AS Split(a)
)DT
WHERE dt.Data2 <>0
)
SELECT DISTINCT ID
,STUFF((
SELECT ', ' + CAST(TablesData AS VARCHAR(50))
FROM Final i
WHERE i.ID = o.ID
FOR XML PATH('')
), 1, 1, '') AS ColumnsWith#ValuesOnly
FROM Final o
ORDER BY 1 ASC
OutPut
ID ColumnsWith#ValuesOnly
---------------------------
1 #2345, #111111, #5656
2 #5923, #010101, #4
3 #5823, #9333
4 #3555, #5789, #9000, #4568
The best way is to use this script below:
IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
Drop table #TempTable
CREATE TABLE [dbo].#TempTable(
[ID] [int] NOT NULL,
[Value] [varchar](1000) NULL
) ON [PRIMARY]
INSERT [dbo].#TempTable ([ID], [Value]) VALUES (1, N'#2345 Changed by Mark #111111,767677,33333,#5656 vbvb')
GO
INSERT [dbo].#TempTable ([ID], [Value]) VALUES (2, N'Paul changed ticket number #5923,5555555 464646 #010101,5555544rrr,wwww AND #4 ')
GO
INSERT [dbo].#TempTable ([ID], [Value]) VALUES (3, N'Someone changed ticket number #5823 and #9333,7777')
GO
INSERT [dbo].#TempTable ([ID], [Value]) VALUES (4, N'#3555 is missing from the list, can only see #5789, #9000 and #4568')
GO
;WITH cte
AS
(
SELECT ID,Split.a.value('.', 'VARCHAR(1000)') AS TablesData
FROM (
SELECT ID,CAST('<S>' + REPLACE(Value, ' ', '</S><S>') + '</S>' AS XML) AS TablesData
FROM #TempTable
) AS A
CROSS APPLY TablesData.nodes('/S') AS Split(a)
)
,Final
AS
(
SELECT ID,TablesData FROM
(
SELECT ID, Split.a.value('.', 'VARCHAR(1000)') AS TablesData,CHARINDEX('#',Split.a.value('.', 'VARCHAR(1000)')) AS Data2
FROM (
SELECT ID,CAST('<S>' + REPLACE(TablesData, ',', '</S><S>') + '</S>' AS XML) AS TablesData
FROM cte
) AS A
CROSS APPLY TablesData.nodes('/S') AS Split(a)
)DT
WHERE dt.Data2 <>0
)
SELECT DISTINCT ID
,STUFF((
SELECT ', ' + CAST(TablesData AS VARCHAR(50))
FROM Final i
WHERE i.ID = o.ID
FOR XML PATH('')
), 1, 1, '') AS ColumnsWith#ValuesOnly
FROM Final o
ORDER BY 1 ASC
I've vehicle tracking data, I want to create Vehicle stoppage report. Help me to write sql query for that. Actual Data and resultant data are like Table1 and Table2 respectively.
Thank you!
Ok, here is some working statement
declare #t TABLE(id INT, dt DATETIME, sp int)
INSERT INTO #t VALUES(1, '2015-01-17 12:00:05', 12)
INSERT INTO #t VALUES(1, '2015-01-17 12:00:06', 0)
INSERT INTO #t VALUES(1, '2015-01-17 12:00:07', 0)
INSERT INTO #t VALUES(1, '2015-01-17 12:00:08', 0)
INSERT INTO #t VALUES(1, '2015-01-17 12:00:09', 5)
INSERT INTO #t VALUES(1, '2015-01-17 12:00:10', 8)
INSERT INTO #t VALUES(1, '2015-01-17 12:00:11', 0)
INSERT INTO #t VALUES(1, '2015-01-17 12:00:12', 0)
INSERT INTO #t VALUES(1, '2015-01-17 12:00:13', 0)
INSERT INTO #t VALUES(1, '2015-01-17 12:00:14', 7)
INSERT INTO #t VALUES(2, '2015-01-17 12:00:05', 10)
INSERT INTO #t VALUES(2, '2015-01-17 12:00:06', 0)
INSERT INTO #t VALUES(2, '2015-01-17 12:00:07', 0)
INSERT INTO #t VALUES(2, '2015-01-17 12:00:08', 0)
INSERT INTO #t VALUES(2, '2015-01-17 12:00:09', 12)
INSERT INTO #t VALUES(2, '2015-01-17 12:00:10', 0)
INSERT INTO #t VALUES(2, '2015-01-17 12:00:11', 0)
INSERT INTO #t VALUES(2, '2015-01-17 12:00:12', 10)
;
WITH cte1
AS ( SELECT a.id ,
a.dt AS stdt ,
b.dt AS endt ,
a.sp ,
a.rn
FROM ( SELECT * ,
1
+ ROW_NUMBER() OVER ( PARTITION BY id ORDER BY dt ) AS n ,
ROW_NUMBER() OVER ( ORDER BY GETDATE() ) AS rn
FROM #t t1
) a
LEFT JOIN ( SELECT * ,
ROW_NUMBER() OVER ( PARTITION BY id ORDER BY dt ) AS n
FROM #t t1
) b ON b.id = a.id
AND b.n = a.n
),
cte2
AS ( SELECT t1.id ,
t1.stdt ,
ISNULL(t1.endt, GETDATE()) AS endt ,
t1.sp ,
t1.rn ,
SUM(t2.sp) AS sum
FROM cte1 t1
INNER JOIN cte1 t2 ON t1.rn >= t2.rn
GROUP BY t1.id ,
t1.stdt ,
t1.endt ,
t1.sp ,
t1.rn
)
SELECT id ,
MIN(stdt) ,
MAX(stdt) ,
DATEDIFF(ss, MIN(stdt), MAX(stdt))
FROM cte2
WHERE sp = 0
GROUP BY id ,
SUM
Here is result
1 2015-01-17 12:00:06.000 2015-01-17 12:00:08.000 2
1 2015-01-17 12:00:11.000 2015-01-17 12:00:13.000 2
2 2015-01-17 12:00:06.000 2015-01-17 12:00:08.000 2
2 2015-01-17 12:00:10.000 2015-01-17 12:00:11.000 1
You have result for vehicle 1 12:06 to 12:08. But i really think that results should be 12:06 to 12:09. If so, just change
SELECT id ,
MIN(stdt) ,
MAX(stdt) ,
DATEDIFF(ss, MIN(stdt), MAX(stdt))
FROM cte2
WHERE sp = 0
GROUP BY id ,
SUM
to
SELECT id ,
MIN(stdt) ,
MAX(endt) ,
DATEDIFF(ss, MIN(stdt), MAX(endt))
FROM cte2
WHERE sp = 0
GROUP BY id ,
SUM