简体   繁体   English

如何从数据库中的一列中获取计算数据

[英]How to get calculated data from one column in database

im new in sql. I cannot get data with format what i want in one step.我是 sql 中的新人。我无法一步获取具有我想要的格式的数据。 Now i'm using more sql commands.现在我正在使用更多 sql 命令。 I want to get all data in one command because i cant to connect them in subquery with group by.我想在一个命令中获取所有数据,因为我无法在子查询中将它们与 group by 连接起来。 Somebodys can help me?有人可以帮助我吗?

example of Table i have:表的例子我有:

id ID order_id order_id order_status订单状态
1 1个 1 1个 0 0
2 2个 1 1个 0 0
3 3个 1 1个 0 0
4 4个 1 1个 1 1个
5 5个 1 1个 1 1个
6 6个 2 2个 0 0
7 7 2 2个 0 0
8 8个 2 2个 1 1个

Table i want to have after sql query:在 sql 查询之后我想要的表:

order_id order_id count数数 of progress(%)进步(%)
1 1个 2 2个 5 5个 40 40
2 2个 1 1个 3 3个 33 33

queries i use:我使用的查询:

SELECT order_id, COUNT(status) as count
FROM `orders`
WHERE status = 1
GROUP by order_id;
SELECT order_id, COUNT(status) as of
FROM `orders`
GROUP by order_id;
SELECT order_id,
       CAST((SELECT COUNT(status) FROM `orders` WHERE status = 1) /
            (SELECT COUNT(status) FROM `orders`) *100 as int) AS progress FROM orders
group by order_id;

but last working properly only if i use where to single order id.但只有当我使用 where to single order id 时才能正常工作。

I want to make this data in one sql query to format i showed up.我想在一个 sql 查询中将这些数据制作成我出现的格式。

Thanks a lot guys!非常感谢你们!

You don't need subqueries to do this, SQL's ordinary aggregate functions already work as you want with your group by clause:您不需要子查询来执行此操作,SQL 的普通聚合函数已经可以按照您希望的方式使用 group by 子句:

SELECT order_id,
       SUM(order_status) AS `count`,
       COUNT(*) AS `of`,
       SUM(order_status) / COUNT(order_status) * 100 as `progress`
FROM orders
group by order_id;

See example at http://sqlfiddle.com/#!9/d1799db/4/0请参阅http://sqlfiddle.com/#!9/d1799db/4/0中的示例

you need to use multiple subqueries here's a query that I used and worked on your example on the onecompiler.com website您需要使用多个子查询这是我在 onecompiler.com 网站上使用和处理您的示例的查询

-- create
CREATE TABLE EMPLOYEE (
  order_id INTEGER,
  order_status INTEGER
);

-- insert
INSERT INTO EMPLOYEE VALUES (1,0 );
INSERT INTO EMPLOYEE VALUES (1, 0);
INSERT INTO EMPLOYEE VALUES (1, 0);


INSERT INTO EMPLOYEE VALUES (1, 1);
INSERT INTO EMPLOYEE VALUES (1,1 );


INSERT INTO EMPLOYEE VALUES (2, 0);
INSERT INTO EMPLOYEE VALUES (2, 0);
INSERT INTO EMPLOYEE VALUES (2, 1);

select *
from EMPLOYEE;

SELECT order_id, count, off , count/off
from(
    select distinct order_id as order_id, 
    (select count(order_id) from EMPLOYEE C WHERE A.order_id=C.order_id  AND order_status =1)  as 'count',
    (select count(order_id) from EMPLOYEE B WHERE A.order_id=B.order_id )  as 'off'
    FROM EMPLOYEE A
    ) AA
    ;

You need to use sum and count with group by.您需要将 sum 和 count 与 group by 一起使用。

 create table orders( id int, order_id int, order_status int); insert into orders values (1,1,0), (2,1,0), (3,1,0), (4,1,1), (5,1,1), (6,2,0), (7,2,0), (8,2,1);
 select order_id, sum(order_status) count, count(order_id) "of", (100 * sum(order_status)) / count(order_id) progress from orders group by order_id order by order_id;
 order_id |订单编号 | count |计数 | of |的| progress -------: |进展 ------: | ----: | ----: | -: | -: | -------: 1 | ------: 1 | 2 | 2 | 5 | 5 | 40.0000 2 | 40.0000 2 | 1 | 1 | 3 | 3 | 33.3333 33.3333

