Monday, February 20, 2012

Newb question

I am attempting to retrieve specific task related data from a Project server
environment for input to an Excel 2003 spreadsheet and have created the
following SQL view using:
SELECT dbo.MSP_PROJECTS.PROJ_NAME, dbo.MSP_TASKS.TASK_NAME,
dbo.MSP_TASKS.TASK_IS_MILESTONE
FROM dbo.MSP_PROJECTS CROSS JOIN
dbo.MSP_TASKS
WHERE (dbo.MSP_PROJECTS.PROJ_ID = 92) AND
(dbo.MSP_TASKS.TASK_IS_MILESTONE = 1)
My logic here is that this query will pull back all tasks that are
milestones from 'project 92' only. However, this query pulls back all
milestones from all projects... I have no idea why, could someone please
shed some light on this newb.
Many thanks,
Nock (SQL Newb, Australia)I think the CROSS JOIN might be a clue. I would just use a JOIN
"Nock" wrote:

> I am attempting to retrieve specific task related data from a Project serv
er
> environment for input to an Excel 2003 spreadsheet and have created the
> following SQL view using:
> SELECT dbo.MSP_PROJECTS.PROJ_NAME, dbo.MSP_TASKS.TASK_NAME,
> dbo.MSP_TASKS.TASK_IS_MILESTONE
> FROM dbo.MSP_PROJECTS CROSS JOIN
> dbo.MSP_TASKS
> WHERE (dbo.MSP_PROJECTS.PROJ_ID = 92) AND
> (dbo.MSP_TASKS.TASK_IS_MILESTONE = 1)
> My logic here is that this query will pull back all tasks that are
> milestones from 'project 92' only. However, this query pulls back all
> milestones from all projects... I have no idea why, could someone please
> shed some light on this newb.
> Many thanks,
> Nock (SQL Newb, Australia)|||it is because of the CROSS JOIN used to connect the MSP_PROJECTS and
MSP_TASKS tables. A cross join between two tables produces what is known as
a
Cartesian product, which is a table that contains all of the possible
combinations between the rows between the input tables. In other words, a
cross join between two tables X and Y with x and y rows respectively will
contain 1 row for each y rows for each row in X, for a total of x times y
rows.
What you probably want is something like this
SELECT dbo.MSP_PROJECTS.PROJ_NAME, dbo.MSP_TASKS.TASK_NAME,
dbo.MSP_TASKS.TASK_IS_MILESTONE
FROM dbo.MSP_PROJECTS INNER JOIN
dbo.MSP_TASKS ON dbo.MSP_PROJECTS.PROJ_ID =
dbo.MSP_TASKS.PROJ_ID
WHERE (dbo.MSP_PROJECTS.PROJ_ID = 92) AND
(dbo.MSP_TASKS.TASK_IS_MILESTONE = 1)
"Nock" wrote:

> I am attempting to retrieve specific task related data from a Project serv
er
> environment for input to an Excel 2003 spreadsheet and have created the
> following SQL view using:
> SELECT dbo.MSP_PROJECTS.PROJ_NAME, dbo.MSP_TASKS.TASK_NAME,
> dbo.MSP_TASKS.TASK_IS_MILESTONE
> FROM dbo.MSP_PROJECTS CROSS JOIN
> dbo.MSP_TASKS
> WHERE (dbo.MSP_PROJECTS.PROJ_ID = 92) AND
> (dbo.MSP_TASKS.TASK_IS_MILESTONE = 1)
> My logic here is that this query will pull back all tasks that are
> milestones from 'project 92' only. However, this query pulls back all
> milestones from all projects... I have no idea why, could someone please
> shed some light on this newb.
> Many thanks,
> Nock (SQL Newb, Australia)|||Nock,
What is the common column between the tables? n other words, does MSP_TASKS
have a foreign Key column to the Primary Key column on MSP_PROJECTS, or
vice-versa.
Whichever is the key column you should then use a standard JOIN statement.
If you want just the Tasks that are associated with Project 92, then you
should use the following...
SELECT proj.PROJ_NAME,
task.TASK_NAME,
task.TASK_IS_MILESTONE
FROM dbo.MSP_PROJECTS proj LEFT JOIN
dbo.MSP_TASKS task ON proj.{PK} = task.{FK}
WHERE (proj.PROJ_ID = 92)
AND (task.TASK_IS_MILESTONE = 1)
The LEFT Join will ensure you get all Project Data back together with any
Task data that is relevent, or NULL values if none present. If you use INNER
join then there will need to be at least one reacord in each table.
Enjoy,
"Nock" wrote:

> I am attempting to retrieve specific task related data from a Project serv
er
> environment for input to an Excel 2003 spreadsheet and have created the
> following SQL view using:
> SELECT dbo.MSP_PROJECTS.PROJ_NAME, dbo.MSP_TASKS.TASK_NAME,
> dbo.MSP_TASKS.TASK_IS_MILESTONE
> FROM dbo.MSP_PROJECTS CROSS JOIN
> dbo.MSP_TASKS
> WHERE (dbo.MSP_PROJECTS.PROJ_ID = 92) AND
> (dbo.MSP_TASKS.TASK_IS_MILESTONE = 1)
> My logic here is that this query will pull back all tasks that are
> milestones from 'project 92' only. However, this query pulls back all
> milestones from all projects... I have no idea why, could someone please
> shed some light on this newb.
> Many thanks,
> Nock (SQL Newb, Australia)|||Thanks Mark et al, much appreciated and great explanation.
I'm in one of those situations where I've been asked to become a 'SQL
person' in a day...
Loving life :)
Cheers,
Nock
"Mark Williams" wrote:
> it is because of the CROSS JOIN used to connect the MSP_PROJECTS and
> MSP_TASKS tables. A cross join between two tables produces what is known a
s a
> Cartesian product, which is a table that contains all of the possible
> combinations between the rows between the input tables. In other words, a
> cross join between two tables X and Y with x and y rows respectively will
> contain 1 row for each y rows for each row in X, for a total of x times y
> rows.
> What you probably want is something like this
> SELECT dbo.MSP_PROJECTS.PROJ_NAME, dbo.MSP_TASKS.TASK_NAME,
> dbo.MSP_TASKS.TASK_IS_MILESTONE
> FROM dbo.MSP_PROJECTS INNER JOIN
> dbo.MSP_TASKS ON dbo.MSP_PROJECTS.PROJ_ID =
> dbo.MSP_TASKS.PROJ_ID
> WHERE (dbo.MSP_PROJECTS.PROJ_ID = 92) AND
> (dbo.MSP_TASKS.TASK_IS_MILESTONE = 1)
> --
> "Nock" wrote:
>|||Try these sites for a general SQL overview...
http://www.w3schools.com/sql/sql_intro.asp
http://sqlzoo.net/
"Nock" <Nock@.discussions.microsoft.com> wrote in message
news:05979A9F-A3BC-401B-867F-CDC7114D2CE1@.microsoft.com...
> Thanks Mark et al, much appreciated and great explanation.
> I'm in one of those situations where I've been asked to become a 'SQL
> person' in a day...
> Loving life :)
> Cheers,
> Nock
> "Mark Williams" wrote:
>
as a
a
will
y
server
the
please

No comments:

Post a Comment