Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-05-11 : 06:26:47
|
Hi, I have the following SQL select statement which gives results as; SELECT TOP (100) PERCENT gprdsql.TblPracDetails.prac_no, gprdsql.TblPracDetails.sub_media, MAX(gprdsql.TblCollections.col_date) AS MaxColDate, MAX(gprdsql.TblCollections.audit_end+1) AS audit_startFROM gprdsql.TblCollections INNER JOIN gprdsql.TblPracDetails ON gprdsql.TblCollections.prac_no = gprdsql.TblPracDetails.prac_no INNER JOIN dbo.QryMaxColDate ON gprdsql.TblCollections.prac_no = dbo.QryMaxColDate.prac_no AND gprdsql.TblCollections.col_date = dbo.QryMaxColDate.col_dateWHERE (gprdsql.TblPracDetails.prac_status = 'Active') AND (gprdsql.TblCollections.stage = 'Processed')GROUP BY gprdsql.TblPracDetails.prac_no, gprdsql.TblPracDetails.sub_media, gprdsql.TblCollections.media_type, gprdsql.TblPracDetails.prac_statusORDER BY gprdsql.TblPracDetails.prac_no prac_no submedia MaxColdate audit_start1 Email 01/03/2012 562 Email 17/04/2012 893 Email 21/04/2012 733 Email 21/04/2012 704 Email 05/05/2012 8282The problem with prac_no 3 - it has 2 MaxColdate with different audit_end values. Therefore, I want to take the maximum out of the two and ignore the other one.. Therefore, in this case the result should be; prac_no submedia MaxColdate audit_start1 Email 01/03/2012 562 Email 17/04/2012 893 Email 21/04/2012 734 Email 05/05/2012 8282Thanks |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2012-05-11 : 06:44:57
|
| [code] Try This.SELECT T1.prac_no, T1.submedia T1.MaxColdate ,MAX( T1.audit_start) audit_start FROM (SELECT TOP (100) PERCENT gprdsql.TblPracDetails.prac_no, gprdsql.TblPracDetails.sub_media, MAX(gprdsql.TblCollections.col_date) AS MaxColDate, MAX(gprdsql.TblCollections.audit_end+1) AS audit_startFROM gprdsql.TblCollections INNER JOIN gprdsql.TblPracDetails ON gprdsql.TblCollections.prac_no = gprdsql.TblPracDetails.prac_no INNER JOIN dbo.QryMaxColDate ON gprdsql.TblCollections.prac_no = dbo.QryMaxColDate.prac_no AND gprdsql.TblCollections.col_date = dbo.QryMaxColDate.col_dateWHERE (gprdsql.TblPracDetails.prac_status = 'Active') AND (gprdsql.TblCollections.stage = 'Processed')GROUP BY gprdsql.TblPracDetails.prac_no, gprdsql.TblPracDetails.sub_media, gprdsql.TblCollections.media_type, gprdsql.TblPracDetails.prac_statusORDER BY gprdsql.TblPracDetails.prac_no) T1 GROUP BY T1.prac_no, T1.submedia T1.MaxColdate[/code] |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-05-11 : 06:49:39
|
| Cant I update the query to perform the task - few applications are dependent on this query and dont want to create another table then run another query. Is there any chance I can do to have the desired within the same query?Thank you |
 |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2012-05-11 : 07:02:24
|
| [code]SELECT TOP (100) PERCENT gprdsql.TblPracDetails.prac_no, gprdsql.TblPracDetails.sub_media, MAX(gprdsql.TblCollections.col_date) AS MaxColDate, MAX(gprdsql.TblCollections.audit_end+1) AS audit_startFROM gprdsql.TblCollections INNER JOIN gprdsql.TblPracDetails ON gprdsql.TblCollections.prac_no = gprdsql.TblPracDetails.prac_no INNER JOIN dbo.QryMaxColDate ON gprdsql.TblCollections.prac_no = dbo.QryMaxColDate.prac_no AND gprdsql.TblCollections.col_date = dbo.QryMaxColDate.col_dateWHERE (gprdsql.TblPracDetails.prac_status = 'Active') AND (gprdsql.TblCollections.stage = 'Processed')GROUP BY gprdsql.TblPracDetails.prac_no, gprdsql.TblPracDetails.sub_media,MAX(gprdsql.TblCollections.col_date) ORDER BY gprdsql.TblPracDetails.prac_noIf you remove below columns from your query from group by clause and add column which i have added in your query.This is just hit and trial . I hope this shoul work for you gprdsql.TblCollections.media_type, gprdsql.TblPracDetails.prac_status[/code] |
 |
|
|
markandan26
Starting Member
3 Posts |
Posted - 2012-07-23 : 00:57:39
|
| This is my query.update two column in same table and update duplicate values in new column.I'm having query for this update but it will work for single column. i want two columnplease help me........UPDATE [Table] SET [Table].DUP = "Duplicate"WHERE ((([Table].column1) In (SELECT [column1] FROM [Table] As Tmp WHERE TMP.ID <= [Table].ID GROUP BY [column1] HAVING Count(*)>1 ))); Thank U..................Markandan M |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-23 : 10:32:49
|
quote: Originally posted by markandan26 This is my query.update two column in same table and update duplicate values in new column.I'm having query for this update but it will work for single column. i want two columnplease help me........UPDATE [Table] SET [Table].DUP = "Duplicate"WHERE ((([Table].column1) In (SELECT [column1] FROM [Table] As Tmp WHERE TMP.ID <= [Table].ID GROUP BY [column1] HAVING Count(*)>1 ))); Thank U..................Markandan M
use a join thenUPDATE t SET t.DUP = 'Duplicate'FROM [Table] tINNER JOIN(SELECT [column1],[column2] FROM [Table] GROUP BY [column1],[column2] HAVING Count(*)>1 )t1ON t1.column1 = t. column1 AND t1.column2 = t.column2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|