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
Related
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
I have a table with these columns:
id (pk, int identity), imei (varchar), name (varchar), lastconnected (datetime)
Some of the entries in this table have the same name and imei, but different id and different lastconnected date.
How can I effectively filter out all entries that have duplicates (with a SQL script), and then delete the one with the latest lastconnected date?
A simple ROW_NUMBER and DELETE should do the trick:
WITH CTE AS
(
SELECT *,
RN = ROW_NUMBER() OVER(PARTITION BY imei, [name] ORDER BY lastconnected DESC)
FROM dbo.YourTable
)
DELETE FROM CTE
WHERE RN = 1;
This is easy and will solve your problem
DECLARE #table TABLE
(
id int,
name varchar(10),
imei varchar(10)
)
insert into #table select 1, 'a','a'
insert into #table select 2, 'b','a'
insert into #table select 3, 'c','a'
insert into #table select 4, 'a','a'
insert into #table select 5, 'c','a'
insert into #table select 6, 'a','a'
insert into #table select 7, 'c','a'
insert into #table select 8, 'a','a'
WHILE (exists (select '' from #table group by name , imei having count(*) > 1))
BEGIN
delete from #table where id in (
select max(id) from #table group by imei , name having count(*) > 1)
End
select * from #table
My first instinct is to use RANK(). This will delete all duplicates, not just the most recent, in cases where things are duplicated multiple times.
delete a
from (
select id, imei, name, lastconnected, RANK() over(partition by imei, name order by lastconnected) as [rank] from #temp
) as a
where a.rank>1
It selects the maximum of the date for each combination of name and iemi and then deletes that particular row.
DELETE FROM yourtablee
WHERE (lastconnecteddate,name,imei) in
(SELECT max(lastconnecteddate), name,imei
FROM yourtable
GROUP BY name,imei)
I want to get distinct dates from my dbtable named tblFormno2 in an ascending order.For that i've written the following query but its not working properly.
Column date_submit is declared as datetime
select distinct (convert(nvarchar(100),date_submit,103)) as dob from
tblFormno2 order by dob asc
Here the the output is shown as
05/07/2011
06/03/2011
06/07/2011
07/04/2011
08/01/2012
instead of
06/03/2011
07/04/2011
05/07/2011
06/07/2011
08/01/2012
How to solve this problem ???
How about
select convert(nvarchar(10), date_submit_inner, 103) as date_submit from
(
select distinct date_submit as date_submit_inner from tblFormno2
) as T
order by T.date_submit_inner asc
Your order by is not sorting by date_submit from the table. Is is sorting by the named output column of date_submit. If you specific the table name in the order by it should work. If that doesn't work, then try giving the output a different name than the table column.
select distinct (Convert(nvarchar(100),date_submit,103)) as date_submit
from tblFormno2
order by tblFormno2.date_submit asc
create table #temp
(
DT varchar(20)
)
Insert into #temp(DT)values('13/05/2011')
Insert into #temp(DT)values('03/06/2011')
Insert into #temp(DT)values('07/06/2011')
Insert into #temp(DT)values('04/07/2011')
Insert into #temp(DT)values('01/08/2011')
Select * from #temp
Below are the database records...
select (convert(varchar,Dt,107)) t into #t from #temp
select * from #t
drop table #temp
drop table #t
I have 2 temp Tables [Description] and [Institution], I want to have these two in one table.
They are both tables that look like this:
Table1; #T1
|Description|
blabla
blahblah
blagblag
Table2; #T2
|Institution|
Inst1
Inst2
Inst3
I want to get it like this:
Table3; #T3
|Description| |Institution|
blabla Inst1
blahblah Inst2
blagblag Inst3
They are already in sort order.
I just need to get them next to each other..
Last time I asked was something almost the same.
I used this query
Create Table #T3
(
[From] Datetime
,[To] Datetime
)
INSERT INTO #T3
SELECT #T1.[From]
, MIN(#T2.[To])
FROM #T1
JOIN #T2 ON #T1.[From] < #T2.[To]
GROUP BY #T1.[From]
Select * from #T3
It did work for the date values, but it won't work here ? :s
Thank you.
One thing that concerns me is that you say that the values "are already in sort order". There really is no default sort order -- if you don't specify a sort order, you are at the mercy of SQL Server to determine the order in which the data is returned. The solution below assumes that there is some way to sort the data such that the records "match up" (using the ORDER BY clauses).
Hope this helps,
John
-- Table 1 test data
Create Table #T1
(
[Description] nvarchar(30)
)
INSERT INTO #T1 ([Description]) VALUES ('desc1')
INSERT INTO #T1 ([Description]) VALUES ('desc2')
INSERT INTO #T1 ([Description]) VALUES ('desc3')
-- Table 2 test data
Create Table #T2
(
[Institution] nvarchar(30)
)
INSERT INTO #T2 (Institution) VALUES ('Inst1')
INSERT INTO #T2 (Institution) VALUES ('Inst2')
INSERT INTO #T2 (Institution) VALUES ('Inst3')
-- Create table 3
Create Table #T3
(
[Description] nvarchar(30),
[Institution] nvarchar(30)
);
-- Use CTE2 to add row numbers to the data; use the row numbers to join the tables
-- you must specify the sort order for the data in the tables
WITH CTE1 (Description, RowNum) AS
(
SELECT [Description], ROW_NUMBER() OVER(ORDER BY [Description]) as RowNum
FROM #T1
),
CTE2 (Institution, RowNum) AS
(
SELECT Institution, ROW_NUMBER() OVER(ORDER BY Institution) as RowNum
FROM #T2
)
INSERT INTO #T3
SELECT CTE1.Description, CTE2.Institution
FROM CTE1
LEFT JOIN CTE2 ON CTE1.RowNum = CTE2.RowNum
Select * from #T3