Solved

Help with query

Posted on 2009-05-12
4
463 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server Agent "Access Denied" Error 3 36
VM SQL server license. 1 67
TDE for SQL Web Edition 1 42
SSIS Error on Bat File execute 2 15
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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

752 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