Say I have a table as follows
Employee RecNumber
Joe Bloggs 123456
Joe Bloggs 123456
Bob Bloggs 123457
Dup Bloggs 123456
And I just want to return all Rec Numbers where 2 People have had the same RecNumber which shouldn't happen.
Note that one person can have same request number multiple times I just want returned where 2 people have the same rec Number
So all I want returned is
123456
select record_number
from my_table
group by record_number
having count(distinct employee) > 1
SELECT A.RECNUMBER,COUNT(B.EMPLOYEE)
(SELECT DISTINCT RECNUMBER FROM YOURTABLE) A,
YOURTABLE B
WHERE A.RECNUMBER = B.RECNUMBER
GROUP BY A.RECNUMBER
HAVING COUNT(B.EMPLOYEE)=2;
Something like this?
SELECT RecNumber FROM Table GROUP BY RecNumber HAVING COUNT(*) > 1
SELECT RecNumber FROM [a table]
GROUP BY RecNumber HAVING COUNT(*) > 1;
Or perhaps
SELECT RecNumber FROM (SELECT RecNumber, Employee
FROM [a table]
GROUP BY RecNumber, Employee HAVING COUNT(*) > 1
) AS x;
You can write something like that, i think it's working :
use *DATABASE_NAME*
go
select *Your_Field*, Nbre=count(*Your_Field*) from *TABLE_NAME*
group by *Your_Field*
having count(*Your_Field*)>1
order by 2 desc
Enjoy
Related
I have a table like this:
SchoolName|SchoolID
--------------------
School A |x
School B |x
School C |y
School D |z
School D |z
There are actually more columns, including a unique ID. The above is just for brevity.
What I need to do is select all columns, where the SchoolID occurs more than once, but only if the corresponding value in the SchoolName column doesn't.
So I'd like get all columns for the rows with School A and School B, but not the School C and School D rows.
The db is MS SQL 2008 R2.
The original question was terribly confused, hopefully I've managed to rephrase it correctly now!
You can use window version of COUNT to count SchoolID and SchoolName occurences:
SELECT *
FROM (
SELECT *,
COUNT(*) OVER (PARTITION BY SchoolID) AS cntSchoolID,
COUNT(*) OVER (PARTITION BY SchoolID, SchoolName) AS cntSchoolName
FROM mytable ) t
WHERE t.cntSchoolID > 1 AND t.cntSchoolName = 1
SQL Fiddle Demo
So do you want something like this?
SELECT A.*
FROM yourTable A
CROSS APPLY (
SELECT TOP 1 NULL col
FROM yourTable
WHERE A.SchoolID = SchoolID
AND A.SchoolName = SchoolName
GROUP BY SchoolID,SchoolName
HAVING COUNT(*) > 1
) CA
I would build a list of all the schoolnames that repeat, and all the schoolids that repeat, and then pull all of the rows where the schoolname or the schoolid match:
select
*
from
mytable
where
(schoolname in
(
select schoolname
from
mytable
group by schoolname
having count (*) > 1)
OR
schoolid in
(
select
schoolid
from
mytable
group by schoolid
having count (*) > 1)
)
Ugly, but I think it gives you your desired result.
SQL Fiddle
I have a table PRODUCT that is basically set up so there is an PRODUCTID, PRODUCTNAME... it looks sort of like this
PRODUCTID PRODUCTNAME
100 PNAME1
101 PNAME2
102 PNAME3
Now I have to insert a record into new table PRODUCTMAPPING for each row in the PRODUCT.
so my new table PRODUCTMAPPING should look like this
PRODUCTMAPPINGID PRODUCTID
1 100
1 101
1 102
2 100
2 101
2 102
3 100
and so on ....
I tried doing while but I need it using Join.
Can we acheive this using joins ?
Thanks in advance.
One way;
select
row_number() over(partition by a.PRODUCTID order by a.PRODUCTID) PRODUCTMAPPINGID,
a.PRODUCTID
from PRODUCT a, PRODUCT b
Using LOOP
The following example specifies that the JOIN operation in the query is performed by a LOOP join.
Select sp.Name,spqh.quota
FROM Sales.SalesPersonQuotaHistory AS spqh
INNER LOOP JOIN Sales.SalesPerson AS sp
ON spqh.SalesPersonID = sp.SalesPersonID
WHERE sp.SalesYTD > 2500000.00;
GO
Refer this MSDN link
INSERT
INTO dbo.PRODUCTMAPPING
(
PRODUCTMAPPINGID
,PRODUCTID
)
SELECT pmv.PRODUCTMAPPINGID
,p.PRODUCTID
FROM dbo.Product p
CROSS JOIN
(
SELECT pm.ProductMappingID
FROM dbo.ProductMappingValues pmv -- WHERE DO THESE COME FROM?
) pmv
I have two tables, one for Employees and the second for records. I want to get total entries for each employee and order the results by max total entry like:
Daniel 3
David 1
tblEmployee:
EID Name
1 Daniel
2 David
tblEntry:
ID Column1 EID
1 XX 1
2 XX 1
3 XX 2
4 XX 1
try this:
select emp.EID,emp.Name,COUNT(etr.EID)
as total_entries from Employee emp join Entry etr
on emp.EID=etr.EID
group by emp.EID,emp.Name
You must use group by
select count(*) from tblEmployee ee, tblEntry e where ee.eid = e.eid group by ee.name
There are several variations on this, and you don't say what version of SQL Server you're using, but I like doing it this way:
;
using A
as (
select EID
, RecordCount = COUNT(*)
from tblEntry
group by
EID
)
select a.EID
, e.Name
, a.RecordCount
from A
join tblEmployee e
on A.EID = e.EID
order by
RecordCount desc
I like doing it this way rather than joining and then summarizing because you only have to group on the minimum number of fields. EID in tblEntry is likely to already have an index on it, while Name in tblEmployee may not.
I'm stuck with a query which should be pretty simple but, for reasons unknown, my brain is not playing ball here ...
Table:
id(int) | strategy (varchar) | value (whatever)
1 "ABC" whatevs
2 "ABC" yeah
3 "DEF" hello
4 "DEF" kitty
5 "QQQ" hurrr
The query should select ALL rows grouped on strategy but only one row per strategy - the one with the higest id.
In the case above, it should return rows with id 2, 4 and 5
SELECT id, strategy , value
FROM (
SELECT id, strategy , value
,ROW_NUMBER() OVER (PARTITION BY strategy ORDER BY ID DESC) rn
FROM Table_Name
) Sub
WHERE rn = 1
Working SQL FIDDLE
You can use window function to get the solution you want. Fiddle here
with cte as
(
select
rank()over(partition by strategy order by id desc) as rnk,
id, strategy, value from myT
)
select id, strategy, value from
cte where rnk = 1;
Try this:
SELECT T2.id,T1.strategy,T1.value
FROM TableName T1
INNER JOIN
(SELECT MAX(id) as id,strategy
FROM TableName
GROUP BY strategy) T2
ON T1.id=T2.id
Result:
ID STRATEGY VALUE
2 ABC yeah
4 DEF kitty
5 QQQ hurrr
See result in SQL Fiddle.
SELECT id, strategy , value
FROM (
SELECT id, strategy , value
,MAX(id) OVER (PARTITION BY strategy) MaxId
FROM YourTable
) Sub
WHERE id=MaxId
You may try this one as well:
SELECT id, strategy, value FROM TableName WHERE id IN (
SELECT MAX(id) FROM TableName GROUP BY strategy
)
Bit depends on your data, you might get results faster with it as it does not do sorting, but by the other hand it uses IN, which can slow you down if there is many 'strategies'
I have table named Schedule which has fields named TeacherName and ClassTakenDate.The values in the table are as shown below:
TeacherName ClassTakenDate
Anish 2011-10-01
Anish 2011-10-01
John 2011-10-01
John 2011-10-02
I want result like this :
TeacherName NoOfDays
Anish 1
John 2
how we can do this?
SELECT TeacherName,
COUNT(DISTINCT ClassTakenDate) AS NoOfDays
FROM Schedule
GROUP BY TeacherName
SELECT TeacherName, ClassTakenDate, COUNT( * ) AS NoOfDays
FROM (
SELECT DISTINCT TeacherName, ClassTakenDate
FROM Schedule
) AS foo
GROUP BY foo.ClassTakenDate