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 |
|
SummerWind
Starting Member
5 Posts |
Posted - 2006-07-14 : 03:26:19
|
| Hello, I have problem that I haven’t manage to solve myself. There’s customers, months and visits (dmphsern).I’d like to fill up those NULL fields with previous DMPHSERN, because there’s no visits for each month. That case I’d like to show previous visit. DMPHSERN is not growing number, it can be anything.SELECT T1.CUSTOMER, T1.YEAR_MONTH, T2.DMPHSERN, FROM dbo.CalendarJoinCustomer T1 LEFT OUTER JOIN dbo.Month_last_visit T2 ON T1.CustomerYearMonth = T2.CustomerYearMonthI try to do something with CASE WHEN dmphsern is null… but no success CUSTOMER / YEAR_MONTH / DMPHSERN100000000042135 /200401/NULL100000000042135 /200402 /610000000309630100000000042135 /200403 /NULL100000000042135 /200404 /610000000327948100000000042135 /200405 /180000000352566100000000042135 /200406 /NULL100000000042135 /200407 /NULL100000000042135 /200408 /180000000378866100000000042135 /200409 /NULL100000000042135 /200410 /180000000396350100000000042135 /200411 /180000000414001100000000042135 /200412 /180000000423207100000000042135 /200501 /180000000430233100000000042135 /200502 /180000000442918100000000042135 /200503 /NULL100000000042135 /200504 /NULL100000000042135 /200505 /NULL100000000042135 /200506 /180000000478431100000000042135 /200507 /NULL100000000042135 /200508 /NULL100000000042135 /200509 /180000000490822100000000042135 /200510 /NULL100000000042135 /200511 /180000000514803100000000042135 /200512 /NULL100000000042135 /200601 /NULL100000000042135 /200602 /NULL100000000042135 /200603 /180000000548683100000000042135 /200604 /NULL100000000042135 /200605 /NULL100000000042135 /200606 /180000000564980100000000042135 /200607 /NULL998000000002070 /200401 /NULL998000000002070 /200402 /NULL998000000002070 /200403 /610000000317721998000000002070 /200404 /610000000326862998000000002070 /200405 /610000000331684998000000002070 /200406 /610000000348823998000000002070 /200407 /NULL998000000002070 /200408 /610000000353790RegardsSummerWind |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-07-14 : 03:36:50
|
| What you want to show instead of NULL?You can try IsNull() if you want to handle NULL values?can you provide some more details on this?Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
SummerWind
Starting Member
5 Posts |
Posted - 2006-07-14 : 03:50:01
|
| I'd like to show previous visit serial number (dmphsern) if there is one for that customer.CUSTOMER /YEAR_MONTH /DMPHSERN ->WHAT I WANT100000000042135 200401 NULL ->NULL100000000042135 200402 610000000309630 -> 610000000309630100000000042135 200403 NULL -> 610000000309630100000000042135 200404 610000000327948 ->610000000327948100000000042135 200405 180000000352566 ->180000000352566100000000042135 200406 NULL ->180000000352566100000000042135 200407 NULL ->180000000352566100000000042135 200408 180000000378866 ->180000000378866100000000042135 200409 NULL ->180000000378866100000000042135 200410 180000000396350 ->180000000396350100000000042135 200411 180000000414001 ->180000000414001... 998000000002070 200401 NULL ->NULL998000000002070 200402 NULL ->NULL998000000002070 200403 610000000317721 ->610000000317721998000000002070 200404 610000000326862 ->610000000326862998000000002070 200405 610000000331684 ->610000000331684998000000002070 200406 NULL ->610000000331684Summer |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-07-14 : 04:20:03
|
Do this...select T1.CUSTOMER, T1.YEAR_MONTH, T2.DMPHSERNinto #test FROM dbo.CalendarJoinCustomer T1 LEFT OUTER JOINdbo.Month_last_visit T2 ON T1.CustomerYearMonth = T2.CustomerYearMonthalter table #testadd i int identity(1,1)select t1.Customer,T1.YEAR_MONTH, (case when t1.a is NULL then (select max(t2.a) from #test t2 where t2.i<t1.i and t2.a is not null) else t1.a end) as DMPHSERN from #test t1 First statement is to temporarily store the output of your first select statement in some temp table.Add identity column in that table and last query will give you desired output !Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
SummerWind
Starting Member
5 Posts |
Posted - 2006-07-14 : 06:51:23
|
| Thanks a lot for your reply. I tried to test this... if I undestand you correctly.First about this selectselect t1.Customer,T1.YEAR_MONTH, (case when t1.a is NULL then (select max(t2.a) from #test t2 where t2.i<t1.i and t2.a is not null) else t1.a end) as DMPHSERN from #test t1Did you mean that a is dmphsern, otherwise I can't get it work at all.Result of a that query was that it didn't care when the customer changed, it always gave a previous dmphsern? Query should also give null if customer don't have any dmphsern.Best RegardsSummerWind |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-07-14 : 07:27:40
|
yes a = dmphsernTo get the output you want, change the query as follows:select T1.Customer, T1.YEAR_MONTH, (case when t1.a is NULL then (select max(t2.a) from #test t2 where t2.i<t1.i and t2.a is not null and t1.Customer = t2.Customer ) else t1.a end) from #test t1 Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
SummerWind
Starting Member
5 Posts |
Posted - 2006-07-14 : 14:20:00
|
| Thanks a lot.I think that might be right solution. I will test it as soon I get back to my work and that's after my 4 weeks summer holiday.Have a nice summer =)RegardsSummerWind |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-17 : 06:48:49
|
Here's my effort (based on the sample data provided).SummerWind - if you give the underlying ddl and data for it, we can give you a query based on that. Have a good summer.--datadeclare @t table (CUSTOMER bigint, YEAR_MONTH int, DMPHSERN bigint)insert @t select 100000000042135, 200401, NULLunion all select 100000000042135, 200402, 610000000309630union all select 100000000042135, 200403, NULLunion all select 100000000042135, 200404, 610000000327948union all select 100000000042135, 200405, 180000000352566union all select 100000000042135, 200406, NULLunion all select 100000000042135, 200407, NULLunion all select 100000000042135, 200408, 180000000378866union all select 100000000042135, 200409, NULLunion all select 100000000042135, 200410, 180000000396350union all select 100000000042135, 200411, 180000000414001union all select 100000000042135, 200412, 180000000423207union all select 100000000042135, 200501, 180000000430233union all select 100000000042135, 200502, 180000000442918union all select 100000000042135, 200503, NULLunion all select 100000000042135, 200504, NULLunion all select 100000000042135, 200505, NULLunion all select 100000000042135, 200506, 180000000478431union all select 100000000042135, 200507, NULLunion all select 100000000042135, 200508, NULLunion all select 100000000042135, 200509, 180000000490822union all select 100000000042135, 200510, NULLunion all select 100000000042135, 200511, 180000000514803union all select 100000000042135, 200512, NULLunion all select 100000000042135, 200601, NULLunion all select 100000000042135, 200602, NULLunion all select 100000000042135, 200603, 180000000548683union all select 100000000042135, 200604, NULLunion all select 100000000042135, 200605, NULLunion all select 100000000042135, 200606, 180000000564980union all select 100000000042135, 200607, NULLunion all select 998000000002070, 200401, NULLunion all select 998000000002070, 200402, NULLunion all select 998000000002070, 200403, 610000000317721union all select 998000000002070, 200404, 610000000326862union all select 998000000002070, 200405, 610000000331684union all select 998000000002070, 200406, 610000000348823union all select 998000000002070, 200407, NULLunion all select 998000000002070, 200408, 610000000353790--calculationselect CUSTOMER, YEAR_MONTH, (select top 1 DMPHSERN from @t where CUSTOMER = a.CUSTOMER and YEAR_MONTH <= a.YEAR_MONTH and DMPHSERN is not null order by YEAR_MONTH desc) as DMPHSERNfrom @t a/*resultsCUSTOMER YEAR_MONTH DMPHSERN -------------------- ----------- -------------------- 100000000042135 200401 NULL100000000042135 200402 610000000309630100000000042135 200403 610000000309630100000000042135 200404 610000000327948100000000042135 200405 180000000352566100000000042135 200406 180000000352566100000000042135 200407 180000000352566100000000042135 200408 180000000378866100000000042135 200409 180000000378866100000000042135 200410 180000000396350100000000042135 200411 180000000414001100000000042135 200412 180000000423207100000000042135 200501 180000000430233100000000042135 200502 180000000442918100000000042135 200503 180000000442918100000000042135 200504 180000000442918100000000042135 200505 180000000442918100000000042135 200506 180000000478431100000000042135 200507 180000000478431100000000042135 200508 180000000478431100000000042135 200509 180000000490822100000000042135 200510 180000000490822100000000042135 200511 180000000514803100000000042135 200512 180000000514803100000000042135 200601 180000000514803100000000042135 200602 180000000514803100000000042135 200603 180000000548683100000000042135 200604 180000000548683100000000042135 200605 180000000548683100000000042135 200606 180000000564980100000000042135 200607 180000000564980998000000002070 200401 NULL998000000002070 200402 NULL998000000002070 200403 610000000317721998000000002070 200404 610000000326862998000000002070 200405 610000000331684998000000002070 200406 610000000348823998000000002070 200407 610000000348823998000000002070 200408 610000000353790*/ Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
|
|
|
|
|