根据每个国家/地区的预定义份额将固定值拆分到国家/地区

问题描述

DB-Fiddle

get_header_image()

预期结果:

<?PHP

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admin;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;

class ForgotePasswordController extends Controller
{
    public function resetForm()
    {
        return view('auth.forgot-password');
    }

    public function sendMail(Request $request)
    {
        $request->validate([
            'email' => 'required|email|exists:admins',]);

        $token = Str::random(64);

        DB::table('password_resets')->insert(
            ['email' => $request->email,'token' => $token,'created_at' => Carbon::Now()]
        );


        Mail::send('auth.verify-email',['token' => $token],function ($message) use ($request) {
            $message->to($request->email);
            $message->subject('Reset Password Notification');
        });

        return back()->with('message','We have e-mailed your password reset link!');
    }

    public function getpassword($token)
    {
        return view('auth.reset-password',['token' => $token]);
    }

    public function updatePassword(Request $request)
    {
        $request->validate([
            'email' => 'required|email|exists:admins','password' => 'required|string|min:6|confirmed',]);

        $updatePassword = DB::table('password_resets')
            ->where(['email' => $request->email,'token' => $request->token])
            ->first();
        
        if (!$updatePassword)
            return back()->withinput()->with('error','Invalid token!');

        $user = Admin::where('email',$request->email)
            ->update(['password' => Hash::make($request->password)]);

        DB::table('password_resets')->where(['email' => $request->email])->delete();

        return redirect()->route('admin.login')->with('message','Your password has been changed!');
    }
}

我想根据预定义份额np.linspace(0,2 * np.pi,10)分给每天每个国家/地区
股份是:import numpy as np import matplotlib.pyplot as plt # input data azimut = np.random.rand(3000) * 2 * np.pi radius = np.random.rayleigh(9,size=3000) # binning rbins = np.linspace(0,radius.max(),7) abins = np.linspace(0,10) subdivs = 10 abins2 = np.linspace(0,(len(abins) - 1) * subdivs + 1) # histogram hist,_,_ = np.histogram2d(azimut,radius,bins=(abins,rbins)) A1,R1 = np.meshgrid(abins,rbins) A2,R2 = np.meshgrid(abins2,rbins) fig,(ax1,ax2) = plt.subplots(ncols=2,figsize=(10,4),subplot_kw=dict(projection="polar")) # plot with original mesh pc1 = ax1.pcolormesh(A1,R1,hist.T,cmap='hsv') ax1.tick_params(axis='y',labelcolor='white') ax1.set_xticks(abins[:-1]) fig.colorbar(pc1,ax=ax1) # plot with subdivided mesh pc2 = ax2.pcolormesh(A2,R2,np.repeat(hist.T,subdivs,axis=1),cmap='hsv') ax2.tick_params(axis='y',labelcolor='white') ax2.set_xticks(abins[:-1]) ax2.set_yticks(rbins,minor=True) ax2.grid(axis='x',color='white') ax2.grid(axis='y',which='minor',color='white') fig.colorbar(pc2,ax=ax2) plt.tight_layout() plt.show()

CREATE TABLE costs (
    id SERIAL PRIMARY KEY,event_date DATE,country VARCHAR,channel VARCHAR,costs DECIMAL
);

INSERT INTO costs
(event_date,country,channel,costs)
VALUES 
('2020-02-08','DE','channel_01','400'),('2020-02-08','channel_02','channel_03','FR','NL',('2020-04-15','300'),'300');

你知道我需要什么查询来实现这个吗?

解决方法

最好的方法是将这些费率添加到表格中并在您的查询中使用该表格。

说明如何使用查询中的硬编码值执行此操作:

SELECT
c.event_date,c.country,c.costs * case country when  'DE' then 0.6 
          when 'FR' then 0.3
          when 'NL' then 0.1 
          end 
FROM costs c
GROUP BY 1,2,3
ORDER BY 1,2;
,

只需使用 CASE 表达式:

SELECT c.event_date,(CASE WHEN c.country = 'DE' THEN 0.6 * c.costs
             WHEN c.country = 'FR' THEN 0.3 * c.costs
             WHEN c.country = 'NL' THEN 0.1 * c.costs
        END) as allocated_costs
FROM costs c
GROUP BY c.event_date,c.costs
ORDER BY 1,2;

如果您愿意,您可以更方便地将值存储在派生表中:

SELECT c.event_date,(v.alloc * c.costs) as allocated_costs
FROM costs c JOIN
     (VALUES ('DE',0.6),('FR',0.3),('NL',0.1)
     ) v(country,alloc)
     USING (country)
GROUP BY c.event_date,c.costs,v.alloc
ORDER BY 1,2;

Here 是一个 dbfiddle。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...