2011-11-04 13 views
6
 
id || week_id || user_id || catch_id(0,1,2) 
1 || 2  || 6  || 0 
1 || 3  || 6  || 1 
1 || 4  || 6  || 1 
1 || 5  || 6  || 1 
1 || 6  || 6  || 0 
1 || 7  || 6  || 0 
1 || 8  || 6  || 2 
1 || 9  || 6  || 0 
1 || 10  || 6  || 0 
1 || 11  || 6  || 1 

मैं अधिकतम लगातार सप्ताह catch = 1 और अधिकतम लगातार सप्ताह catch = 0 प्रत्येक उपयोगकर्ता के लिए (लगता है सब) पता लगाने के जरूरत की अधिकतम कोई पता लगाएं। मुझे आशा है कि मैं खुद को स्पष्ट कर दूंगा।लगातार रिकॉर्ड

उपर्युक्त तालिका में

लगातार अधिकतम पकड़ = उपयोगकर्ता 6 के लिए 1 (3,4,5 सप्ताह)

अधिकतम लगातार हफ्तों पकड़ = उपयोगकर्ता 6 के लिए 0 है (है सप्ताह 6,7 और/या सप्ताह 9, 10)

मैं कैसे जा सकता हूं। क्या मैं इसे पूरी तरह से एसक्यूएल में कर सकता हूं। एक php समाधान भी स्वागत है

उत्तर

3

यह एक एसक्यूएल समाधान के लिए काम करना चाहिए के अनुसार क्रमबद्ध मिलता है। हालांकि यह आपको पकड़ में पकड़ने के लिए केवल एक सप्ताह_आईडी देगा। मैं नहीं जानता कि क्या अपनी मेज कहा जाता है तो मैं यह consecutive नीचे जवाब में कहा जाता है:

drop table if exists consecutive; 

create table consecutive 
(id int,week_id int,user_id int,catch_id int); 

insert into consecutive values (1,2,6,0); 
insert into consecutive values (1,3,6,1); 
insert into consecutive values (1,4,6,1); 
insert into consecutive values (1,5,6,1); 
insert into consecutive values (1,6,6,0); 
insert into consecutive values (1,7,6,0); 
insert into consecutive values (1,8,6,2); 
insert into consecutive values (1,9,6,0); 
insert into consecutive values (1,10,6,0); 
insert into consecutive values (1,11,6,1); 

select w,count(*) as max_consecutive_weeks 
from 
(
select 
case when @cur_catch_id != catch_id then @cur_week_id := week_id else @cur_week_id end as w, 
@cur_catch_id := catch_id as catchChange, 
c.* 
from consecutive c 
cross join (select @cur_catch_id := -1,@cur_week_id := -1) t 
where user_id = 6 
order by week_id asc 
) t 
where catch_id = 1 
group by w 
order by max_consecutive_weeks desc,w asc 
limit 1; 

आप एक ही क्वेरी का उपयोग कर सकते where catch_id = 0 को where catch_id = 1 बदलकर catch_id = 0 के साथ अधिकतम लगातार week_ids मिलता है।

गुड लक!

+0

आपकी क्वेरी काम नहीं कर रही है। बस इसे चलाएं और – aWebDeveloper

+0

क्षमा करें। एक उपनाम में टाइपो। मैंने अपना जवाब संपादित कर लिया है। –

1

तो पीएचपी ठीक है, मैं इसे सीधे आगे करना होगा:

  • पकड़ होने सभी वस्तुओं को पुनः प्राप्त = एक्स (0 या 1 जा रहा है, क्या आप गणना करना चाहते हैं पर निर्भर करता है एक्स) आइटम के माध्यम से week_id
  • दोहराएं की एएससी क्रम में:
    • चेक में week_id
    • अद्यतन अंतराल के अधिकतम है या नहीं
0

मैंने कोड की कोशिश नहीं की है, लेकिन इसे काम करना चाहिए; शायद थोड़ा tweaking की आवश्यकता है।

एसक्यूएल उपयोग और सभी पंक्तियों week_id

$currentcatch = ''; 
$run = 0; 
$results = array(); 

while($record) { 
    if ($record['catch_id'] == $currentcatch) { 
     $run++; 
    } else { 
     if (!empty($currentcatch)) { 
     if (empty($results[$currentcatch]) { 
      $results[$currentcatch] = $run; 
     } else { 
      if ($results[$currentcatch] < $run) { 
       $results[$currentcatch] = $run; 
      } 
     } 
     } 

     $run = 1; 
     $currentcatch = $record['catch_id']; 
    } 
} 

print_r($results); 
0

यहां एक PHP समाधान है। मैंने इसे अपने दीपक पर परीक्षण किया है और इसे काम करना चाहिए।

/* 
    steps: 
    1.sort the week_ids 
    2.iterate the sorted week_ids and try to get all possible max numbers of consecutive records 
    3.show the greatest one. 

*/ 
$numstr = array(4,2,5,6,7,1); //sample week_ids,should be fetched from db by catch_id 
sort($numstr); 
$int_max = 1; 
$maxs_array = array(); 
for($i=0;$i<sizeof($numstr);$i++) 
{ 
    $k = $i; 
    while($numstr[$k]) 
    { 
     if($numstr[$k+1] && $numstr[$k+1] == $numstr[$k]+1) //duplicate week_ids not considered yet 
     { 
      $int_max ++; 
     } 
     else 
     { 
      array_push($maxs_array,$int_max); 
      $int_max = 1; 
      continue 2; 
      } 
      $k++; 
    } 
} 
sort($maxs_array); 
echo array_pop($maxs_array); //output 4 
1

एक प्रश्न लिखें और एक सरणी प्रारूप सप्ताह = पकड़ने का कहा जाता है डेटा प्राप्त (कुंजी सप्ताह है और पकड़ मान है) प्रत्येक $ लकीर के

$streak = array(); 
$WeekId = 0; 
$prev = 0; 
$count = 1; 
foreach ($data as $week => $catch) 
{ 
    if($week == ++$WeekId && $prev == $catch) 
    { 
     $count ++; 
     $WeekId = $week; 
    } 
    else 
    { 
     if($prev !== 0) 
     { 
      $streak[$prev][$count]  = $count; 
     } 
     $WeekId      = $week;  
     $count      = 1; 
     $prev      = $catch; 

    } 
} 
$streak[$prev][$count]  = $count; 

अब अधिकतम गणना() [0] और $ streak [1]

संबंधित मुद्दे