db<>fiddle here db<> 在这里摆弄

i was described my problem without some details, wi want to join with other table but i see only record with status我被描述了我的问题但没有一些细节,我想加入其他表但我只看到有状态的记录

oders_details |订单详情 | id |编号 | order_describe |订单描述 | order_date |订购日期 | |:----:|:--------------:|:----------:| |:----:|:------------:|:----------:| | | 1 | 1 | sample 1 |样本 1 | 2022-02-28 | 2022-02-28 | | | 2 | 2 | sample 2 |样本 2 | 2022-02-28 | 2022-02-28 | | | 3 | 3 | sample 3 |样品 3 | 2022-03-01 | 2022-03-01 | | | 4 | 4 | sample 4 |样品 4 | 2022-03-02 | 2022-03-02 |

orders_status |订单状态 | id |编号 | order_id |order_status| order_id |order_status| |:---:|:---------------:|:----------:| |:---:|:----------------:|:----------:| | | 1 | 1 | 1 | 1 | 0 | 0 | | | 2 | 2 | 1 | 1 | 0 | 0 | | | 3 | 3 | 1 | 1 | 0 | 0 | | | 4 | 4 | 1 | 1 | 1 | 1 | | | 5 | 5 | 1 | 1 | 1 | 1 | | | 6 | 6 | 2 | 2 | 0 | 0 | | | 7 | 7 | 2 | 2 | 0 | 0 | | | 8 | 8 | 2 | 2 | 1 | 1 |

table i want after query查询后我想要的表

orders_view |订单视图 | id |order_id|order_describe| id |order_id|order_describe| order_date |订购日期 | count |计数 | of |的| progress |进展 | |-----|--------|--------------|------------|-------|----|:--------:| |-----|---------|-------------|------------|----- -|----|:--------:| | | 1 | 1 | 1 | 1 | sample 1 |样本 1 | 2022-02-28| 2022-02-28| 2 | 2 | 5 | 5 | 40 | 40 | | | 2 | 2 | 2 | 2 | sample 2 |样本 2 | 2022-02-28| 2022-02-28| 1 | 1 | 3 | 3 | 33 | 33 | | | 3 | 3 | 3 | 3 | sample 3 |样品 3 | 2022-03-01| 2022-03-01| null |null| null |空| null | null | | | 4 | 4 | 4 | 4 | sample 4 |样品 4 | 2022-03-02| 2022-03-02| null |null| null |空| null | null |

i want to get some hint what i have todo, to get finally table or view, not complete solution, to better understand sql lang我想得到一些提示我必须做什么,最终得到表格或视图,而不是完整的解决方案,以便更好地理解 sql lang

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Pyspark / Python 数据集中先前计算的列中获取值 - How to get value from previous calculated column in Pyspark / Python data set 如何将数据从Arduino接收到mysql数据库中并计算出平均值 - How to take data from Arduino into an mysql database with an average calculated 如何用另一个表中的数据(计算)填充 SQL 列 - How to fill a SQL column with data (calculated) from another table 如何在一列中使用SUM从数据库中检索数据的值 - How to retrieve value of data from database with SUM in one column 如何在一个视图中从两个表中获取多个列数据 - How to get multiple column data from two tables in one view 如何更改数据库中一列的数据? - How to change data for one column in a database? 如何将计算输出作为 mysql 列? - How to get a calculated output as mysql column? 数据库中的一列存储一个或多个复选框的值,如何从整个列中获得不同的值? - A column in database store one or more values of check boxes, How i get distinct values from whole column? 从一行中的列中获取多个数据 - get multiple data from column in one row 如何将数据从一个mysql数据库传输到另一个数据库,以及如何使用python映射具有不同列名的数据 - how to transfer data from one mysql database to another and mapping the data with different column names using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM