| Author |
Topic |
|
jonathan.crockett
Starting Member
4 Posts |
Posted - 2009-01-16 : 06:27:54
|
| I have two seperate views (vw_dm_last_bill_date and vm_inv_last_bill_date) which show the max bill date from two other queries, both contain only two fields; mmatter and last_bill_date.I want to create a third view which gives me the max last_bill_date for each mmatter.Any help would be much appreciated. |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-01-16 : 06:32:01
|
| [code]Create View as NewView asselect mmatter,max(vw_dm_last_bill_date) as maxbilldatefrom(select mmatter,vw_dm_last_bill_date from vw_dm_last_bill_dateunion allselect mmatter,vw_dm_last_bill_date from vm_inv_last_bill_date)tgroup by mmatter[/code] |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2009-01-16 : 06:32:52
|
Umh...my personal recommendation would be to stay away from nested views but I guess that wasn't the question... :SELECT MAX(a.last_bill_date)FROM ( SELECT last_bill_date FROM vw_dm_last_bill_date UNION ALL SELECT last_bill_date FROM vm_inv_last_bill_date ) as a EDIT: just changed column name...- Lumbago |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-16 : 06:40:56
|
| Union all The Data With Both Of Two Views and then find the max from this.... |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-01-16 : 06:46:18
|
quote: Originally posted by Lumbago Umh...my personal recommendation would be to stay away from nested views but I guess that wasn't the question... :SELECT MAX(a.last_bill_date)FROM ( SELECT last_bill_date FROM vw_dm_last_bill_date UNION ALL SELECT last_bill_date FROM vm_inv_last_bill_date ) as a EDIT: just changed column name...- Lumbago
That'll return you one row (max date of all nmatters) and not max for every nmatter. |
 |
|
|
jonathan.crockett
Starting Member
4 Posts |
Posted - 2009-01-16 : 06:47:10
|
| Well I'd be more than happy to do it from the actual tables, but that was getting my head in an even bigger mess. So if you could help with that. The two original views are*** 1st View ***ALTER VIEW [dbo].[vw_dm_last_bill_date]ASSELECT son_db.dbo.matter.mmatter, MAX(son_db.dbo.ledger.ltradat) AS last_bill_dateFROM son_db.dbo.matter LEFT OUTER JOIN son_db.dbo.ledger ON son_db.dbo.ledger.lmatter = son_db.dbo.matter.mmatter AND son_db.dbo.ledger.llcode IN ('FEES') GROUP BY son_db.dbo.matter.mmatter*** 2nd View ***ALTER VIEW [dbo].[vw_inv_last_bill_date]ASSELECT [Matter Number] as mmatter, MAX([Invoice Date]) AS last_bill_DateFROM dbo.INV_INVOICESGROUP BY [Matter Number]I essentially need a view that will tell me the last Bill date for each mmatter.If someone has a view that would tidy this up that would be awesome |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-01-16 : 06:54:17
|
| [code]Create YourNewView as select mmatter,max(last_bill_date) as max_last_bill_datefrom(SELECT son_db.dbo.matter.mmatter, (son_db.dbo.ledger.ltradat) AS last_bill_dateFROM son_db.dbo.matter LEFT OUTER JOINson_db.dbo.ledger ON son_db.dbo.ledger.lmatter = son_db.dbo.matter.mmatter AND son_db.dbo.ledger.llcode IN ('FEES') union allSELECT [Matter Number] as mmatter, ([Invoice Date]) AS last_bill_DateFROM dbo.INV_INVOICES)tgroup by mmatter[/code] |
 |
|
|
jonathan.crockett
Starting Member
4 Posts |
Posted - 2009-01-16 : 07:30:55
|
| Thanks so much for that, works a treat. |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-16 : 07:34:22
|
| May Be Like This,......with ashishashish as(select mmatter,max(last_bill_date),row_number() over (partition by mmatter order by mmatter)as RN from ( select * from vw_dm_last_bill_date UNION ALL select * from vm_inv_last_bill_date)t)where RN=1Thanks,, |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-16 : 08:03:49
|
| A Little modification Is required In the last post,,,,,select * from (select mmatter,max(last_bill_date),row_number() over (partition by mmatter order by mmatter)as RN from ( select * from vw_dm_last_bill_date UNION ALL select * from vm_inv_last_bill_date)t)Awhere A.RN=1Try This ....Thanks..... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-16 : 09:02:58
|
quote: Originally posted by ashishashish May Be Like This,......with ashishashish as(select mmatter,max(last_bill_date),row_number() over (partition by mmatter order by mmatter)as RN from ( select * from vw_dm_last_bill_date UNION ALL select * from vm_inv_last_bill_date)t)where RN=1Thanks,,
ashishashish please test your queries before posting. above query doesnt have select part at all (you're just creating a cte alone) |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-16 : 09:48:50
|
quote: Originally posted by visakh16
quote: Originally posted by ashishashish May Be Like This,......with ashishashish as(select mmatter,max(last_bill_date),row_number() over (partition by mmatter order by mmatter)as RN from ( select * from vw_dm_last_bill_date UNION ALL select * from vm_inv_last_bill_date)t)where RN=1Thanks,,
ashishashish please test your queries before posting. above query doesnt have select part at all (you're just creating a cte alone)
yea so i do post the modified query ,,,well thanks for our suggestion and let me know if further correction is required,,,Thanks,,, |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-16 : 10:05:09
|
quote: Originally posted by ashishashish May Be Like This,......with ashishashish as(select mmatter,max(last_bill_date),row_number() over (partition by mmatter order by mmatter)as RN from ( select * from vw_dm_last_bill_date UNION ALL select * from vm_inv_last_bill_date)t)where RN=1Thanks,,
So Sorry for replying wrong query this query should be like that,,,,,with ashishashish as(select mmatter,max(last_bill_date),row_number() over (partition by mmatter order by mmatter)as RN from ( select * from vw_dm_last_bill_date UNION ALL select * from vm_inv_last_bill_date)t)select * from ashishashish where RN=1May Be This Is the Right One,,,,,Thanks,,,, |
 |
|
|
|