Combine two rows in single column data

hi,
I have an requirement
for ex:
Division              Resourcename   PRojectname    Projectstartdate           Projectenddate    allocated percentage
development           A                     one                    '01/01/2014'           '03/31/2014'  
           100%
development           B                     one                    '01/01/2014'           '03/31/2014'  
            100%
development           C                    one                    '01/01/2014'           '03/31/2014'  
            100%
development           D                    one                    '01/01/2014'           '03/31/2014'  
            100%
development           E                   one                    '01/01/2014'             '01/31/2014'  
                25%
development           E                  one                    '02/01/2014'             '03/31/2014'  
                25%
In the above "Resource E have multiple dates ,will work on separate dates on single project,but the resource allocated percentage will be always same(ie)=25%,so the report output look like
Division              Resourcename   PRojectname    Projectstartdate           Projectenddate    allocated percentage
development           A                     one                    '01/01/2014'           '03/31/2014'  
           100%
development           B                     one                    '01/01/2014'           '03/31/2014'  
            100%
development           C                    one                    '01/01/2014'           '03/31/2014'  
            100%
development           D                    one                    '01/01/2014'           '03/31/2014'  
            100%
development           E                   one                    '01/01/2014'             '01/31/2014'
               25%
       '02/01/2014'             '03/31/2014'              
the above SSRS report output look after grouping on division,resource.
this same output i need from sql query.how to achieve this.
Thanks
r.b

Bhupesh, I do not have any idea how your actual data would look like. So the below snippet can
only taken as reference not as an actual.
[Join condition, CASE...WHEN everything would change as per your actual data]
Try the below:
create table Bhupesh_test(Division varchar(50), Res_Name varchar(50), Proj_Name varchar(50), startdate date, enddate date, allocated int)
Insert into Bhupesh_test Values
('development','A','one', '01/01/2014','03/31/2014',100),
('development','B','one', '01/01/2014','03/31/2014',100),
('development','C','one', '01/01/2014','03/31/2014',100),
('development','D','one', '01/01/2014','03/31/2014',100),
('development','E','one', '01/01/2014','01/31/2014',25),
('development','E','one', '02/01/2014','03/31/2014',25)
;With cte
as
Select *, ROW_NUMBER()OVER(Partition by Division,Res_Name,Proj_Name Order by startdate)Rn
From Bhupesh_test
Select Division,Res_Name,Proj_Name,startdate,enddate,allocated From
Select Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
and A.startdate <>B.startdate Then NULL Else A.Division end Division,
Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
and A.startdate <>B.startdate Then NULL Else A.Res_Name end Res_Name,
Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
and A.startdate <>B.startdate Then NULL Else A.Proj_Name end Proj_Name,
Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
Then A.startdate Else B.startdate end startdate,
Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
AND A.startdate <> B.startdate Then A.enddate Else B.enddate end enddate,
Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
and A.startdate <>B.startdate Then NULL Else A.Allocated end Allocated,1 Ordered
From cte A
Inner join cte B on A.Rn = B.Rn+1 and A.Res_Name = B.Res_Name
Union All
Select Division,Res_Name,Proj_Name,startdate,enddate,allocated,0 From cte A where Rn=1
) A
Order by Ordered
Drop table Bhupesh_test

