subquery

count Query in sql

和自甴很熟 提交于 2020-01-16 18:22:06
问题 i have an issue with query 1st table (Master) Name :MainCategory with fields (Category_id,Category_name) 2nd Table (Transation) Name : Incident with fields (Incident_id,Category_id,subject,description) i want to query to count the appearance of category_id in the table Transation for eg result can be Category_name Number_of_Faults Hardware 10 Software 22 Network 17 thanks Kumar 回答1: Try this: SELECT a.Category_Name, COUNT(b.Incident_Id) Number_of_Faults FROM MainCategory a JOIN Incident b ON

Select grouped by column only, not the aggregate

岁酱吖の 提交于 2020-01-16 06:57:47
问题 In a MySql select statement involving aggregation, is it possible to select just the grouped by column without the aggregate? Basically I want to select IDs in subquery according to a criteria based on an aggregate, in this case the total payments to a client: select idclient, business_name from client where idclient in ( select idclient, sum(amount) as total from payment group by idclient having total > 100 ) ... but this fails with error Operand should contain 1 column(s) because the

Select grouped by column only, not the aggregate

岁酱吖の 提交于 2020-01-16 06:57:09
问题 In a MySql select statement involving aggregation, is it possible to select just the grouped by column without the aggregate? Basically I want to select IDs in subquery according to a criteria based on an aggregate, in this case the total payments to a client: select idclient, business_name from client where idclient in ( select idclient, sum(amount) as total from payment group by idclient having total > 100 ) ... but this fails with error Operand should contain 1 column(s) because the

PostgreSQL: Grouping then filtering table, with condition for nonexistence

こ雲淡風輕ζ 提交于 2020-01-16 04:15:07
问题 In PostgreSQL, I have a table that, abstractly, looks like this: ╔═══╦═══╦═══╦═══╗ ║ A ║ B ║ C ║ D ║ ╠═══╬═══╬═══╬═══╣ ║ x ║ 0 ║ y ║ 0 ║ ║ x ║ 0 ║ x ║ 1 ║ ║ x ║ 1 ║ y ║ 0 ║ ║ x ║ 1 ║ z ║ 1 ║ ║ y ║ 0 ║ z ║ 0 ║ ║ y ║ 0 ║ x ║ 0 ║ ║ y ║ 1 ║ y ║ 0 ║ ╚═══╩═══╩═══╩═══╝ I want to transform it in a query into this: ╔═══╦═══╦══════╗ ║ A ║ B ║ D ║ ╠═══╬═══╬══════╣ ║ x ║ 0 ║ 1 ║ ║ x ║ 1 ║ null ║ ║ y ║ 0 ║ null ║ ║ y ║ 1 ║ 0 ║ ╚═══╩═══╩══════╝ …such that: The input table’s rows are grouped by A and B, and

How to get second argument of Round() to work with columns?

馋奶兔 提交于 2020-01-16 04:13:10
问题 I've a simple table with an integer column: # setup table: create table t(x int); insert t select 1; The query select round(1.234, 1) from t returns 1.2 as expected. However, select round(1.234, x) from t returns 1.2000 . (It should return 1.2 , as per the docs.) Is this a bug? (Tested on version 5.5.10 and latest 5.6.24.) Or, is there any particular technical reason why columns cannot be used in the second argument of round ? How can we get round to work even while there are columns used in

“subquery returns more than 1 row” error.

柔情痞子 提交于 2020-01-15 08:58:27
问题 I am new to web programming and I'm trying to make a twitter-clone. At this point, I have 3 tables: users (id, name) id is the auto-generated id name of the user tweets (id, content, user_id) id is the auto-generated id content is the text of the tweet user_id is the id of the user that made the post followers (id, user_id, following_id) id is the auto-generated id user_id is the user who is doing the following following_id is the user that is being followed So, being new to sql as well, I am

Can SELECT, SELECT COUNT and cross reference tables be handled by just one query?

被刻印的时光 ゝ 提交于 2020-01-14 19:26:06
问题 I have a page that displays a list of projects. With each project is displayed the following data retrieved from a mysqli database: Title Subtitle Description Part number (1 of x) The total number of photos associated with that project A randomly selected photo from the project A list of tags Projects are displayed 6 per page using a pagination system As this is based on an old project of mine, it was originally done with sloppy code (I was just learning and did not know any better) using

How to optimize DELETE .. NOT IN .. SUBQUERY in Firebird

断了今生、忘了曾经 提交于 2020-01-14 08:44:10
问题 I've this kind of delete query: DELETE FROM SLAVE_TABLE WHERE ITEM_ID NOT IN (SELECT ITEM_ID FROM MASTER_TABLE) Are there any way to optimize this? 回答1: You can use EXECUTE BLOCK for sequential scanning of detail table and deleting records where no master record is matched. EXECUTE BLOCK AS DECLARE VARIABLE C CURSOR FOR (SELECT d.id FROM detail d LEFT JOIN master m ON d.master_id = m.id WHERE m.id IS NULL); DECLARE VARIABLE I INTEGER; BEGIN OPEN C; WHILE (1 = 1) DO BEGIN FETCH C INTO :I; IF

t-SQL to find Top 10 Records for Each Group

[亡魂溺海] 提交于 2020-01-14 08:12:26
问题 I am trying to figure out how to return the top 10 records for each group of Trans.TranSID. SELECT a.ABID, a.ABName, t.TranSID, SUM(IIF(TranTypeID = 'CO', td.Qty * CAST(td.Price AS money) * - 1, td.Qty * CAST(td.Price AS money))) AS TotalSales FROM Trans t INNER JOIN TransDetail td ON t.TranID = td.TranID INNER JOIN ABook a ON t.TranABID = a.ABID WHERE (t.TranDate BETWEEN CONVERT(DATETIME, '2012-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2013-01-01 00:00:00', 102)) AND t.TranTypeID in ('SO'

t-SQL to find Top 10 Records for Each Group

非 Y 不嫁゛ 提交于 2020-01-14 08:12:00
问题 I am trying to figure out how to return the top 10 records for each group of Trans.TranSID. SELECT a.ABID, a.ABName, t.TranSID, SUM(IIF(TranTypeID = 'CO', td.Qty * CAST(td.Price AS money) * - 1, td.Qty * CAST(td.Price AS money))) AS TotalSales FROM Trans t INNER JOIN TransDetail td ON t.TranID = td.TranID INNER JOIN ABook a ON t.TranABID = a.ABID WHERE (t.TranDate BETWEEN CONVERT(DATETIME, '2012-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2013-01-01 00:00:00', 102)) AND t.TranTypeID in ('SO'