subquery

Divide by Subquery returning 'Subquery returned more than 1 value.' error

有些话、适合烂在心里 提交于 2021-01-29 17:20:18
问题 I have the following table in which I have two sales orders and each sales order has a different number of transactions. SaleOrder Transaction Amount S1 T1 20 S1 T2 20 S2 T1 15 S2 T2 15 S2 T3 15 The problem is I am getting the total sales order amount against each transaction in SQL Server table when in fact they need to be divided equally among the number of transactions like this: SaleOrder Transaction Amount S1 T1 10 S1 T2 10 S2 T1 5 S2 T2 5 S2 T3 5 I've written the following query: SELECT

MySQL procedure subquery returns null

Deadly 提交于 2021-01-29 10:16:52
问题 I have a mysql procedure where I'm setting values using subqueries as it progresses, and partway through the subqueries, all structured the same only using different parameters, start returning null. If I literally copy and paste the subquery into a sql window, it returns a result. Service Revenue to Income Summary works just fine, but after "COGS to Income Summary", "VALUE" continues to return NULL DELIMITER $$ CREATE DEFINER=`shopf740`@`localhost` PROCEDURE `PeriodEnd`(IN `processdate` DATE

How to sum *count* subqueries with SQLAlchemy?

ぐ巨炮叔叔 提交于 2021-01-29 08:36:38
问题 I have the following models in my DB (Flask-SQLALchemy, declarative approach, simplified): class Player(db.Model): id = db.Column(db.Integer, primary_key = True) ... class Game(db.Model): id = db.Column(db.Integer, primary_key = True) creator_id = db.Column(db.Integer, db.ForeignKey('player.id')) creator = db.relationship(Player, foreign_keys='Game.creator_id') opponent_id = db.Column(db.Integer, db.ForeignKey('player.id')) opponent = db.relationship(Player, foreign_keys='Game.opponent_id')

How to display the total count of the last row by filestatus?

半世苍凉 提交于 2021-01-29 08:13:06
问题 I have to display the total count of the last row by filestatus. tbl_bankdata bank_id | b_orderno| b_bankname| lead_id 1 | 01-01 | 1 | 1 2 | 01-02 | 2 | 1 3 | 02-01 | 3 | 2 4 | 03-01 | 1 | 3 tbl_fileStatus f_id | f_bankid| f_filestatus 1 | 1 | 1 2 | 2 | 1 3 | 2 | 2 4 | 1 | 2 5 | 1 | 3 6 | 3 | 2 7 | 3 | 3 I have two tables tbl_bankdata and tbl_fileStatus . I am sending bank_id in the tbl_fileStatus as a f_bank_id . Now I have to show the last f_bankid count. For example, I have to fetch the

How to exclude records from results when ID from A table is in PID column of B table for the same PUID

こ雲淡風輕ζ 提交于 2021-01-29 07:14:29
问题 How to exclude records from results when ID from A table is in PID column of B table for the same PUIDenter image description here Table A: PUID PID SYSTEMCODE ID 100000 701848421 A 3207479 Table B: PUID PID SYSTEMCODE 100000 3207479 B 100000 6805875 B 回答1: Use not exists : select a.* from tablea a where not exists (select 1 from tableb b where b.pid = a.id and b.puid = a.puid) 来源: https://stackoverflow.com/questions/64794523/how-to-exclude-records-from-results-when-id-from-a-table-is-in-pid

Replace comparison to scalar subquery by inner join or left/right join

夙愿已清 提交于 2021-01-29 04:29:10
问题 I need to write this query using inner joins or right/left joins but I don't know how to start: select * from radicados where asignado = (select estudianteid from estudiantes where usuario = (select usuarioid from usuarios where nombre = $nombre_usuario)) But I don't know how to do the same with joins. I think this must be something like: select * from radicados inner join usuarios on usuarioid=usuario 回答1: It appears you want something like this: select radicados.* from radicados join

Replace comparison to scalar subquery by inner join or left/right join

╄→гoц情女王★ 提交于 2021-01-29 04:22:07
问题 I need to write this query using inner joins or right/left joins but I don't know how to start: select * from radicados where asignado = (select estudianteid from estudiantes where usuario = (select usuarioid from usuarios where nombre = $nombre_usuario)) But I don't know how to do the same with joins. I think this must be something like: select * from radicados inner join usuarios on usuarioid=usuario 回答1: It appears you want something like this: select radicados.* from radicados join

MySQL: Sum of multiple subqueries

不羁岁月 提交于 2021-01-29 03:35:35
问题 I need to order a MySQL query by the resulting SUM of multiple subqueries. Here's some example code for what I'm trying to do: SELECT ... (SELECT SUM( (SELECT one_result ... LIMIT 1) as plays1, (SELECT one_result ... LIMIT 1) as plays2, (SELECT one_result ... LIMIT 1) as plays3 )) as total_plays FROM plays ORDER BY total_plays Basically, I need to run three subqueries that'll each return an integer. I need to order the entire query by the SUM() of these integers. When I try to run this query

SQL | Return MIN values | multiple rows dynamically

筅森魡賤 提交于 2021-01-28 03:20:22
问题 I need to create a query that returns only the rows which have the minimum values in a specific column. I have this results (example): Name | Description | Order ----------------------------- Name1 | A | 1 Name1 | B | 2 Name1 | C | 3 Name2 | A | 1 Name2 | B | 2 Name2 | C | 3 I want to get this results: Name | Description | Order ----------------------------- Name1 | A | 1 Name2 | A | 1 Basically, I need to select only the rows which have the minimum value in the column order . I'm stuck with

MySQL: sum values from subqueries

两盒软妹~` 提交于 2021-01-27 21:04:36
问题 Is it possible to sum two values from subqueries? I need select three values: total_view, total_comments and rating. Both subqueries is very complicated, so i don't wish duplicate it. My query example: SELECT p.id, ( FIRST subquery ) AS total_view, ( SECOND subquery ) AS total_comments, ( total_view * total_comments ) AS rating FROM products p WHERE p.status = "1" ORDER BY rating DESC 回答1: I would suggest using a subquery: SELECT p.*, (total_view * total_comments) as rating FROM (SELECT p.id,