Solved

Help with query

Posted on 2009-05-12
4
455 Views
Last Modified: 2012-05-06
I got this query but it is returning WI's which had the 'Ready for Deployment' status currently or previously. I just need the records with WI in that state currently.

I dont have more points else would have used them.
SELECT DISTINCT dbo.[File].[File], MAX(dbo.[File].__LastUpdatedTime) AS LastUpdated, dbo.[Work Item].System_Id, dbo.Person.Person

FROM         dbo.[File] INNER JOIN

                      dbo.[Code Churn] ON dbo.[File].__ID = dbo.[Code Churn].Filename INNER JOIN

                      dbo.Changeset ON dbo.[Code Churn].Changeset = dbo.Changeset.__ID INNER JOIN

                      dbo.[Work Item Changeset] ON dbo.Changeset.__ID = dbo.[Work Item Changeset].Changeset INNER JOIN

                      dbo.[Work Item] ON dbo.[Work Item Changeset].[Work Item] = dbo.[Work Item].__ID INNER JOIN

                      dbo.Person ON dbo.[Code Churn].[Checked In By] = dbo.Person.__ID

WHERE     (dbo.[File].[File Extension] = N'.sql')

GROUP BY dbo.[File].[File], dbo.[Work Item].System_State, dbo.[Work Item].System_Id, dbo.Person.Person

HAVING      (dbo.[Work Item].System_State = N'Ready for Deployment')

ORDER BY dbo.[Work Item].System_Id

Open in new window

0
Comment
Question by:shahprabal
  • 3
4 Comments
 
LVL 9

Expert Comment

by:Hwkranger
ID: 24366015
Your SQL is sort of a mess, it's hard to read with the naming and all.... Try this, I put the max of the last update equal to the current updated so that shoudl be all current work items that have that status...


SELECT DISTINCT dbo.[File].[File], MAX(dbo.[File].__LastUpdatedTime) AS LastUpdated, dbo.[Work Item].System_Id, dbo.Person.Person

FROM         dbo.[File] INNER JOIN

                      dbo.[Code Churn] ON dbo.[File].__ID = dbo.[Code Churn].Filename INNER JOIN

                      dbo.Changeset ON dbo.[Code Churn].Changeset = dbo.Changeset.__ID INNER JOIN

                      dbo.[Work Item Changeset] ON dbo.Changeset.__ID = dbo.[Work Item Changeset].Changeset INNER JOIN

                      dbo.[Work Item] ON dbo.[Work Item Changeset].[Work Item] = dbo.[Work Item].__ID INNER JOIN

                      dbo.Person ON dbo.[Code Churn].[Checked In By] = dbo.Person.__ID

WHERE     (dbo.[File].[File Extension] = N'.sql')

GROUP BY dbo.[File].[File], dbo.[Work Item].System_State, dbo.[Work Item].System_Id, dbo.Person.Person

HAVING      (dbo.[Work Item].System_State = N'Ready for Deployment')

						AND

	         MAX(dbo.[File].__LastUpdatedTime) = dbo.[File].__LastUpdatedTime

ORDER BY dbo.[Work Item].System_Id

Open in new window

0
 
LVL 14

Author Comment

by:shahprabal
ID: 24366160
Thanks for trying but this is what I get :
Msg 8121, Level 16, State 1, Line 1
Column 'dbo.File.__LastUpdatedTime' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.

I realise that without showing the schema it will be hard to fix this but I have attached the query that lists the important fields in the Work Item table. Also the second query returns one record per WI. I just need to incorporate that into the main query.
 Result of the 1st query:

__ID           Work Item           System_Id           System_Rev
 ----------------------------------------------------------------------------
1637                     366-1                     366                     1
1638                     366-2                     366                     2
1953                     366-3                     366                     3
2077                     366-4                     366                     4
2079                     366-5                     366                     5
2080                     366-6                     366                     6
2083                     366-7                     366                     7
2086                     366-8                     366                     8
2087                     366-9                     366                     9
(9 row(s) affected)
 
 
Result of the 2nd query:

                system_id
----------- -----------
2087          366
 
(1 row(s) affected)

select __ID, [Work Item], System_Id, System_Rev from [work item]

where system_id=366
 

select distinct max(__id), system_id  from [work item]

group by system_id

having system_id=366

Open in new window

0
 
LVL 14

Author Comment

by:shahprabal
ID: 24367272
I am sure there is a better way of getting the same data but the below query works... if someone can suggest a better way I will award the points to them.
SELECT     main.[File], main.LastUpdated, main.Person, sub.System_Id AS [Work Item]

FROM         (SELECT DISTINCT dbo.[File].[File], MAX(dbo.[File].__LastUpdatedTime) AS LastUpdated, dbo.Person.Person, [Work Item Changeset].[Work Item]

                       FROM          dbo.[File] INNER JOIN

                                              dbo.[Code Churn] ON dbo.[File].__ID = dbo.[Code Churn].Filename INNER JOIN

                                              dbo.Changeset ON dbo.[Code Churn].Changeset = dbo.Changeset.__ID INNER JOIN

                                              dbo.[Work Item Changeset] ON dbo.Changeset.__ID = dbo.[Work Item Changeset].Changeset INNER JOIN

                                              dbo.Person ON dbo.[Code Churn].[Checked In By] = dbo.Person.__ID

                       WHERE      (dbo.[File].[File Extension] = N'.sql')

                       GROUP BY dbo.[File].[File], dbo.Person.Person, [Work Item Changeset].[Work Item]) AS main INNER JOIN

                          (SELECT     one.ID, one.System_Id

                            FROM          (SELECT DISTINCT MAX(__ID) AS ID, System_Id

                                                    FROM          dbo.[Work Item]

                                                    GROUP BY System_Id) AS one INNER JOIN

                                                       (SELECT     __ID AS ID, System_Id

                                                         FROM          dbo.[Work Item] AS [Work Item_1]

                                                         WHERE      (System_State = N'Ready for Deployment')) AS two ON one.ID = two.ID) AS sub ON main.[Work Item] = sub.ID

Open in new window

0
 
LVL 14

Accepted Solution

by:
shahprabal earned 0 total points
ID: 24451334
Got it
SELECT     main.[File], main.LastUpdated, main.Person, sub.System_Id AS [Work Item]

FROM         (SELECT DISTINCT dbo.[File].[File], MAX(dbo.[File].__LastUpdatedTime) AS LastUpdated, dbo.Person.Person, [Work Item Changeset].[Work Item]

                       FROM          dbo.[File] INNER JOIN

                                              dbo.[Code Churn] ON dbo.[File].__ID = dbo.[Code Churn].Filename INNER JOIN

                                              dbo.Changeset ON dbo.[Code Churn].Changeset = dbo.Changeset.__ID INNER JOIN

                                              dbo.[Work Item Changeset] ON dbo.Changeset.__ID = dbo.[Work Item Changeset].Changeset INNER JOIN

                                              dbo.Person ON dbo.[Code Churn].[Checked In By] = dbo.Person.__ID

                       WHERE      (dbo.[File].[File Extension] = N'.sql')

                       GROUP BY dbo.[File].[File], dbo.Person.Person, [Work Item Changeset].[Work Item]) AS main INNER JOIN

                          (SELECT     one.ID, one.System_Id

                            FROM          (SELECT DISTINCT MAX(__ID) AS ID, System_Id

                                                    FROM          dbo.[Work Item]

                                                    GROUP BY System_Id) AS one INNER JOIN

                                                       (SELECT     __ID AS ID, System_Id

                                                         FROM          dbo.[Work Item] AS [Work Item_1]

                                                         WHERE      (System_State = N'Ready for Deployment')) AS two ON one.ID = two.ID) AS sub ON main.[Work Item] = sub.ID

Open in new window

0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now