Questions tagged [count]

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

Filter by
Sorted by
Tagged with
1
vote
1answer
21 views

Count nested joined table records only if the table associated via bridge table have two records with given names

I have the main table on which I have to apply count. I have two bridge tables and two nested tables for has_many relation. You can generate tables and bridge tables with below code and also insert ...
0
votes
2answers
19 views

How to check if staff member has end date with a Null value

I have a database that records employees tasks throughout the organisation and when they finish etc., I need to identify if each employee, has at least one row with a null value in either finish. ...
0
votes
1answer
19 views

Count percentage of column with GROUP BY

Consider the following query: SELECT country , count(id) AS "count" FROM lead WHERE date_part('year', lead.since) = date_part('year', CURRENT_DATE) GROUP BY "country" ORDER BY "count" DESC ; ...
1
vote
1answer
29 views

SQL (Redshift) SELECT COUNT from CSV column

Hi exchange community, Problem statement and details: I am trying to query a column that contains CSV data to produce a count of instances of each value in the table. There are a total of 14 ...
0
votes
1answer
53 views

SQL count(*) grouped by date range

I have a table as follows: DROP TABLE IF EXISTS #PersonnelRecord; CREATE TABLE #PersonnelRecord ( person_id int NOT NULL, company_id int NOT NULL, entry_date date NOT NULL, exit_date ...
0
votes
1answer
41 views

List top N rentals for each month

I'm trying to work out the SQL to list the top 5 rentals for each month on MySQL or SQL Server. The database is the Sakila Sample database and the table is rental. Here are the first several rows in ...
-1
votes
1answer
25 views

SQL joining three tables and displaying column with values zero count

