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 |
|
ruby_sqlserver
Starting Member
24 Posts |
Posted - 2007-06-05 : 07:29:39
|
| Hiiiiiii had a table a with 3 attributes i.e sno, date, amountand output is sno date amount1 1/Jan/07 1001 1/APR/07 200NOW I WANT FOLLOWING OUTPUT WITH THE HELP OF SOME FUNCTION OR PROCEDURE PLZ GIVE ME A CODE FOR THAT WHICH WILL RETRIVE THE FOLLOWING OUTPUTSNO DATE AMOUNT1 1/JAN/2007 1001 1/FEB/2007 1001 1/MAR/2007 1001 1/APR/07 2001 1/MAY/07 2001 1/JUN/07 200THE ABOVE OUTPUT SHOULD BE RECOVERED WITH THAT TABLE TILL THE CURRENT MONTH IN OUR SYSTEMhOPE U GOT MY PROBLEMHELP PLEASEWAITING FOR SOMEBODY RESPONSEWith Best RegardsRuby |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-06-05 : 07:34:46
|
| SELECT t.sno, dateadd(month, d.m, date), t.amountfrom table1 as tcross join (select 0 as m union all select 1 union all select 2) as dPeter LarssonHelsingborg, Sweden |
 |
|
|
ruby_sqlserver
Starting Member
24 Posts |
Posted - 2007-06-05 : 07:56:03
|
| thanx.......... it works now i want to split wef field intoapplicablefrom = firstdate ofmonthapplicableupto = last date of monthhope u got my pointoutput should be Sno Date Amount applicablefrom applicableupto1 01/Jan/2007 100 01/jan/2007 31/Jan/2007for same output as above postWith Best RegardsRuby |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-06-05 : 08:24:58
|
| Add this as two column in your select statementdateadd(month, datediff(month,0,date), 0), dateadd(day, -1, dateadd(month, datediff(month,0,date) + 1, 0))--------------------------------------------------S.Ahamed |
 |
|
|
ruby_sqlserver
Starting Member
24 Posts |
Posted - 2007-06-05 : 08:39:15
|
| i m using SELECT t.sno, dateadd(month, d.m, date), t.amountfrom table1 as tcross join (select 0 as m union all select 1 union all select 2) as dabove query for my output and the answer u provided does not involve the proper outputso insert ure commands to the above querymoreover the above query had one more problem its retriving the multiple enteries for the provided dateWith Best RegardsRuby |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-06-05 : 08:51:02
|
| SELECT t.sno, dateadd(month, d.m, date), t.amount,dateadd(month, datediff(month,0,dateadd(month, d.m, date)), 0), dateadd(day, -1, dateadd(month, datediff(month,0,dateadd(month, d.m, date)) + 1, 0))from table1 as tcross join (select 0 as m union all select 1 union all select 2) as d--------------------------------------------------S.Ahamed |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-06-05 : 08:56:49
|
| SELECT t.sno, dateadd(month, d.m, t.date) as thedate, dateadd(month, datediff(month, 0, t.date), 0) as thefirst, dateadd(month, datediff(month, -1, t.date), -1) as thelast, t.amountfrom table1 as tcross join (select 0 as m union all select 1 union all select 2) as dPeter LarssonHelsingborg, Sweden |
 |
|
|
ruby_sqlserver
Starting Member
24 Posts |
Posted - 2007-06-05 : 09:02:58
|
| Thanks Friends It works Hurry........Will post some more problem i faced Hopefully all r solved discussingWith Best RegardsRuby |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-06-05 : 09:03:49
|
| Ahh, now I get you!You want to insert missing monthly records, from the first found until current month?If a record is missing, use data from last known record?Peter LarssonHelsingborg, Sweden |
 |
|
|
ruby_sqlserver
Starting Member
24 Posts |
Posted - 2007-06-05 : 09:06:22
|
| yatrueactually i was making a function for it but it creates error in getdate fieldso i started with query selectionand u people helped me Thanx once againWith Best RegardsRuby |
 |
