Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Editable view without duplicates

 
   Database Help (Home) -> General Discussions RSS
Next:  Mathematical Calculations  
Author Message
Bernard West

External


Since: Dec 04, 2008
Posts: 2



(Msg. 1) Posted: Thu Dec 04, 2008 5:45 am
Post subject: Editable view without duplicates
Archived from groups: comp>databases>ms-sqlserver (more info?)

I'm having a mental block about how to make a view displaying the
records I need that's editable and not read-only. Given the following
basic table structure:

CREATE TABLE [dbo].[Person](
[PersonID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](255) NULL,
[Surname] [nvarchar](255) NULL)

CREATE TABLE [dbo].[Contract](
[ContractID] [int] IDENTITY(1,1) NOT NULL,
[PersonID] [int] NULL,
[PostID] [int] NULL)

CREATE TABLE [dbo].[Post](
[PostID] [int] IDENTITY(1,1) NOT NULL,
[PostTitle] [nvarchar](50) NULL,
[Department] [nvarchar](2) NULL)

I want to select the records from [Person] who have a contract
corresponding to a [Post] in a particular department. If I do the
basic

SELECT PersonID, FirstName, Surname
FROM Person INNER JOIN
Contract ON Person.PersonID = Contract.PersonID
INNER JOIN
Post ON Contract.PostID = Post.PostID
WHERE (Post.Department = N'Department')

I obviously get duplicates where there are multiple contracts for one
person. What I need is something that does the same job as

SELECT PersonID, FirstName, Surname
FROM Person INNER JOIN
(SELECT DISTINCT Contract.PersonID
FROM Contract INNER JOIN
Post ON
Contract.PostID = Post.PostID
WHERE (Post.Department =
'Department')) AS DepartmentFilter ON Person.PersonID =
DepartmentFilter.PersonID

but which lets me edit the information returned - this version is read-
only.

 >> Stay informed about: Editable view without duplicates 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 2) Posted: Thu Dec 04, 2008 6:25 pm
Post subject: Re: Editable view without duplicates [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Bernard West ( ) writes:
> I want to select the records from [Person] who have a contract
> corresponding to a [Post] in a particular department. If I do the
> basic
>
> SELECT PersonID, FirstName, Surname
> FROM Person INNER JOIN
> Contract ON Person.PersonID = Contract.PersonID
> INNER JOIN
> Post ON Contract.PostID = Post.PostID
> WHERE (Post.Department = N'Department')
>
> I obviously get duplicates where there are multiple contracts for one
> person. What I need is something that does the same job as
>
> SELECT PersonID, FirstName, Surname
> FROM Person INNER JOIN
> (SELECT DISTINCT Contract.PersonID
> FROM Contract INNER JOIN
> Post ON
> Contract.PostID = Post.PostID
> WHERE (Post.Department =
> 'Department')) AS DepartmentFilter ON Person.PersonID =
> DepartmentFilter.PersonID
>
> but which lets me edit the information returned - this version is read-
> only.

Rather than joining, use EXISTS:

SELECT P.PersonID, P.FirstName, P.Surname
FROM Person P
WHERE EXISTS (SELECT *
FROM Contracts C
JOIN Post ON C.PostID = Post.PostID
WHERE Post.Department = 'Department'
AND P.PersonID = C.PersonID)

--
Erland Sommarskog, SQL Server MVP, esquel DeleteThis @sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 >> Stay informed about: Editable view without duplicates 
Back to top
Login to vote
Bernard West

External


Since: Dec 04, 2008
Posts: 2



(Msg. 3) Posted: Fri Dec 05, 2008 1:36 am
Post subject: Re: Editable view without duplicates [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 4 Dec, 22:30, Erland Sommarskog wrote:
> Bernard West ( ) writes:
> > I want to select the records from [Person] who have a contract
> > corresponding to a [Post] in a particular department.  If I do the
> > basic
>
> > SELECT     PersonID, FirstName, Surname
> > FROM         Person INNER JOIN
> >                       Contract ON Person.PersonID = Contract.PersonID
> > INNER JOIN
> >                       Post ON Contract.PostID = Post.PostID
> > WHERE     (Post.Department = N'Department')
>
> > I obviously get duplicates where there are multiple contracts for one
> > person.  What I need is something that does the same job as
>
> > SELECT     PersonID, FirstName, Surname
> > FROM         Person INNER JOIN
> >                           (SELECT    DISTINCT Contract.PersonID
> >                             FROM          Contract INNER JOIN
> >                                                    Post ON
> > Contract.PostID = Post.PostID
> >                             WHERE      (Post.Department =
> > 'Department')) AS DepartmentFilter ON Person.PersonID =
> > DepartmentFilter.PersonID
>
> > but which lets me edit the information returned - this version is read-
> > only.
>
> Rather than joining, use EXISTS:
>
>     SELECT P.PersonID, P.FirstName, P.Surname
>     FROM   Person P
>     WHERE  EXISTS (SELECT *
>                    FROM   Contracts C
>                    JOIN   Post ON C.PostID = Post..PostID
>                    WHERE  Post.Department = 'Department'
>                      AND  P.PersonID = C.PersonID)
>
> --
> Erland Sommarskog, SQL Server MVP, esq....RemoveThis@sommarskog.se
>
> Links for SQL Server Books Online:
> SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
> SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
> SQL 2000:http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx


That's the one! Fabulous - thanks.
 >> Stay informed about: Editable view without duplicates 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Please Help - Can't check for duplicates - I'm driving myself crazy with a problem in trying to translate a query written for Access to that for SQL server. I would think that I would use a trigger, but am not sure how to set it up. We have a database that manages bookings in four banquet..

Help setting up a view - How do I set up a sql statement to use as a view? I want to use two tables but they are both from different databases. From the Offices database I want to use a table called Office Code and only the Name column The other database is called Library and...

permissions to see design view - Is there a way to allow users to see the design view of a table without having dbo permissions? Thanks

Anyway to declare and use variables in a View - I have some SQL code I wrote that returns about 15,000 rows (using a group by statement, raw data is around 1mill rows). In this code I have a few variables that I declare and set. These variables for the most part are just calling functions that return...

What's wrong with this View? Duplicate columns? - When I add this code in a view and try to save . . . SELECT TOP 610 * FROM dbo.Master INNER JOIN dbo.TypeByCase ON dbo.TypeByCase.CaseNum = dbo.Master.CaseNum It gives the error: ODBC error: [Microsoft][ODBC SQL Server Driver]Column names in each view....
   Database Help (Home) -> General Discussions All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]