Im trying to display all the city names and their counts of events where the event price = 0 create table event (eventID decimal(12) not null primary key, AddressID decimal(12) not null, eventName ...
0
votes
0answers
11 views

MYSQL - Retrieve running total over time of records in foreign table for each record in table

I have a table group & a table members. A group has many members. A single member record can belong to a single group. The members table has a foreign key/column group_id. I need to retrieve, ...
0
votes
1answer
19 views

Trying to Count Multiple Columns SQL

I'm trying to count multiple columns in a select statement for a class I'm in. Everything is coming from the same table. I want to know how many times that person paid something and how many times ...
0
votes
1answer
28 views

How can I rearrange these two queries to get the second column from inside the IN()

How can I get the random.id column from inside this first select select id, name from random where id IN(1,2,3,4); +----+-----------+ | id | name | +----+-----------+ | 1 | Frank | | 2 | ...
0
votes
1answer
15 views

MySQL : count occurences on different columns in a same query

I've got his table: +----+------+---------+ | id | user | upgrade | +----+------+---------+ | 0 | A | NULL | +----+------+---------+ | 1 | B | NULL | +----+------+---------+ | 2 | ...
0
votes
1answer
24 views

Selecting difference between two tables returns different number of results than counts of same two tables

I'm using SQL Server and when I run SELECT * FROM TABLE1 EXCEPT SELECT * FROM TABLE2 it returns 1610 entries. When I query SELECT COUNT(*) FROM TABLE1 SELECT COUNT(*) FROM TABLE2 I get 6399 and ...
0
votes
1answer
23 views

postgresql cumulative counts in date range

I'm trying to get the cumulative count of rows (by group_id) between two dates that represent a time period where the row was active. I have a table like this: group_id | id | type | start_date ...
0
votes
0answers
19 views

How can I have multiple group by in MongoDB aggregation?

My sample data is: [{ "company": "apple", "last_view": "2020-01-01" }, { "company": "google", "last_view": "2020-01-01" }, { "company": "google", "last_view": "2020-01-01" }] Now to ...
0
votes
1answer
12 views

SELECT COUNT in same table based on current row value within a single query

Let's say I have a table like below uid | value | date_added ----------------------------------- 14152 | NA4156 | 2019-03-01 14153 | EA4656 | 2019-03-02 14152 | EA4656 |...
-2
votes
1answer
23 views

SUM the occurrences (COUNT) of 2 table [closed]

i have 2 databases with same columns [id, name] and many many rows. I want to make query separatly and then make a union. This because the extremely dimension of the 2 databases. SELECT id, count(...
0
votes
1answer
25 views

Calculate products count based on category tree table

It is necessary to calculate how many products were created in each category, of course, that the products created in a child must be considered for the parent. How can i do that SQL/Postgresql? ...
1
vote
2answers
38 views

WHERE clause not preventing return of row but changing values for aggregate function (here: count)

I have recently discovered a PostgreSQL behavior which is strange and problematic in my opinion. Consider this simple query SELECT 'something confidential' WHERE FALSE; The nonsense clause ...
0
votes
1answer
24 views

MySQL COUNT based on a condition [duplicate]

I have a MariaDB database, in which I have this table: ID | Item Name | Item replaced | ----------------------------------------- 0 | Item 1 | Replaced 1 | Item 2 | 2 | Item 3 | ...
0
votes
1answer
30 views

SELECT with a GROUP BY column?

The situation is like this: I have a table that has one row for each copy of a letter, which also contains the letter ID. I want a single query that outputs each letter copy row with the letter copy ...
0
votes
1answer
23 views

MySQL query help

I need a query that returns the number of liked messages and orders them by the number of likes (from most to least). I have tried: SELECT users.user_id, messages.author, messages....
-1
votes
2answers
32 views

How to count field with multiple values in MYSQL [closed]

I have a MySQL database and Table1 has a column that can get four distinct values e.g 1,2,3,4. Is it possible to create one query to count 4s and all the other values together? For example ...
0
votes
1answer
21 views

How can I find the difference between queries that return multiple rows?

I have two tables, website and page, that look like such: website id | name ------------ 1 | site 1 2 | site 2 3 | site 3 page id | name | site_id | deleted ---------------------------------- 1 ...
0
votes
1answer
46 views

trigger to update row number

I have a MariaDB table id | key | year | .... | position 1 | key1 | 2004 | ... | 3 2 | key1 | 2011 | ... | 2 3 | key1 | 2017 | ... | 1 4 | key2 | 1994 | ... | 2 5 | key2 | ...
1
vote
2answers
35 views

How to get sum of 3 sub count queries and group them using an attribute

I have 3 queries as follows: For the date 2019-12-02, I am calculating 3 type of values. The total value for 2019-12-02 must be the sum of these 3 queries. Q1 SELECT count(*) FROM ...
-1
votes
1answer
24 views

MySQL - How to count unique instances of a value in relation to another column's value

I have a table with the following columns and values: I need to count how many times a value is within the "pickup_customer_ref" column, however I don't want to count the value twice if it is within ...
-4
votes
1answer
33 views

can I be helped? my querey should = something like .000534 however I get 0

My query result should be 0.000534, but I'm getting 0. Please help me. psql -Upostgres -dcans -c \ 'select (select COUNT(*) FROM brd.gaf_info where ...
-1
votes
2answers
395 views

db2 select statement from a table name where the table name is a variable [closed]

I need to select all the table row count from a db2 database. I have a query to select all the schema and table name: select rtrim(tabschema)||'.'||rtrim(tabname) as tableName from syscat.tables ...
0
votes
1answer
26 views

Use plpgsql to count records selected for output AND output those records

My query SELECTs 1 row from table1, which has a number of fields plus one very long text field. It JOINs with a second table2, that may have many matching corresponding rows. I would like to be able ...
1
vote
2answers
36 views

How can I have an aggregate function in the WHERE clause?

Using PostgreSQL 9.3.24 Issue Just simple operation, but I don't know how to do that best simplest way. How can I display results only when sum of available_counts for same product_id is 0 and ...
0
votes
1answer
20 views

How to optimize count(*) using only foreign key index on PostgreSQL

I have a web app that extensively uses data tables that connect to the database to fetch data. The problem though, it always needs first to count the total results before it could paginate the data by ...
1
vote
1answer
30 views

Filtering on two separate colums

The schema I'm using MySQL Consider a table with columns id uuid type char is_active Boolean Here are some facts about the data in this table type is either 'A' or 'B' There will be a maximum of 2 ...
0
votes
1answer
40 views

Count up numbers from a specific column [closed]

Hi there was wondering if someone could help me out, and can give me a little code example. i have a table named "projecten", and columns named "pstatus and pafgifte" these columns have numbers only. ...
2
votes
3answers
182 views

Query to get counts of values per column

I have a big table of vendor-supplied data (that I can't change around much) with about 315 columns. I suspect that many of the columns are not being used (or at least not consistently). I'd like a ...
1
vote
1answer
68 views

Postgres: Getting the “partition number” instead of row_number

Figure a table with the following data, how to exclude 'alone' values? id; data (1, 'foo'), (2, 'foo'), (3, 'foo'), (4, 'bar'), (5, 'bar'); (6, 'jak'); I try with this: select id, data, row_number()...
0
votes
1answer
558 views

Sub Query with group by returns more than 1 row Issue

I am facing the issue of subquery returns more than 1 row cause I am using group by in the subquery and outer query both to get the desired result. This is my query SELECT Count(*) ...
0
votes
0answers
19 views

select rows and calculate number of rows based on parent column value

I have a total of three tables with a many-to-many relationship. 1.) users id | email ----------- 1 | usera@example.com 2 | userb@example.com 3 | userc@example.com 2.) accounts id | userId | ...
2
votes
1answer
49 views

More efficient group by to count items in group

My query is: SELECT COUNT("EventType"."id") AS "eventCount", "EventType"."id" AS "EventType.id" FROM "events" AS "Event" INNER JOIN "event_types" AS "EventType" ON "Event"."eventTypeId" = "...
0
votes
1answer
26 views

Group by unique items in an array column and count records that contain each item

I have a table that looks like this: records ------------------------------------- id items[] ------------------------------------- 1 {toaster,piano} 2 {car} 3 {car, toaster} 4 {tv}...
0
votes
1answer
244 views

Optimize slow COUNT in MySQL subquery

I have the following query that takes minutes to complete: SELECT DISTINCT Designation, Model, ( SELECT COUNT( Equipment.EquipmentID ) FROM Equipment INNER JOIN EquipmentDesignation_Vw ...
0
votes
4answers
182 views

Postgresql extremely slow count (with index, simple query) [duplicate]

I need to run these simple queries on a table with millions of rows: SELECT COUNT(*) FROM "subscriptions" WHERE "subscriptions"."project_id" = 123; SELECT COUNT(*) FROM "subscriptions" WHERE "...
-1
votes
1answer
37 views

Month on Month count to determine variance of table growth

Good Afternoon Team, Hoping you can help me . Every month i get a count of records associated to a table. What i would like to do is month on month calculate the % variance (between jan vs feb, feb ...
2
votes
2answers
118 views

Summing the count of two columns in postgres that contain the same value

I want to sum the total number of occurrences of one value across two columns in the same table. So, an example would be: id |node1 |node2 1 | 111 | 123 2 | 122 | 124 3 | 111 | 125 4 ...
0
votes
2answers
26 views

Count two different columns from two different tables and throw into a single variable

I have two tables in my project: "cats" and "items". Every single item in "items" table has the value "item_cat" (column), which represents the ID of actual category, where that item is assigned to. ...
0
votes
1answer
48 views

How to get the customer payment count for each row in MYSQL

I have a payment system, and want to display the counter of each customer payment (first, second, third ...) and if it's the fist payment, display (New). How can i do this in one query ? SELECT c....
0
votes
0answers
53 views

MySQL Count rows of multiple tables

CREATE OR REPLACE VIEW organizedevent AS SELECT o.organizerID AS "Organizer ID", o.organizerName AS "Name", o.contact AS "Contact", COUNT(e.eventID) AS "...
0
votes
0answers
30 views

How to implement row count for REST API?

I’m building a REST API backed by PostgreSQL. I'm encountering something I don’t think is very niche, but I’m having trouble finding any discussion of it. Good REST API practice dictates that if a ...
0
votes
1answer
23 views

MySQL Count(Table1.ColId) Where IN (any of Table2.Col1 ,Table2.Col2)

I have 2 Tables Table 1 has Id, Name Table 2 has Id, Col1_IdVal, Col2_IdVal,Col3_IdVal,Col4_IdVal I would like simple solution for Count(T1.Id) WHERE T1.Id In (T2.Col1_IdVal,T2.Col2_IdVal,T2....
0
votes
2answers
14 views

Select value based on a test

I have two tables user_session_logs and blacklisted_ips. The first one for logging access logs and the second one is for blacklisted ips, and both tables have a column named ip_address. I need to ...
0
votes
1answer
47 views

How to query results from a huge postgres database?

I am trying to query a few tables in a 125GB Postgres database. I find a hard time getting the results back quickly! Even to find unique rows of a column it takes more than 10 minutes. Either I use a ...

1
2 3 4 5
8