开发者

Not able to fetch data for a day and display with all data fetched from a table

I have a table with sales data for Multiple places and dates. eg:

Date        Area      Total Sales Obj1    Obj2    Obj3    Obj4  

5/29/2011   Maharastra      4       1       0       3       0  
5/30/2011   Maharastra      1       0       0       0       1  
5/30/2011   Kolkota         5       0       5       0       3  
5/30/2011   Bihar           2       0       2       0       3  
5/30/2011   Mumbai          0       0       0       0       1  
5/30/2011   AndhraPradesh   3       0       3       0       2

I want this to be displayed alongwith few more columns that onl开发者_运维知识库y have the previous days data,ie, for Maharastra it should be a single column as shown below eg:

Area     Total Sales Obj1 Obj2 Obj3 Obj4 PrvDyTtl PrvObj1 PrvObj2 PrvObj3 PrvObj4  

Maharastra         5    1    0    3    1        4       1       0       3       0  
Kolkota            5    0    5    0    3        0       0       0       0       0  
Bihar              2    0    2    0    3        0       0       0       0       0  
Mumbai             0    0    0    0    1        0       0       0       0       0  
AndhraPradesh      3    0    3    0    2        0       0       0       0       0  

I am unable to do this as whatever i try to do gives me two rows for the 2 dates for Maharashtra. Please help.


** Please note I am not entirely sure if this statement is 100% compatible with oracle but the overall structure should be the same

SELECT tblToday.Date, tblToday.Area, tblToday.Total, tblToday.Sales, tblToday.Obj1, tblToday.Obj2, tblToday.Obj3, tblToday.Obj4,tblYesterday.Total AS PrevDayTotal, tblYesterday.Obj AS PrevObj1, tblYesterday.Obj2 as PrevObj2, tblYesterday.Obj3 AS PrevObj3,tblYesterday.Obj4 AS PrevObj4
FROM table tblToday LEFT OUTER JOIN 
     table tblYesterday ON DateADD(day,-1,tblToday.Date) = tblYesterday.Date AND tblToday.Area = tblYesterday.Area

Specifying tblToday.Date in the where clause i.e. tblToday.Date = GETDATE()


    select * 
    from sales_data today 
       , sales_data ystd
    where today.area= ystd.area(+)
    and  trunc(today.date_column, 'DD') = trunc(SYSDATE, 'DD')
    and  trunc(ystd.date_column(+), 'DD') = trunc(SYSDATE-1,'DD')

EDIT

SELECT * 
FROM (
      SELECT 
              date_column
            , Area
            , Total
            , Sales
            , Obj1
            , LEAD( Total) over (partition by area order by date_column DESC)
            , LEAD( Sales) over (partition by area order by date_column DESC)
            , LEAD( Obj1 ) over (partition by area order by date_column DESC)
      FROM sales
      WHERE trunc(date_column,'DD') = trunc(SYSDATE-1, 'DD') 
         OR trunc(date_column, 'DD') = trunc(SYSDATE, 'DD') 
)
WHERE trunc(date_column, DD) = trunc(SYSDATE - 1, 'DD')


I know this is not up to the mark in performence point of view as well as in database architechure..

but it should give output what you want in your extraction...

 select t1.Area, t1.Total_Sales as "Total sales", t2.*
from (select area, sum(total_sales) total_sales
      from sales_data
     group by area) t1,
   (select i_t_1.Obj1,
           i_t_1.Obj2,
           i_t_1.Obj3,
           i_t_1.Obj4,
           nvl(i_t_2.Total_Sales, 0) PrvDyTtl,
           nvl(i_t_2.obj1, 0) PrvObj1,
           nvl(i_t_2.Obj2, 0) PrvObj2,
           nvl(i_t_2.Obj3, 0) PrvObj3,
           nvl(i_t_2.Obj4, 0) PrvObj4
      from sales_data i_t_1, sales_data i_t_2
     where i_t_1.area = i_t_2.area(+)
       and trunc(i_t_1.date)= trunc(SYSDATE)
       and trunc(i_t_2.date(+)) = trunc(SYSDATE - 1)) t2
  where t2.area = t1.area;

I supposed that table name is sales_data before check this just replace table name from sales_data to your desired table_name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