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

时间:2021-05-05 12:59:27

标签: sql postgresql

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, 2 * np.pi, 10) subdivs = 10 abins2 = np.linspace(0, 2 * np.pi, (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', 'DE', 'channel_02', '400'),
('2020-02-08', 'DE', 'channel_03', '400'),
('2020-02-08', 'FR', 'channel_01', '400'),
('2020-02-08', 'FR', 'channel_02', '400'),
('2020-02-08', 'NL', 'channel_01', '400'),

('2020-04-15', 'DE', 'channel_01', '300'),
('2020-04-15', 'FR', 'channel_01', '300'),
('2020-04-15', 'NL', 'channel_01', '300'),
('2020-04-15', 'NL', 'channel_02', '300'),
('2020-04-15', 'NL', 'channel_03', '300');

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

2 个答案:

答案 0 :(得分:0)

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

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

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;

答案 1 :(得分:0)

只需使用 CASE 表达式:

SELECT c.event_date,
       c.country,
       (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.country, c.costs
ORDER BY 1, 2;

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

SELECT c.event_date, c.country, (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.country, c.costs, v.alloc
ORDER BY 1, 2;

Here 是一个 db<>fiddle。