| Author |
Topic |
|
Vithi
Starting Member
19 Posts |
Posted - 2008-12-18 : 08:58:54
|
| Table_1DOCID ID12345 9999912345 9999812345 9999712345 9999623456 9999523456 9999423456 99993 34567 99992 34567 9999145678 9999056789 8888956789 8888456789 8888356789 8888256789 88881Table_2ID Value Date (yyyy/m/d)99999 x 2008/01/0599998 x 2008/01/0999997 y 2008/01/1099996 t 2008/05/0399995 y 2008/05/0699994 m 2006/04/0699993 d 2006/04/2399992 x 2006/11/2299991 m 2006/11/2399990 m 2007/05/0288889 m 2008/09/2588884 y 2008/09/2988883 y 2008/08/0188882 x 2008/01/0688881 m 2008/09/27Should look like this:DOCID Value X Latest Date Value Y Latest Date Value M Latest Date12345 2008/01/09 2008/01/10 -23456 - 2008/05/06 2006/04/0634567 2006/11/22 - 2006/11/2345678 - - 2007/05/0256789 2008/01/06 2008/09/29 2008/09/27 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-18 : 09:20:36
|
Subreports? All you need is a crosstab/pivot report.SELECT t1.DOCID, MAX(CASE WHEN t2.Value = 'X' THEN t2.Date ELSE NULL END) AS [Val X Latest Date], MAX(CASE WHEN t2.Value = 'Y' THEN t2.Date ELSE NULL END) AS [Val Y Latest Date], MAX(CASE WHEN t2.Value = 'M' THEN t2.Date ELSE NULL END) AS [Val M Latest Date]FROM Table_1 AS t1INNER JOIN Table_2 AS t2 ON t2.ID = t1.IDGROUP BY t1.DOCIDORDER BY t1.DOCID E 12°55'05.63"N 56°04'39.26" |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2008-12-18 : 09:37:19
|
| moved from script library___________________________________________________________________________Causing trouble since 1980Blog: http://weblogs.sqlteam.com/mladenpSpeed up SSMS development: www.ssmstoolspack.com <- version 1.1 out! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-18 : 09:53:35
|
[code]SELECT t1.DOCID, MAX(CASE WHEN t2.Value = 'X' THEN t2.Date ELSE NULL END) AS [Val X Latest Date], MAX(CASE WHEN t2.Value = 'Y' THEN t2.Date ELSE NULL END) AS [Val Y Latest Date], MAX(CASE WHEN t2.Value = 'M' THEN t2.Date ELSE NULL END) AS [Val M Latest Date]FROM Table_1 AS t1INNER JOIN Table_2 AS t2 ON t2.ID = t1.IDWHERE t2.Value IN ('X', 'Y', 'M')GROUP BY t1.DOCIDORDER BY t1.DOCID[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
Vithi
Starting Member
19 Posts |
Posted - 2008-12-18 : 09:57:31
|
| LATEST MAX FUNCTIONALITY STILL DOES NOT WORK :( |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-18 : 09:59:57
|
[code]SELECT t1.DOCID, MAX(CASE WHEN t2.Value = 'X' THEN t2.Date ELSE NULL END) AS [Val X Latest Date], MAX(CASE WHEN t2.Value = 'Y' THEN t2.Date ELSE NULL END) AS [Val Y Latest Date], MAX(CASE WHEN t2.Value = 'Z' THEN t2.Date ELSE NULL END) AS [Val Z Latest Date]FROM Table_1 AS t1INNER JOIN Table_2 AS t2 ON t2.ID = t1.IDWHERE t2.Value IN ('X', 'Y', 'Z')GROUP BY t1.DOCIDORDER BY t1.DOCID[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-18 : 10:27:02
|
The your dates are NOT dates. I believe you have them stored as varchar, am I right?SELECT t1.DOCID, MAX(CASE WHEN t2.Value = 'X' THEN CAST(t2.Date AS DATETIME) ELSE NULL END) AS [Val X Latest Date], MAX(CASE WHEN t2.Value = 'Y' THEN CAST(t2.Date AS DATETIME) ELSE NULL END) AS [Val Y Latest Date], MAX(CASE WHEN t2.Value = 'Z' THEN CAST(t2.Date AS DATETIME) ELSE NULL END) AS [Val Z Latest Date]FROM Table_1 AS t1INNER JOIN Table_2 AS t2 ON t2.ID = t1.IDWHERE t2.Value IN ('X', 'Y', 'Z')GROUP BY t1.DOCIDORDER BY t1.DOCID E 12°55'05.63"N 56°04'39.26" |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-12-18 : 10:27:09
|
| Peso's query should work for you. Do you need M latest date as well? Then you need to add to crosstab result and put like this in Where clause:WHERE t2.Value IN ('X', 'Y', 'Z','M') |
 |
|
|
Vithi
Starting Member
19 Posts |
Posted - 2008-12-18 : 10:32:51
|
| no. just x, y and m. it is stored as 'date'.I tried it in diff table and it seems to work so far. i am guessing that if users entered the data themselves, it does not work. it only works for the system generated data. I have to resolve that problem. IT WORKS!!!!!! :)Thank you guys so much. I've been at this for a while now. I can do simple sql. I should start doing some exercise on this.THank you thank you thank you.You guys have been a GREAT HELP :) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-18 : 11:04:13
|
quote: Originally posted by Vithi no. just x, y and m. it is stored as 'date'.
Which RDBMS are you using? Date datatype is available in SQL Server 2008 and on.And now you want x, y and m only, again as in original post?Before you wantedquote: Originally posted by Vithi MY MISTAKE. BUT STILL THE LATEST X OR Y OR Z DOES NOT COME UP WHEN IT HAS MORE THAN 1 VALUE. I HAVE 12/16/2008 AND 12/11/2008 FOR M, AND 12/11/2008 COMES UP.
Btw, there is no need for shouting. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-18 : 11:14:46
|
quote: Originally posted by Vithi MY MISTAKE AGAIN. X, Y, M NOT Z.SORRY IF THERE WERE ANY RUDE SHOUTING...I DIDN'T MEANT TO. ONLY HAPPY SHOUTING..I'LL RESTRAIN MYSELF FROM THAT TOO. :)
Keep your pants on  E 12°55'05.63"N 56°04'39.26" |
 |
|
|
Vithi
Starting Member
19 Posts |
Posted - 2008-12-19 : 09:39:16
|
| Yes. |
 |
|
|
Vithi
Starting Member
19 Posts |
Posted - 2008-12-23 : 10:34:14
|
Table_1DOCID ID12345 9999912345 9999812345 9999712345 9999623456 9999523456 9999423456 99993 34567 99992 34567 9999145678 9999056789 8888956789 8888456789 8888356789 8888256789 88881Table_2ID Value Date (yyyy/m/d) Updated 99999 x 2008/01/05 Y99998 x 2008/01/09 N99997 y 2008/01/1099996 t 2008/05/0399995 y 2008/05/06 99994 m 2006/04/06 99993 d 2006/04/2399992 x 2006/11/22 N99991 m 2006/11/2399990 m 2007/05/02 88889 m 2008/09/2588884 y 2008/09/2988883 y 2008/08/0188882 x 2008/01/06 Y88881 m 2008/09/27Should look like this:DOCID Val X Latest Date Value Y Latest Date Value M Latest Date12345 2008/01/05 2008/01/10 -23456 - 2008/05/06 2006/04/0634567 - - 2006/11/2345678 - - 2007/05/0256789 2008/01/06 2008/09/29 2008/09/27Another condition needs to be included, so that only the maximum date of x should come up and it the 'Updated' in Table_2 must be 'y'.SELECT t1.DOCID, MAX(CASE WHEN (t2.Value = 'X' and t2.updated = 'y') THEN t2.Date ELSE NULL END) AS [Val X Latest Date], MAX(CASE WHEN t2.Value = 'Y' THEN t2.Date ELSE NULL END) AS [Val Y Latest Date], MAX(CASE WHEN t2.Value = 'M' THEN t2.Date ELSE NULL END) AS [Val Z Latest Date]FROM Table_1 AS t1INNER JOIN Table_2 AS t2 ON t2.ID = t1.IDWHERE t2.Value IN ('X', 'Y', 'M')GROUP BY t1.DOCIDORDER BY t1.DOCIDby altering the code that way, i am receiving the correct values for x when it is max date and the updated value 'y' from table_2.But i don't want the rest of it to show up with the id. i need to exclude the blank ones. i am not sure if i have to do an if statement. if i were to include it in the code so that t2.updated = 'y', it would be general and the rest of the values will not show up either. i want it to be specific. if it is possible, let me know. thank you. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-12-23 : 14:23:48
|
quote: Originally posted by Vithi 1
Why did you edit/delete original Post? Now you will not get any help from experts over here. |
 |
|
|
Vithi
Starting Member
19 Posts |
Posted - 2008-12-23 : 14:43:09
|
| that was by mistake. my bad. |
 |
|
|
|