| Author |
Topic |
|
Petronas
Posting Yak Master
134 Posts |
Posted - 2009-06-15 : 09:55:36
|
| Hi All,I need to get all the data for fist_billing, second_billing and third_billing, which isstored in the table billing as billing_cycle=1,2,3. But in this BIlling table it is stored asOrder_id Billing_cycle Bill_date5123456 1 4/14/20095123456 2 5/15/20097168907 1 3/2/20097168907 2 4/7/20097168907 3 5/5/2009 I have the below query:if object_id('#temp1') is not null drop table #temp1select distinct(o.order_id),o.product_id,min(convert(varchar(11),f.mail_date,110))date,into #temp1 from fullfillorders f (nolock),orders_base o (nolock)where o.order_id=f.order_id and o.product_id in('pp1','pp2') group by o.order_id,o.product_idselect t.order_id,t.product_id,date as intial_date,case when b.billing_cycle =1 then convert(varchar(11),b.bill_date,110) end as First_Billing_date,case when b.billing_cycle =2 then convert(varchar(11),b.bill_date,110) end as Second_Billing_date,case when b.billing_cycle =3 then convert(varchar(11),b.bill_date,110) end as Third_Billing_datefrom #temp2 t (nolock), billing b(nolock)where t.order_id=b.order_idorder by 4I am getting the following result set:Order_id Product_id Initial_date First_Billing Second_billing Third_Billing7168907 pp1 2/9/2009 3/2/2009 NULL NULL7168907 pp1 2/9/2009 NULL 4/7/2009 NULL7168907 pp1 2/9/2009 NULL NULL 5/5/2009What I want to get is the following:Order_id Product_id Initial_date First_Billing SEcond_billing Third_Billing 7168907 pp1 2/9/2009 3/2/2009 4/7/2009 5/5/2009Thanks for your help,Petronas |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-15 : 10:00:27
|
[code]select t.order_id,t.product_id,date as intial_date,max(case when b.billing_cycle =1 then convert(varchar(11),b.bill_date,110) end) as First_Billing_date,max(case when b.billing_cycle =2 then convert(varchar(11),b.bill_date,110) end) as Second_Billing_date,max(case when b.billing_cycle =3 then convert(varchar(11),b.bill_date,110) end) as Third_Billing_datefrom #temp2 t (nolock), billing b(nolock)where t.order_id=b.order_idGROUP BY t.order_id,t.product_id,dateorder by 4[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Petronas
Posting Yak Master
134 Posts |
Posted - 2009-06-15 : 10:18:05
|
| Hi khtan,Thanks for your time. The solution worked.Appreciate your help.Thanks,Petronas |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-15 : 10:27:35
|
you are welcome KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|