|
|
ruby_sqlserver
Starting Member
24 Posts |
Posted - 2007-06-06 : 01:14:15
|
| friend wen i use the sql query SELECT TOP 100 PERCENT t.PERSONNEL_ID, DATEADD(month, d.m, t.WEF_DATE) AS wefdate, t.HEAD_AMOUNT, DATEADD(month, DATEDIFF(month, 0, DATEADD(month, d.m, t.WEF_DATE)), 0) AS applicablefrom, DATEADD(day, - 1, DATEADD(month, DATEDIFF(month, 0, DATEADD(month, d.m, t.WEF_DATE)) + 1, 0)) AS applicable_upto, t.HEAD_CODE, t.PLANT_CODEFROM dbo.PA_PER_FIXEDSAL_T t CROSS JOIN (SELECT 0 AS m UNION ALL SELECT 1 UNION ALL SELECT 2) dORDER BY wefdatethen multiple ouput is coming for middle month i reply u the answer tooPERSONNEL_ID wefdate HEAD_AMOUNT applicablefrom applicable_upto HEAD_CODE90801652 01-Mar-07 6600 01-Mar-07 31-Mar-07 BAS90801652 01-Mar-07 950 01-Mar-07 31-Mar-07 CON90801652 01-Mar-07 950 01-Mar-07 31-Mar-07 HR5090801652 01-Apr-07 950 01-Apr-07 30-Apr-07 HR5090801652 01-Apr-07 950 01-Apr-07 30-Apr-07 CON90801652 01-Apr-07 6600 01-Apr-07 30-Apr-07 BAS90801652 01-May-07 1950 01-May-07 31-May-07 HR5090801652 01-May-07 1950 01-May-07 31-May-07 CON90801652 01-May-07 950 01-May-07 31-May-07 CON90801652 01-May-07 6600 01-May-07 31-May-07 BAS90801652 01-May-07 950 01-May-07 31-May-07 HR5090801652 01-May-07 8600 01-May-07 31-May-07 BAS90801652 01-May-07 1950 01-May-07 31-May-07 CON90801652 01-May-07 1950 01-May-07 31-May-07 HR5090801652 01-Jun-07 1950 01-Jun-07 30-Jun-07 HR5090801652 01-Jun-07 1950 01-Jun-07 30-Jun-07 CON90801652 01-Jun-07 8600 01-Jun-07 30-Jun-07 BAS90801652 01-Jun-07 1950 01-Jun-07 30-Jun-07 HR5090801652 01-Jun-07 1950 01-Jun-07 30-Jun-07 CON90801652 01-Jul-07 1950 01-Jul-07 31-Jul-07 CON90801652 01-Jul-07 1950 01-Jul-07 31-Jul-07 HR5090801652 01-Jul-07 8600 01-Jul-07 31-Jul-07 BAS90801652 01-Jul-07 1950 01-Jul-07 31-Jul-07 HR5090801652 01-Jul-07 1950 01-Jul-07 31-Jul-07 CONthe green lines are repeated twicemodify the query for sure so that for each month we r able to retieve one valueWith Best RegardsRuby |
 |
|
|
ruby_sqlserver
Starting Member
24 Posts |
Posted - 2007-06-06 : 01:18:12
|
to the above post i had made a select query which compiles al three headsSELECT PLANT_CODE, PERSONNEL_ID, applicablefrom, applicable_upto, SUM(HEAD_AMOUNT) AS Expr1FROM dbo.vw_tryGROUP BY PLANT_CODE, PERSONNEL_ID, applicablefrom, applicable_uptoPLANT_CODE PERSONNEL_ID applicablefrom applicable_upto Expr11110 90801652 01-Mar-07 31-Mar-07 85001110 90801652 01-Apr-07 30-Apr-07 85001110 90801652 01-May-07 31-May-07 249001110 90801652 01-Jun-07 30-Jun-07 164001110 90801652 01-Jul-07 31-Jul-07 16400here for 01/May/07 the data coming is 8500 + 16400 = 24900but it should come 16400it is coming because in above query the double netry is coming for a particular dat rangeHope this will be solved soonas i need it urgently Hope my problem is being understoodWith Best RegardsRuby |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-06-06 : 01:22:35
|
| can you post sample data of table dbo.PA_PER_FIXEDSAL_T--------------------------------------------------S.Ahamed |
 |