Similar Messages

  • Combine multiple rows in single row

    I am new to SQL server and i am trying to combine multiple row in single row but i am not able to do it.Can anyone help me out?
    Input :
    Id |RED |BUY |BSW
    1328 NULL NULL 0.05
    1328 NULL 0.06 NULL
    1328 0.01 NULL NULL
    1328 0.05 NULL NULL
    1329 NULL NULL 0.05
    1329 NULL 0.05 NULL
    1329 0.05 NULL NULL
    Output
    Id |RED |BUY |BSW
    1328 0.01 0.06 0.05
    1328 0.05 NULL NULL
    1329 0.05 0.05 0.05

    Actually I am consolidating above result into text file and sending it to external system.Main aim is to remove NULL values and arrange the data as expected output.
    Also expected output can be
    Id         |RED   
    |BUY    |BSW
    1328        0.05   
    0.06    0.05
    1328        0.01   
    NULL    NULL
    Or
    Id         |RED   
    |BUY    |BSW
    1328        0.01   
    0.06    0.05
    1328        0.05   
    NULL    NULL
    for Id= 1328.

  • Logic to show two rows of a column as Single row

    Hi
    I need output some thing like below
    ind type amount
    xxxx abc 123
    bac 456
    yyyy abc 123
    bac 456
    zzzzz abc 123
    bac 456
    two rows considered as one row .
    Can any one please help me how to solve this.

    sample like
    iin the first two the column ind should be show as single xxxx and also type with in same row
    Edited by: varma on Apr 10, 2012 1:27 PM

  • Combining two Characteristics in one column for a Bex query

    Hi experts,
    I'm reproducing a Bex query ver. 3.x, with a new query version 7.0.
    Within 3.x query there are two char as rows, where the first has a hierarchy.
    The char are Product(Books) and Volume(book's series), where Volume rapresents a detail of Product.
    When I execute this query, both char are combined and are showed in a single column, not in two distinct columns.
    *NODE 1*
    *--NODE PRODUCT 1A*
       *--NODE PRODUCT 1AA*
          *-- VOLUME 1AA*
          *-- VOLUME 1BB*
          *-- VOLUME 1CC*................
    Instead my new query 7.0 shows me the two char into two distinct columns, not in one like example above.
    Besides, both two version of the master data are exacly alike.
    I'd like to reproduce the first query scenario.
    How can I do?
    Thanks in advance.
    Claudia

    Hi Simon,
    yes, the original 3.5 query have the hierarchy switched on.
    Have you any idea?
    Thanks.

  • Display two heading in single column using ALV report

    Hi Experts,
    I got a requirement for displaying 2 rows heading in a single column report. Is it possible that I can perform this task using ALV. How to get this 2 rows in ALV.
    This is a criteria that need to be output in ALV REPORT.
    MATERIAL CODE--MATERIAL NUMBERSTORAGE LOC----SLOC1     SLOC2
    --DATE--DATE1      DATE2
    123445--TEST MATERIAL22--
    3
    As mentioned above storage loc and date will be changing with respect to data dynamically and under this double heading qty will be displayed.
    Just want to know how to get double heading. It is clear that how to display dynamically but unaware of double heading using fieldcatalog.
    Regards,
    Yahya

    Hi Yahya,
    Please pass row position in fieldcatalogue for the respective columns.
    E.g  MOVE '2' to w_fieldcat-row_pos.  " This will display the field in 2nd row.
    Thanks,
    Rupali

  • How to join 2 rows under single column header

    i want to join 2 or more rows under a single column header. as shown "container details" is the column name with "h", "w", and "s" and the sub column names. so my header should contain all these four names and my 3 rows under h,w and s should be clubed under this container details.
    anyone help me out. thanks in advance.
    container details
    | h | w | s

    Bummer, I just tried the link (which I saved from a couple months ago) and it doesn't work. This was a great site for showing how to do various things in Swing. Sorry for the bad link. Maybe someone saved the sites info or knows where it may have moved. Dang...

  • How to get difference between two rows for a column field?

    hi, all,
    Could anyone show me what query statement is to get the difference betweem two rows for two column fields?
    The tables and its records are like this:
    id,      begin,      end
    p1         21          30
    p2          45          60
    p3          120          150
    I would like to have the query result like this
    id,    diff
    p1     15    --- which is 45 minus 30
    p2     60    --- which is 120 minus 60
    and so on...
    thank you in advance.
    Raffy

    You can use the LAG function to access values from previous rows:
    with q as (select 'p1' id, 21 v_start, 30 v_end from dual
    union all
    select 'p2', 45, 60 from dual
    union all
    select 'p3', 120, 150 from dual)
    select id, v_start, v_end, v_start - lag (v_end, 1, 0)
      over (order by id) v_diff from q
    ID,V_START,V_END,V_DIFF
    p1,21,30,21
    p2,45,60,15
    p3,120,150,60
    See the SQL Language doc
    http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions075.htm

  • Convert rows to single column using t-sql

    Hi All,
    I have a table with 7 columns, et say..
      ID ,PatientName,Date,Time,Room_Num ,Specialized, DoctorName
    My source date looks like this..
      ID      PatientName     Date          Time    Room_Num  Specialized   DoctorName
       1         Sam        10/02/2010      10:00     4         Heart        
    John
       1         Sam        10/02/2010      10:00     4         Lungs        
    Harris
       2         Jones      11/12/2011      11:00     1         Lungs        
    Bob
       3         Jim        12/05/2001      01:00     2         Kidney       
    Greg
       3         Jim        12/05/2001      09:00     2         Eye          
    Roby
       1         Sam        12/22/2010      11:00     1         Heart        
    John
       1         Sam        12/22/2010      11:00     1         Lungs        
    Harris
    My out put should look like this..
       ID      PatientName     Date             DoctorName
       1         Sam        10/02/2010           John,Harris ( Need to be in 1 row because - ID, PatientName, Date is same)
       2         Jones      11/12/2011            Bob       ( Need to show as it is due to no repeatition
    of ID)
       3         Jim        12/05/2001            Greg,Roby   ( Need to be in 1 row because - ID, PatientName,
    Date is same)
       1         Sam        12/22/2010            John,Harris ( Need to be in 1 row because - ID, PatientName, Date is same)
    I am using below query to get above results but I am getting wrong results..Need some help in tweaking the query...
     SELECT DISTINCT ID ,PatientName , Date 
             ,STUFF(( SELECT ',' + DoctorName 
              FROM SampleTable ST1
              WHERE ST1.ID=ST2.ID
              FOR XML PATH('')),1,1,' ') AS DoctorName
              FROM SampleTable1 ST2
    GROUP BY ID ,PatientName , Date
    With above query I am getting results like this...
      ID      PatientName     Date             DoctorName
       1         Sam        10/02/2010         John,Harris ,John,Harris ( here John,Harris are repeating twice because of 4th row)
       2         Jones      11/12/2011         Bob      
       3         Jim        12/05/2001         Greg,Roby   
       1         Sam        12/22/2010         John,Harris ,John,Harris ( here John,Harris are repeating twice because of 1st row)
    Create table statement:
    Create table SampleTable
    ID Int null,     PatientName  varchar(25) null,   Date  datetime null,  Time varchar(10) null,   Room_Num int null, Specialized  varchar(20) null, DoctorName varchar(20) null
    Insert statement:
    Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 1,'Sam','10/02/2010' ,'10:00',4 ,'Heart','John')
     Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 1, 'Sam','10/02/2010' ,'10:00'   ,  4    ,     'Lungs','Harris')
     Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 2, 'Jones' ,'11/12/2011'  ,    '11:00',1, 'Lungs',  'Bob')
     Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values  ( 3,'Jim' ,'12/05/2001'    ,  '01:00',2,'Kidney' ,  'Greg')
     Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 3,'Jim' ,'12/05/2001'     , '09:00',2,'Eye' ,   'Roby')
     Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 1,'Sam' ,'12/22/2010'     , '11:00',1,'Heart', 'John')
     Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 1,'Sam', '12/22/2010'     , '11:00',1 , 'Lungs','Harris')
    I need help to tweak my query.
    Thanks,
    RH
    sql

    WITH cte AS(
    SELECT [ID]
    ,[PatientName]
    ,[Date]
    ,[Time]
    ,[Room_Num]
    ,[Specialized]
    ,[DoctorName]
    ,ROW_NUMBER() OVER(PARTITION BY [ID],[PatientName],[Date] ORDER BY Time) as RowID
    FROM [test].[dbo].[SampleTable]
    SELECT
    cte.[ID]
    ,cte.[PatientName]
    ,CAST(cte.[Date] as DATE) as [Date]
    ,STUFF((SELECT ',' + c.[DoctorName]
    FROM cte c
    WHERE c.ID=cte.ID AND c.PatientName=cte.[PatientName] AND c.[Date]=cte.[Date]
    FOR XML PATH(''),TYPE).value('.','varchar(max)')
    ,1,1,'') AS [DoctorName]
    FROM cte
    WHERE cte.RowID=1

  • Combine two row in sql

    Hello Guys,
    Is it possible to create this kinda output.
    Input:
    Table1
    Column1
    Column
    1
    A
    2
    B
    3
    C
    4
    D
    Table2
    A
    B
    C
    D
    0
    1
    0
    0
    1
    0
    1
    0
    0
    1
    1
    1
    0
    1
    0
    1
    Output:
    B
    AC
    BCD
    BD
    Actually i want to Combine two different table column deepen where 1 .
    thanks

    Hello ,
    you need to use PIVOT ,See the below sample :
    Step 1 :
    insert into DailyIncome values ('SPIKE', 'FRI', 100)
    insert into DailyIncome values ('SPIKE', 'MON', 300)
    insert into DailyIncome values ('FREDS', 'SUN', 400)
    insert into DailyIncome values ('SPIKE', 'WED', 500)
    insert into DailyIncome values ('SPIKE', 'TUE', 200)
    insert into DailyIncome values ('JOHNS', 'WED', 900)
    insert into DailyIncome values ('SPIKE', 'FRI', 100)
    insert into DailyIncome values ('JOHNS', 'MON', 300)
    insert into DailyIncome values ('SPIKE', 'SUN', 400)
    insert into DailyIncome values ('JOHNS', 'FRI', 300)
    insert into DailyIncome values ('FREDS', 'TUE', 500)
    insert into DailyIncome values ('FREDS', 'TUE', 200)
    insert into DailyIncome values ('SPIKE', 'MON', 900)
    insert into DailyIncome values ('FREDS', 'FRI', 900)
    insert into DailyIncome values ('FREDS', 'MON', 500)
    insert into DailyIncome values ('JOHNS', 'SUN', 600)
    insert into DailyIncome values ('SPIKE', 'FRI', 300)
    insert into DailyIncome values ('SPIKE', 'WED', 500)
    insert into DailyIncome values ('SPIKE', 'FRI', 300)
    insert into DailyIncome values ('JOHNS', 'THU', 800)
    insert into DailyIncome values ('JOHNS', 'SAT', 800)
    insert into DailyIncome values ('SPIKE', 'TUE', 100)
    insert into DailyIncome values ('SPIKE', 'THU', 300)
    insert into DailyIncome values ('FREDS', 'WED', 500)
    insert into DailyIncome values ('SPIKE', 'SAT', 100)
    insert into DailyIncome values ('FREDS', 'SAT', 500)
    insert into DailyIncome values ('FREDS', 'THU', 800)
    insert into DailyIncome values ('JOHNS', 'TUE', 600)
    Now :
    VendorId IncomeDay IncomeAmount
    SPIKE FRI 100
    SPIKE MON 300
    FREDS SUN 400
    SPIKE WED 500
    SPIKE TUE 200
    JOHNS WED 900
    SPIKE FRI 100
    JOHNS MON 300
    SPIKE SUN 400
    SPIKE WED 500
    FREDS THU 800
    JOHNS TUE 600
    Step 3:
    select * from DailyIncome
    pivot (avg (IncomeAmount) for IncomeDay in ([MON],[TUE],[WED],[THU],[FRI],[SAT],[SUN])) as AvgIncomePerDay
    Output :
    VendorId MON TUE WED THU FRI SAT SUN
    FREDS 500 350 500 800 900 500 400
    JOHNS 300 600 900 800 300 800 600
    SPIKE 600 150 500 300 200 100 400
    More details
    Ahsan Kabir Please remember to click Mark as Answer and Vote as Helpful on posts that help you. This can be beneficial to other community members reading the thread. http://www.aktechforum.blogspot.com/

  • Convert rows to single column

    Hi All
    Need some assistance, i have a table where i want the output to be a single column
    ex: select from t1;*
    query output_
    rownum col_1
    1     8217
    2     6037
    3     5368
    4     5543
    5     5232
    i would like the result to be : *8217,6037,5368,5543,5232*
    thanks for your help in advance.
    i did look on the web but can't find a solution that is easily understood.

    Hi,
    855161 wrote:
    thanks for responding quickly.
    the link example seems not to work for me:
    below is the information you have requested:Below is some of what I requested. The CREATE TABLE and INSERT statements seem to be missing.
    1. duplicate values have no effect
    2. list doesn't need any order
    3. Version 10.2.0.5.0 For that, I recommend the user-defined aggregate function called STRING_AGG in the Oracle Base page, but called STRAGG by most of the people who use it. You have to copy and run about 60 lines of code from the Oracle Base page, or from AskTom one time, but once you have it installed, the job is as simple as
    SELECT  MIN (rnum)       AS rnum     -- ROWNUM isn't a good column name, since it's the same as a pseudo-column
    ,       STRAGG (count_1)  AS count_1_list
    FROM    table_x
    ;Hundreds of other jobs you have in the future will be just as easy, and you won't have to go through the installation process again.
    Relevant information:
    1. table columns : count_1
    all rows should become a single row with ',' in between
    example:
    select * from t1;
    output:
    Rownum count_1
    1 8217
    2 6037
    3 5368
    4 5543
    5 5232
    Desired results :
    Rownum count_1
    1 8217, 6037, 5368, 5543, 5232
    Edited by: 855161 on Dec 11, 2012 1:25 PMThe main problem with STRAGG is that it doesn't create a list in order. You said that's not an issue in this case, but if you ever do need output in order, then the best option in Oracle 10 is the SYS_CONNECT_BY_PATH technique. The main Oracle-Base page shows how to use SYS_CONNECT_BY_PATH in Oracle 9. Of course, that works in all later versions too, but in version 10 a simpler way, using CONNECT_BY_ISLEAF, became available. See the Oracle-Base Comments Page for the eaiser Oracle 10 technique.

  • Returning two rows with one column containing only one place

    Dear all;
    I have a query that returns two rows similar to this below
    ID      PLACE            PROGRAM
    A       NEWYORK      PROGRAM A
    A       NEWYORK      PROGRAM B
    I would like this instead
    ID      PLACE             PROGRAM
    A       NEWYORK       PROGRAM A
                                   PROGRAM  B
    All help is appreciated. Thank you.Edited by: user13328581 on Mar 22, 2011 11:52 AM

    user13328581 wrote:
    WOW...THanks a lot Solomon, I have never used partitions and row_number in such a manner...can you please explain your logic.Sure:
    row_number() over(partition by id order by place,program)This will take all rows returned by the query andsplit them into buckets (partitions) by id. Inside each bucket it will order rows by place and program and assign them row numbers. So for each ID row number 1 will be row with first (alphabetically) place first programt. And this is the row where we want ID to show up. That is why we wrap the above analytic function in CASE statement which will do exactly that. Now:
    row_number() over(partition by id,place order by program)does pretty much the same just bucket is ID and PLACE combination. So for each such combination we want to show only for the first (alphabetically) programt.
    Now notice in ORDER BY clause I prefix ID and PLACE with table alias. You must do it since otherwise query alias ID and PLACE will take precedence resulting in wrong sort order (remember we nulled all ID and PLACE except for row number 1).
    SY.

  • Convert different rows into single column

    DB : 11.1.0.7
    OS : Solaris Sparc 5.10
    I have one query which is joining few tables and giving me output like below.
    personnum orgnm
    ======= =======
    6     Keyholder
    9     Sales
    3     Mgmt
    I would like to convert that into single column like below.
    col1
    ========
    6,Keyholder,9,Sales,3,Mgmt
    I have tried with pivot and decode, but not getting resule which I am exepcting. Any suggesstions ?

    yashwanth437 wrote:
    listagg() function could work.LISTAGG is not available in 11.1. It was introduced in 11.2.
    Anyway, XML solution:
    with sample_table as (
                          select 6 personnum,'Keyholder' orgnm from dual union all
                          select 9,'Sales' from dual union all
                          select 3,'Mgmt' from dual
    select  rtrim(xmlagg(xmlelement(e,personnum || ',' || orgnm,',').extract('//text()')),',') col1
      from  sample_table
    COL1
    6,Keyholder,9,Sales,3,Mgmt
    SQL> SY.

  • Combining Multiple Rows into single row with multple columns

    Hi Experts,
    I have the following requirement, kindly help me.
    I have data in my table like below.
    ID NAME DEPT
    1 Sam 10
    1 Sam 20
    2 alex     30
    2 alex 40
    2 alex 50
    3 vinod 60
    3 vinod 70
    I want to show the same data into single row with dynamically generating columns for DEPT. I want show like below.
    ID NAME DEPT1 DEPT2 DEPT3
    1 Sam 10 20
    2 alex 30 40 50
    3 vinod 60 70
    It's urgent requirement, kindly help me.
    Thanks in advance.

    Right I've had my drink, so what was this "urgent" question then?
    798616 wrote:
    I have data in my table like below.
    ID NAME DEPT
    1 Sam 10
    1 Sam 20
    2 alex     30
    2 alex 40
    2 alex 50
    3 vinod 60
    3 vinod 70
    I want to show the same data into single row with dynamically generating columns for DEPT. I want show like below.Dynamic numbers of columns eh! Tricky.
    If you understand how SQL statements are executed it's along these lines...
    1. Open Cursor
    2. Parse SQL statement and determine columns
    3. Bind in any input values
    4. Fetch data
    5. Bind out values to columns
    6. Repeat step 3 until no more data
    7. Close cursor
    Now, you're expecting that you can determine the columns (step 2) from the fetched data (step 4 onwards). You can't. The SQL engine needs to know the expected columns before any data is fetched so, it can't base the number of columns on the data itself.
    If you need that requirement, you would need to query the data first and build up a dynamic query based on the data and then execute that dynamically built query to fetch the data and pivot it into those columns, which means that you have queried the data twice. Not good practice and not good (or simple) coding.
    What you're talking of doing is something that should be handled at the presentation/interface layer, not as part of the data fetch.
    Typically these sorts of things are handled most easily in report generation/writer tools such as Oracle Reports, Business Objects etc. where they fetch the data from the database and then process it to format it on the display, pivoting the results as required.
    It's not something that lends itself to be easily achieved in SQL. Yes, SQL can do pivoting of data quite easily, but NOT with a dynamic number of columns.
    If you were to specify that there is a maximum number of columns that you could get (rather than wanting it dynamic), then you can do it simply in SQL with the max-decode method...
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select deptno, ename, row_number() over (partition by deptno order by ename) as rn from emp)
      2  --
      3  select deptno
      4        ,max(decode(rn,1,ename)) as ename1
      5        ,max(decode(rn,2,ename)) as ename2
      6        ,max(decode(rn,3,ename)) as ename3
      7        ,max(decode(rn,4,ename)) as ename4
      8        ,max(decode(rn,5,ename)) as ename5
      9        ,max(decode(rn,6,ename)) as ename6
    10        ,max(decode(rn,7,ename)) as ename7
    11        ,max(decode(rn,8,ename)) as ename8
    12        ,max(decode(rn,9,ename)) as ename9
    13        ,max(decode(rn,10,ename)) as ename10
    14  from t
    15  group by deptno
    16* order by deptno
    SQL> /
        DEPTNO ENAME1     ENAME2     ENAME3     ENAME4     ENAME5     ENAME6     ENAME7     ENAME8     ENAME9     ENAME10
            10 CLARK      KING       MILLER
            20 ADAMS      FORD       JONES      SCOTT      SMITH
            30 ALLEN      BLAKE      JAMES      MARTIN     TURNER     WARD
    SQL>

  • Calculation date deference between two rows in same column and in same table

    mytable
    accountno
    tdate
    1001
    01/01/2014
    1002
    01/01/2014
    1003
    01/01/2014
    1004
    01/01/2014
    1005
    01/01/2014
    1001
    01/02/2014
    1002
    01/02/2014
    1003
    01/02/2014
    1004
    01/02/2014
    1005
    01/02/2014
    1001
    01/03/2014
    1002
    01/03/2014
    1003
    01/03/2014
    1004
    01/03/2014
    1005
    01/03/2014
    1001
    01/04/2014
    1002
    01/04/2014
    1003
    01/04/2014
    1004
    01/04/2014
    1005
    01/04/2014
    This is my table. I want find out account wise date deference between first date to second date, second date to third date, third date to fourth date, fourth date to fifth date........... Could you please provide me the solution with any SQL, VB or MS access
    query!

    In SQL 2012/2014:
    SELECT accountno, tdate,
           datediff(DAY, LAG(tdate) OVER (PARTITION BY accountno ORDER BY tdate), tdate)
    FROM   tbl
    ORDER  BY account, tdate
    In SQL 2005/2008:
    ; WITH CTE AS (
        SELECT accountno, tdate,
               rowno = row_number OVER (PARTITION BY accountno ORDER BY tdate)
        FROM   tbl
    SELECT a.accoounno, a.tdate, datediff(DAY, b.tdate, atdate)
    FROM   CTE A
    LEFT   JOIN CTE B ON b.accountno = a.accountno
                     AND b.rowno     = a.rowno - 1
    ORDER  BY a.accountno, a.tdate
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Convert Rows  into single column

    Hi,
    My employee details table has data like below,
    employee_id position department
    100 technician 50
    100 IT 80
    101 Accountant 60
    101 Accounting Manager 70
    Now i want to covert the rows into column. So my output should be like this.
    output:
    employee_id position department position1 department1
    100 technician 50 IT 80
    101 Accountant 60 Accounting Manager 70
    Help me on this
    Edited by: Vi on Mar 22, 2012 5:36 AM
    Edited by: Vi on Mar 22, 2012 5:36 AM

    Igor.M wrote:
    http:// website link removed /t_converting_rows_columns.htm
    Please don't post links to commercial websites that are only trying to sell their products and services. It breaches the terms of use of the forums.
    There are usually more suitable websites giving much more valuable information without all the self promotion (google adwords etc. are acceptable), or in the case of this particular question there is a FAQ post that relates to it...
    {message:id=9360005}

Maybe you are looking for

  • I want to add timing in my program

    my program wants to run between specified time 5 mins or  10 mins.. it should be chosen manually please help me to add this  kindly help me frndz Solved! Go to Solution. Attachments: GetIPStatus- latest (1).vi ‏30 KB

  • Post-installation for BW, help

    Hi       this is min from india and i want help on steps for the post installation for BW 3.1. i cant work in rsa1 as told in groups i have done the client copy and kept the default profile still i cant work in that rsa1.some where i am doing the mis

  • Gray logo after upgrading from Snow Leopard 10.6.8 to mavericks

    is there any way to fix this, and i don't have the recovary cd for Snow Leopard.

  • Why won't my apps open after I updated them ?

    A few days ago I updated all my apps that needed updates (about 14). However , 2 of those apps didn't update right away (Flixster and TheScore sports) and an error message was displayed which I stupidly did not read and just pressed ok. After I press

  • Why do files converted to Word not open on Mac?

    I am a new user, and have just converted a PDF to Word, on Mac (using Open Office). The message I'm getting says the file may be corrupt, OO tries to fix but fails, and I get Error message. The original is a scan of a Greek text. Is the problem with