Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [count]

COUNT : an aggregate SQL function that is used to count the number of rows.

0
votes
1answer
31 views

How to get Average value from one row if have zero or null values in mysql

I'm new in mysql. I want to ask something about get average value from one row in mysql when have a null or zeros value on them. the image below assuming my table And I want to get the average from ...
0
votes
1answer
12 views

MySQL Subquery count

I need to find how many resources are available in depot when transit arrives. I need to have transit name and resource count on result. I'm using MySQL. I wrote this but it gives me error when adding ...
0
votes
1answer
12 views

Counting and grouping over multiple OUTER JOINs

I'm creating a view of advertisement logs (impressions, clicks, and clicks per impression). I have a simple table structure and some working queries but I'm having some trouble composing these into a ...
0
votes
1answer
26 views

mysql query to fetch group wise count

I am having mysql tables like below tbl_source_1 SRC_ID_1 | SOURCE_NAME_1 1 | ABC 2 | DEF 3 | GHI tbl_source_2 SRC_ID_2 | SOURCE_NAME_2 1 | NAME_1 2 | NAME_2 3 ...
1
vote
2answers
56 views

How can I get the percentage of the table the groups represent?

Given a query like this, CREATE TABLE foo(grp) AS VALUES ('a'),('a'),('b'),('b'),('b'),('c'),('c'),('c'),('c'),('e'); I can use GROUP BY to aggregate into a,b,c,e, but how can I return the different ...
0
votes
1answer
30 views

JOIN tables and return count even if it is 0

I have this query: select settings.customer_id, current::jsonb->'alarms' as alarms, from settings join (select customer_id, current::jsonb->'alarms' as alarms from settings) as res on res....
1
vote
1answer
64 views

Count all rows for each DISTINCT

How can count all rows of WHERE statement: SELECT DISTINCT column1 FROM table COUNT WHERE date::text LIKE '%2014%'; My output: column1 ======= info1 info2 info3 Desired OUTPUT: column1|...
1
vote
0answers
19 views

MySQL count rows of multiple tables for each user

I basically need this with some adjustments, I just can't seem to figure it out how to do it. I need to sum the rows of two tables, for example: SELECT student.StudentID, student.`Name`, COUNT(...
2
votes
1answer
29 views

where clause after limiting results

I have a question about getting results after limiting the rows. I don't have much experience with MySQL, but I'm trying to get results from a database where I need to do the where clause after ...
0
votes
1answer
40 views

Efficient way to check for number of records more than a limit from a table in SQL Server

I came across queries that can most efficiently get the total sum of the records in the table. However, i want to group the records in a table by a column and check if the count of the records is ...
0
votes
2answers
36 views

Count all products (with qty > 0) group by all brands. But get aslo count zero for brand which have 0 products

I have table: brands brand_id name 1 Apple 2 Sony 3 Nokia 4 HTC products id brand_id qty 1 1 3 2 3 0 4 1 1 2 2 1 I need to ...
0
votes
1answer
32 views

Get two row counts from the same table [closed]