|
|
ruby_sqlserver
Starting Member
24 Posts |
Posted - 2007-06-06 : 02:41:08
|
| the ouput of pa_per_fixedsal_tPersonnel_id Head_code Wef_date Head_Amount90801652 BAS 01/03/2007 660090801652 CON 01/03/2007 95090801652 HR50 01/03/2007 95090801652 BAS 01/05/2007 860090801652 CON 01/05/2007 195090801652 HR50 01/05/2007 195090801652 HR50 01/05/2007 195090801652 CON 01/05/2007 1950from wef_date 01/03/2007 total amount is plus with head bas con and hr50 i.e 8500and from wef_date 01/03/2007 total amount is plus with head bas con and hr50 i.e 16400Hope now u got itWith Best RegardsRuby |
 |
|
|
ruby_sqlserver
Starting Member
24 Posts |
Posted - 2007-06-06 : 03:01:51
|
| Now view6 is the ouput which i need to convert select * from view6 where personnel_id = '90801652' Personnel_id Plant code Expr1 wef_date90801652 1110 8500 01/03/200790801652 1110 16400 01/05/2007SELECT t.PERSONNEL_ID , dateadd(month, d.m, WEF_DATE)as WEF_DATE , t.expr1 as HEAD_AMOUNT ,dateadd(month, datediff(month,0,dateadd(month, d.m, WEF_DATE)), 0) as applicable_from, dateadd(day, -1, dateadd(month, datediff(month,0,dateadd(month, d.m, WEF_DATE)) + 1, 0)) as applicable_upto from view6 as t cross join (select 0 as m union all select 1 union all select 2) as d where personnel_id = '90801652' order by WEF_DATE Personnel_id wef_date head_amount applicable_from applicable_upto90801652 01/03/2007 8500 01/03/2007 31/03/200790801652 01/04/2007 8500 01/04/2007 30/04/200790801652 01/05/2007 8500 01/05/2007 31/05/200790801652 01/05/2007 16400 01/05/2007 31/05/200790801652 01/06/2007 16400 01/06/2007 30/06/200790801652 01/07/2007 16400 01/07/2007 31/07/2007here in this for month of 01/05/2007 two enteries are coming for above querybut i need only one i.e which will give ouput 16400 regarding the above oneHopefully this query is understood by u Please reply urgently as i m also trying With Best RegardsRuby |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-06-06 : 04:13:02
|
quote: Originally posted by ruby_sqlserver to the above post i had made a select query which compiles al three headsSELECT PLANT_CODE, PERSONNEL_ID, applicablefrom, applicable_upto, SUM(HEAD_AMOUNT) AS Expr1FROM dbo.vw_tryGROUP BY PLANT_CODE, PERSONNEL_ID, applicablefrom, applicable_uptoPLANT_CODE PERSONNEL_ID applicablefrom applicable_upto Expr11110 90801652 01-Mar-07 31-Mar-07 85001110 90801652 01-Apr-07 30-Apr-07 85001110 90801652 01-May-07 31-May-07 249001110 90801652 01-Jun-07 30-Jun-07 164001110 90801652 01-Jul-07 31-Jul-07 16400here for 01/May/07 the data coming is 8500 + 16400 = 24900but it should come 16400it is coming because in above query the double netry is coming for a particular dat rangeHope this will be solved soonas i need it urgently Hope my problem is being understoodWith Best RegardsRuby
Time being use max(HEAD_AMOUNT) instead of SUM(HEAD_AMOUNT)--------------------------------------------------S.Ahamed |
 |
|
|
ruby_sqlserver
Starting Member
24 Posts |
Posted - 2007-06-06 : 04:41:42
|
| if i use maximum instead of sum then it will not take values for previous months and i want all values should be retrive for the itme being max is working but this is not proper solution i want date range changed for all the cost for a idHope u got my pointWith Best RegardsRuby |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-06 : 04:44:24
|
Ruby,Don mind if you re-post your table DDL, sample data and the expected result ? There are bits of these all over the thread, kind of messy and confusing. KH |
 |
