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

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

1
vote
2answers
40 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
37 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
16 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
51 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
16 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
22 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
43 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
31 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
34 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
99 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
79 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
53 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
527 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
72 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
76 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
51 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
310 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
170 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
103 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
1k 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
100 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
59 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
2k 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
25 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
41 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
376 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
261 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
55 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
124 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
862 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
766 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
47 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
55 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 ...
1
vote
1answer
312 views

Bitmap Heapscan is slow

I have 2 tables: mailings and form_responses. The form_responses table has a foreign key with the mailings table, so a mailing has one-to-many form_responses. What I want to do is calculate mailing ...
1
vote
1answer
38 views

COUNT for instances with no value

I have a query that looks for and counts orders that have been written in a medical record that have a specific display group SID. I have 8 medical centers and I want a count for all of them even if ...
0
votes
0answers
37 views

Counting the number of rows as a result of the filtered with HAVING

For example, there are two tables: CREATE TABLE `products` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `price` decimal(10,2) unsigned NOT NULL, `quantity` smallint(5) unsigned NOT NULL, `name` ...
0
votes
1answer
3k views

Select unique value pairs that occur (count) more than once inside a table

How can I retrieve an row with unique value pairs that occur more than once... Heres an example of what I'm what I am trying to.. SELECT column_fk_id1, column_fk_id2, COUNT(distinct column_fk_id1, ...
0
votes
1answer
53 views

Looking for another method to count in a single query

So, I found this article is very useful Join two tables and return data and count in a single query and here Single query to return counts over different IDs in a single record for some IDs or ...
1
vote
2answers
86 views

Count the total occurences for each day in given dates

I have this table and don't enough SQL to make a query to achieve the result shown in the image below. CREATE TABLE myDates ( id integer NOT NULL, startDate date NOT NULL, endDate date ...
1
vote
1answer
206 views

Postgres row count

I have a table with a row count of 985 records. But it is really slow to query this table. # select count(*) from movie; count ------- 985 Then I perform query using EXPLAIN ANALYZE The row ...
0
votes
2answers
49 views

Keep count in temporary variable for later use in query

I have used WITH query for this as below WITH my_count AS ( SELECT count(emp_name) FROM employee WHERE joined_date < '2017-11-30'::Date AND region = 'UK' ) INSERT INTO ...
0
votes
0answers
25 views

Count number of times two columns are the same in a subquery

I need to display a count, within the query below, of the number of times the current record has appeared within a sub-query with specific values. The query I have right now is below. I want to find ...
2
votes
1answer
87 views

Calculate quantity of tickets based on how many reserved using sub queries

I'm designing a database that will hold ticketing information. The 3 tables I use to track ticketing are tbl_ticket_type to the types of events an event can have tbl_ticket_count to keep track of ...
-2
votes
1answer
100 views

Get multiple count field without using inner query [closed]

Sql fiddle link: http://sqlfiddle.com/#!9/4a58b2 Tables CREATE TABLE `company` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT ...
0
votes
1answer
87 views

Lock the whole table and use transaction

To perform some large operations, I'd like to lock (write) the tables beforehand, and still be able to use transactions. On this page (comment at the bottom) it's recommended to use SELECT ... FOR ...
0
votes
1answer
2k views

Oracle: Get the row count in a cursor when using a OPEN-FOR-USING statement

Within an Oracle procedure, after opening a cursor for a select statement, I fail to find a mean to count the number of rows fetched. OPEN mycursor FOR SELECT * FROM TABLE; -- mycursor%ROWCOUNT is ...