| Author |
Topic |
|
dannyel
Starting Member
2 Posts |
Posted - 2010-06-10 : 13:45:20
|
| Hi all ,I’m new to Sql Queries; I have a requirement which involves Sql query,Can anyone tell the How to proceed or provide me Sql Here goes the requirementCurrent Table ID Month Data1 Jan 10.002 Feb 20.003 Mar 30.004 Apr 50.005 May 60.006 Jun 70.007 Jul 100.008 Aug 110.009 Sep 120.0010 Oct 140.0011 Nov 150.0012 Dec 170.00 Format needed ID Month Data Condition1 Jan 10.00 Jan2 Feb 10.00 Feb-Jan3 Mar 10.00 Mar - Feb4 Apr 20.00 Apr - Mar5 May 10.00 May - Apr6 Jun 10.00 Jun - May7 Jul 30.00 Jul - Aug8 Aug 10.00 Aug - Jul9 Sep 10.00 Sep -Aug10 Oct 20.00 Oct -Sep11 Nov 10.00 Nov - Oct12 Dec 20.00 Dec - NovThanks in advanceAstle astle |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-06-10 : 14:06:14
|
| [code]declare @tbl as table(id int,month varchar(40),inventory decimal(18,2))insert into @tblselect 1, 'Jan' ,10.00 union allselect 2, 'Feb' ,20.00 union allselect 3, 'Mar' ,30.00 union allselect 4, 'Apr' ,50.00 union allselect 5, 'May' ,60.00 union allselect 6, 'Jun' ,70.0 union allselect 7, 'Jul' ,100.00 union allselect 8, 'Aug' ,110.00 union allselect 9, 'Sep', 120.00 union allselect 10,'Oct' ,140.00 union allselect 11,'Nov' ,150.00 union allselect 12,'Dec', 170.00 select t1.id, t1.month,isnull(t1.inventory-t2.inventory,t1.inventory)as Data ,isnull(t2.month + '-' +ISNULL(t1.month,t2.month) ,t1.month) as Condition from @tbl t1left join(select * from @tbl)t2 on t1.id=t2.id+1[/code]Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
dannyel
Starting Member
2 Posts |
Posted - 2010-06-10 : 18:29:36
|
| Let me explain it in detail Create table script (portion of the original table) which I have created in SQL Server CREATE TABLE [Sample]( [ID] [int] NOT NULL, [Month] [nchar](10) NOT NULL, [data] [nchar](10) NULL, CONSTRAINT [PK_Sample] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]SQL to Populate INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(1,'aa','Jan-10',10)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(2,'aa','Feb-10',20)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(3,'aa','Mar-10',30)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(4,'aa','Apr-10',50)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(5,'aa','May-10',60)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(6,'aa','Jun-10',70)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(7,'aa','Jul-10',100)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(8,'aa','Aug-10',110)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(9,'aa','Sep-10',120)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(10,'aa','Oct-10',140)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(11,'aa','Nov-10',150)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(12,'aa','Dec-10',170)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(13,'aa','Jan-09',180.67)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(14,'aa','Feb-09',194.67)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(15,'aa','Mar-09',208.67)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(16,'aa','Apr-09',222.67)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(17,'bb','Jan-10',45)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(18,'bb','Feb-10',80)INSERT INTO [Sample] ([ID] ,[Account],[Month] ,[data]) VALUES(19,'bb','Mar-10',100)And this goes on …Original table has more than million records Result of the Sql Query should look like this ID|--|Account|--|Month|--|Data|--|Data base on this condition1|--|aa|--|Jan-10|--|10|--|Jan 102|--|aa|--|Feb-10|--|10|--|Feb10-Jan103|--|aa|--|Mar-10|--|10|--|Mar10 - Feb104|--|aa|--|Apr-10|--|20|--|Apr10 - Mar105|--|aa|--|May-10|--|10|--|May10 - Apr106|--|aa|--|Jun-10|--|10|--|Jun10 - May107|--|aa|--|Jul-10|--|30|--|Jul10 - Aug108|--|aa|--|Aug-10|--|10|--|Aug10 - Jul109|--|aa|--|Sep-10|--|10|--|Sep10 -Aug1010|--|aa|--|Oct-10|--|20|--|Oct10 -Sep1011|--|aa|--|Nov-10|--|10|--|Nov10 - Oct1012|--|aa|--|Dec-10|--|20|--|Dec10 - Nov1013|--|aa|--|Jan-09|--|181|--|Jan 0914|--|aa|--|Feb-09|--|14|--|Feb09-Jan0915|--|aa|--|Mar-09|--|14|--|Mar09 - Feb0916|--|aa|--|Apr-09|--|14|--|Apr09 - Mar0917|--|bb|--|Jan-10|--|45|--|Jan 1018|--|bb|--|Feb-10|--|35|--|Feb10-Jan1019|--|bb|--|Mar-10|--|20|--|Mar10 - Feb10Explanation1 .for all Jan month, query should return Jan data 2. For rest of the month, it should subtract with previous month within same year and same accounts and return the result (which is specified in Data base on this condition which I have included for the understanding)Please let me know if u require any more information, Thanks in advanceAstle astle |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-06-11 : 01:50:49
|
| Do you want a spoonfeeded answer?Since you are are new to SQL you need to try and show us your attempt at the problem.If you are stuck at something let us know.There are lot of SQL gurus here who will be more than glad to help you.Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
naveengopinathasari
Yak Posting Veteran
60 Posts |
Posted - 2010-06-11 : 03:42:03
|
| Hi All,Have a Look at it, it generates in one query.--------------------------Select id, month, inventory ,row_number() over(order by id) as GenRowNumber,(Select inventory from W PrevVal WHERE PrevVal.id =S.id-1 ) AS prevRowVal, inventory - (Select inventory from W prevVal WHERE PrevVal.id =S.id-1) AS CurrRowValFrom W Sif your id column is Primarykey and is placed in the order this will work fine,else you need to generate a number for the month using Rank().Let me know if any issue.Lets unLearn |
 |
|
|
|
|
|