What I am trying to achieve is run both queries (below) at the same time. From one table rvsessions I need to get the count for the total amount of sessions(which is achieved by the number of times ...
0
votes
1answer
24 views

MySQL, would like to get both a field and a count inside GROUP_CONCAT

I can get a list of statuses that a tracking ticket has had, as well as the id of those statuses, as follows: SELECT t.id, t.name, GROUP_CONCAT( CONCAT(s.id, ':', s.name) ORDER BY s.id) AS ...
1
vote
2answers
51 views

SELECT i.c.w. a COUNT and INNER JOIN gives different results with string literals and numeric values when they are used in a WHERE clause

I have a MySQL query: SELECT p.informationProvider as pGLN, p.productID, p.productStatus FROM tblproducts AS p WHERE -- informationProvider is of type varchar(14) p....
-1
votes
2answers
45 views

MySQL Query can we count rows using php to reduce time of count(*) in a query or there is no difference?

Having the following queries, where the first query is returning a field of number of rows found, and the second returns the actual rows: First Query: SELECT count(*) as total FROM individual i ...
0
votes
1answer
18 views

Counting entries of a unique entry of another field and concatenate the result with the unique field in a new column [closed]

I want to create a query that will search the table and find the following: If a POL_N entry has ENDO_N entries then the COUNT ENDO_N field will start counting the ENDO_N entries and will get the ...
0
votes
0answers
119 views

MySQL COUNT in GROUP BY with LEFT JOIN table

Query I've tried: SELECT UPPER(section.name) AS SECTION, COUNT(employee) AS 'EMPLOYEE COUNT', SUM(salary.amount) AS SALARY FROM employee LEFT JOIN section ON section.id = employee.section LEFT ...
0
votes
1answer
18 views

select data from related tables and display their counts in the relating table

I have a table which stores data about an inspection. Inspections are carried out in a particular company belonging to an industry, located in a region. the inspections table has the industry_id field ...
-1
votes
1answer
23 views

Count comma seperated values in mysql ORDER BY clause

I have a table name products: id - pro_name - ip - date_created 1 - abc1 - 27.106.121.23,27.106.121.86 - 2018-08-15 13:32:00 2 - ...
0
votes
2answers
48 views

Calculate ticket sales by using SUM in SELECT clause

In this ticketing setup on dbfiddle, I am trying to calculate the ticket sales for a particular event by multiplying the ticket price and the total number of tickets sold for that event, based on ...
2
votes
0answers
34 views

MariaDB running indefinitely on inner join with count

I am currently experiencing a very, very weird issue with a query in MariaDB. Our server experiences a "504 Gateway Timeout" quite often after a database upgrade, and I was investigating the issue. ...
1
vote
0answers
62 views

How to apply a condition on “count over partition”?

I'm using Oracle 11g. Assuming multiple consecutive entries (sorted by datetime) with the same place_id represent a single visit. With this table: datetime_id | user_id | place_id and I want to ...
0
votes
3answers
102 views

SQL Count function returning inaccurate results

I am trying to write a query in oracle to list the number of records contained in a single table filtered by multiple columns in the tablespace. Here is the query syntax I have come up with: SELECT ...
3
votes
2answers
95 views

MySql concurrency with COUNT

I Have a table with user_id and product_id. For my purpose, a particular user is allowed up to 5 rows only. To enforce this, I created such a SQL statement: INSERT INTO the_table (product_id, ...
2
votes
1answer
112 views

SQL query to get the user, counting the minimum number of records

I have an table with some records and a column LAST_EDIT_USER. If I want to select all users with the number of appearances for every user, I would do SELECT LAST_EDIT_USER,COUNT(LAST_EDIT_USER) ...
1
vote
2answers
36 views

Basic question about grouping and counts

I'm having trouble coming up with a query in MySQL. Suppose we have a table that logs an action by a user. User1 is a Doctor and is logged several times. User2 is a Doctor and is logged once. User3 ...
1
vote
3answers
539 views

SQL Server COUNT() seems to be ignoring WHERE [closed]

I am trying to set up a SQL Server query that lists the count of people signed up for tours during a conference event and then displays the total sum. This is my SQL code that is coming close, but it ...
-2
votes
1answer
174 views

multiple counts of the same column SQL

currently using the following SQL to get multiple counts from the same column but my results are returning all the same SELECT act.OWNER_LOGIN, COUNT(CASE WHEN act.X_ACTIVITY ='Call' THEN 1 ELSE 0 ...
3
votes
3answers
88 views

Index scan on UNIQUE index for count(*)

I've got a table t with ~23 million rows (4248 MB in size). There's a column row_id in it, with a not null constraint. And a unique index p1 on t(row_id). When I do select count(*) from t to count ...
0
votes
4answers
52 views

Help for grouping a field and taking total sum

I have a sorted data in a table F1 F2 F3 F4 A M X 1 A N Y 2 A P X 3 B M X 3 B N Y 1 B P X 5 but I want the sub count of values in F1 column for every row. ...
0
votes
1answer
29 views

SQL Server count value of column [closed]

I call the table is [vDemo] []1 i want count MaLC by TenNV, result is if record 13 MaLC is L then Count(x) = 16, Count(L) = 1
2
votes
3answers
37 views

Is it possible to combine these three join queries into one?

I would like to combine the counts of the stats by different hit_types into one query. Is that possible? MariaDB [db]> select allurls.id, count(s1.id) from allurls inner join stats s1 on allurls....
2
votes
2answers
452 views

COUNT(), GROUP BY and GROUP_CONCAT() together on MySQL

My question is quite simple. I just want to count events from each client and present them in a comma separated single row. I have this table: +----+----------------+ | id | event | +----+--...
0
votes
1answer
321 views

SQL query to count with subtraction from count of other sql table [closed]

I have 2 tables as below I column is common to both. Table 2 has FK from Table 1. I need to get the count of ID_Tag per BoxNo as result. Condition List all BoxNo and subtract the count if the ID_Tag ...
1
vote
1answer
237 views

How to get count from 2 sql tables with conditions on count

I have 2 sql tables as below only one column is common to both I need to get the count of all ID_Tag from Table 1 which appear in Table 2. Result condition List all ID_Tag with Case No from table ...
1
vote
2answers
2k views

Multiple COUNTs over the same column

I have a table (PostgreSQL 9.6) containing 2.6 million+ timestamped rows associated with account identifiers, and for any given identifier, I want to count the total number of occurrences as well as ...
1
vote
5answers
104 views

Count Rows where value differs

I have the following dataset in MS SQL Server: Name Pattern ------- ------- Peter DRF Peter DRF Peter BBB Peter DRF Peter CCC Peter CCC Sue DRF Sue CCC Sue DRF ...
1
vote
2answers
65 views

Postgres - How to get Multi-Count Query using a Where Clause for a Join Table

I have the following query that gets the counts across multiple tables that are joined: select count(distinct e.planningitemdata->>'receiverDuns') as supplierOrCustomer, count(distinct ...
0
votes
2answers
44 views

How to get a COUNT for both IN and NOT IN a table

I have a table that contains a master list of items and an associated table that contains a unique text string only on specific values (determined by the application that inserts the data). Example: ...
1
vote
1answer
3k views

Counting unique (distinct) users per day

I am new topsql and am interested in counting the number of unique first time users per day. My table only has two columns- user_id (there can be multiple uses by one user per day) and start_time ...
1
vote
0answers
27 views

problem using count in trigger phpmyadmin

how to record the number of column lastbuy_idrbch and lastsell_idrbch from table idrbch in my new table (test) using trigger and count function ?
1
vote
1answer
42 views

Count how many records belong to 2 categories in a n to n relation

I have a database containing articles whose schema is more or less the following: COLLECTIONS ( PK id ) > 1 : n > TOPICS ( PK id, FK collection_id ) > n : n > TOPICS_TO_ARTICLES( FK ...
1
vote
2answers
473 views

Using COUNT in triggers before insert into table

I am new to MySQL and am struggling with triggers. Say I have a table called members with the following data MemberID(PK) (INT) | Name (VARCHAR)| 1 | Barry | and another ...
3
votes
2answers
309 views

Getting the exact count of rows in Postgres database

Is there any way to get the exact row count of all tables in Postgres in fast way? Count(*) seems to run very slowly for larger tables. Is there a way to get an exact count from Postgres statistics ...
0
votes
0answers
59 views

Why query plans is different for queries with the same WHERE Clause?

We use PostgreSQL. We have TABLE_A in our database table has a lot of rows - 500 millions table has a several columns: COLUMN_ID, COLUMN_2, COLUMN_3, COLUMN_4 COLUMN_ID is primary key so it is NOT ...
1
vote
2answers
135 views

Count even deleted rows

Is it possible to count even deleted rows in a SQL Server table? Let's say we have a table Contacts. I'll add 5 new contacts, and hit count on it. It should return value of 5. Now, when I delete a ...
4
votes
3answers
1k views

Speeding up Count(*) on large tables

We're using a Vendor App running on SQL Server Enterprise, and it has a rather annoying quirk of executing COUNT statements on the Items table while processing most financial documents (orders, ...
0
votes
2answers
900 views

SELECT COUNT of multiple column under the same table

Im blowing my brain on this one. I think I am making this harder than I need to. Im trying to run a mySQL query to SELECT a COUNT of members which have any values in any column. my example table is as ...
0
votes
1answer
48 views

How to speed up counting and filtering on table with a lot of columns and filter conditions?

I have TABLE_A in my database that has a lot of columns. Let's say there are 100 columns: COLUMN_1, COLUMN_2, COLUMN_3, .. COLUMN_100 There are so many columns not because of denormalized design, ...
1
vote
1answer
67 views

How to display all the dates in the date-range having group by dates?

Suppose I have a table called “products”: Productive product-name product-quantity created-date 111 ABC 10 1/10/2018 222 XYZ 20 ...