|
|
ruby_sqlserver
Starting Member
24 Posts |
Posted - 2007-06-06 : 05:31:14
|
| hello khtani m reposting my query Now view6 is the ouput which i need to convert select * from view6 where personnel_id = '90801652' Personnel_id Plant code Expr1 wef_date90801652 1110 8500 01/03/200790801652 1110 16400 01/05/2007SELECT t.PERSONNEL_ID , dateadd(month, d.m, WEF_DATE)as WEF_DATE , t.expr1 as HEAD_AMOUNT ,dateadd(month, datediff(month,0,dateadd(month, d.m, WEF_DATE)), 0) as applicable_from, dateadd(day, -1, dateadd(month, datediff(month,0,dateadd(month, d.m, WEF_DATE)) + 1, 0)) as applicable_upto from view6 as t cross join (select 0 as m union all select 1 union all select 2) as d where personnel_id = '90801652' order by WEF_DATE Personnel_id wef_date head_amount applicable_from applicable_upto90801652 01/03/2007 8500 01/03/2007 31/03/200790801652 01/04/2007 8500 01/04/2007 30/04/200790801652 01/05/2007 8500 01/05/2007 31/05/200790801652 01/05/2007 16400 01/05/2007 31/05/200790801652 01/06/2007 16400 01/06/2007 30/06/200790801652 01/07/2007 16400 01/07/2007 31/07/2007here in this for month of 01/05/2007 two enteries are coming for above querybut i need only one i.e which will give ouput 16400 regarding the above oneHopefully this query is understood by u Please reply urgently as i m also trying i just want that query above should be modified so that multiple enteries for a month is not displayed as i had mentioned earlierHope now u got the problem i supposeWith Best RegardsRuby |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-06 : 05:43:41
|
these 2 rows90801652 01/05/2007 8500 01/05/2007 31/05/200790801652 01/05/2007 16400 01/05/2007 31/05/2007 are from diff record of view6. What is the criteria of choosing the one red over the other KH |
 |
|
|
ruby_sqlserver
Starting Member
24 Posts |
Posted - 2007-06-06 : 06:51:10
|
| Basically my purpose ismy cuurent table named view6 displays values as Personnel_id Plant code Expr1 wef_date90801652 1110 8500 01/03/200790801652 1110 16400 01/05/2007and i want a query for displaying data like this Personnel_id wef_date head_amount applicable_from applicable_upto90801652 01/03/2007 8500 01/03/2007 31/03/200790801652 01/04/2007 8500 01/04/2007 30/04/200790801652 01/05/2007 16400 01/05/2007 31/05/200790801652 01/06/2007 16400 01/06/2007 30/06/200790801652 01/07/2007 16400 01/07/2007 31/07/2007so i enter query as suggested like SELECT t.PERSONNEL_ID , dateadd(month, d.m, WEF_DATE)as WEF_DATE , t.expr1 as HEAD_AMOUNT ,dateadd(month, datediff(month,0,dateadd(month, d.m, WEF_DATE)), 0) as applicable_from, dateadd(day, -1, dateadd(month, datediff(month,0,dateadd(month, d.m, WEF_DATE)) + 1, 0)) as applicable_upto from view6 as t cross join (select 0 as m union all select 1 union all select 2) as d where personnel_id = '90801652' order by WEF_DATE and output comes likePersonnel_id wef_date head_amount applicable_from applicable_upto90801652 01/03/2007 8500 01/03/2007 31/03/200790801652 01/04/2007 8500 01/04/2007 30/04/200790801652 01/05/2007 8500 01/05/2007 31/05/200790801652 01/05/2007 16400 01/05/2007 31/05/200790801652 01/06/2007 16400 01/06/2007 30/06/200790801652 01/07/2007 16400 01/07/2007 31/07/2007which gives repeated value for month of Maybut that i dont want i want Personnel_id wef_date head_amount applicable_from applicable_upto90801652 01/03/2007 8500 01/03/2007 31/03/200790801652 01/04/2007 8500 01/04/2007 30/04/200790801652 01/05/2007 16400 01/05/2007 31/05/200790801652 01/06/2007 16400 01/06/2007 30/06/200790801652 01/07/2007 16400 01/07/2007 31/07/2007so either edit my query or give me a solution for the above thingHope now u got my point ReplyWith Best RegardsRuby |
 |
|
|
Next Page
|
|
|
|
|