RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-182584

Kamo Petrosyan's questions

Martin Hope
Kamo Petrosyan
Asked: 2020-10-26 20:59:08 +0000 UTC

pandas DataFrames 聚合最小最大均值

  • 2

有一个数据框columns=['author_id', 'author_name', 'book_title', 'price']

需要获取DataFramecolumns=['author_name', 'max_price', 'min_price']

最好通过

groupby('author_name').agg({'price': 'min', 'price': 'max'})

难点就在这里,因为聚合在同一个字段上,+我不能立即设置新字段的名称。如我错了请纠正我。

pandas
  • 1 个回答
  • 10 Views
Martin Hope
Kamo Petrosyan
Asked: 2020-03-17 15:14:46 +0000 UTC

UTF 到 Python WSGI

  • 0

实际上代码:

def app(environ, start_response):
    start_response('200 OK', [('Content-type', 'text/html')])
    with codecs.open("template.html", 'r', 'utf8') as template_file:
        template_content = template_file.read()
    return template_content

和一个空的服务器响应

GET / => generated 0 bytes in 1 msecs (HTTP/1.1 200) 1 headers in 44 bytes (1105 switches on core 0)

该文档为空。如果将 template_content 替换为 u"Hi" - 同样的事情。“你好”是正常的回答。

return str(template_content) 抛出编码错误

UnicodeEncodeError: 'ascii' codec can't encode characters in position 82-86: ordinal not in range(128)

检查 uwsgi 和 wsgiref.simple_server

python
  • 1 个回答
  • 10 Views
Martin Hope
Kamo Petrosyan
Asked: 2020-03-06 19:05:40 +0000 UTC

SQL 查询优化或反规范化?

  • 0

问题在于对数据库的查询需要极长的时间(最多 400 ms)

    SELECT * FROM production_category AS cc
        INNER JOIN LATERAL (
            SELECT cc.id AS id, SUM(products) AS p_count FROM (
                SELECT cc.id AS parent_id, category_id, COUNT(id) AS products FROM production_product_categories WHERE category_id IN (
                    SELECT id FROM production_category WHERE lft <= cc.rght AND lft >= cc.lft AND tree_id = cc.tree_id)
                    AND product_id IN ( SELECT id FROM production_product WHERE manufacturer_id = 15 )
                    GROUP BY category_id
                ) AS sub_cc
            GROUP BY parent_id ) AS cp USING(id)
    WHERE cp.p_count > 0;

结构如下:

有一个制造商(manufacturer),他的产品(production_product)和任何产品都可以属于一个或多个类别(production_category)。也可能有类别中没有该制造商的产品。

任务是选择包含该制造商产品的类别。

所以问题出现了:要么我对查询太聪明了,要么应该对数据库进行非规范化并将制造商的类别信息存储在单独的表中(确切关系(制造商 ID,类别 ID))。

PostgreSQL (9.3.4)

     Nested Loop  (cost=207.57..129375.37 rows=42 width=223)
       ->  Seq Scan on production_category cc  (cost=0.00..25.20 rows=620 width=191)
       ->  Subquery Scan on cp  (cost=207.57..208.62 rows=1 width=36)
             Filter: (cc.id = cp.id)
             ->  HashAggregate  (cost=207.57..208.09 rows=42 width=12)
                   Filter: (sum((count(production_product_categories.id))) > 0::numeric)
                   ->  HashAggregate  (cost=206.41..206.83 rows=42 width=8)
                         ->  Nested Loop Semi Join  (cost=9.23..206.20 rows=42 width=8)
                               ->  Nested Loop  (cost=8.94..106.81 rows=45 width=12)
                                     ->  Bitmap Heap Scan on production_category  (cost=4.31..12.78 rows=1 width=4)
                                           Recheck Cond: ((lft <= cc.rght) AND (lft >= cc.lft))
                                           Filter: (tree_id = cc.tree_id)
                                           ->  Bitmap Index Scan on production_category_caf7cc51  (cost=0.00..4.31 rows=3 width=0)
                                                 Index Cond: ((lft <= cc.rght) AND (lft >= cc.lft))
                                     ->  Bitmap Heap Scan on production_product_categories  (cost=4.64..93.58 rows=45 width=12)
                                           Recheck Cond: (category_id = production_category.id)
                                           ->  Bitmap Index Scan on production_product_categories_b583a629  (cost=0.00..4.62 rows=45 width=0)
                                                 Index Cond: (category_id = production_category.id)
                               ->  Index Scan using production_product_pkey on production_product  (cost=0.29..2.20 rows=1 width=4)
                                     Index Cond: (id = production_product_categories.product_id)
                                     Filter: (manufacturer_id = 15)
sql
  • 2 个回答
  • 10 Views
Martin Hope
Kamo Petrosyan
Asked: 2020-12-10 13:31:44 +0000 UTC

摆脱重复的 INNER JOIN

  • 0

PostgreSQL 数据库

CREATE TABLE items (
    id SERIAL,
    user_id INTEGER,
    status SMALLINT
);

CREATE TABLE phones (
  phone VARCHAR(12),
  users INTEGER[]
);

其中 items.status 为 3、5 或 7

phone1, phone2... 列表需要选择下面的表格

phone1;COUNT(items.id) where status=3 ;COUNT(items.id) where status=7 phone2;COUNT(items.id) where status=3 ;COUNT(items.id) where status=7 ...

我的版本乘以重复然后总结

SELECT phones.phone as phone, COUNT(it1.id) as saled_count, COUNT(it2.id) as not_saled_count
FROM phones
INNER JOIN items it1 ON phones.users @> ARRAY[it1.user_id]::int[]
INNER JOIN items it2 ON phones.users @> ARRAY[it2.user_id]::int[]
WHERE phones.phone IN ('phone1', 'phone2')
    AND it1.status = 7 AND it2.status = 3
GROUP BY phones.phone;

结果分别

phone1    5    5
phone2    6    6
...

代替

phone1    3    2
phone2    5    1
...

那么,问题本身:如何提出请求?

sql
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5