Questions tagged [conditional-aggregation]
86 questions
0
votes
1 answer
Shorten queries with multiple joins
Is there a way to shorten this query?
This query returns the result I want but I feel like this is to long. Are there tips to make an efficient query wilth miltiple joins?
SELECT home.team_id, home.name,…

jee
- 13
- 2
0
votes
0 answers
MySQL iterate information from one table in to 2 columns in query
I have a database which holds maintenance reports for unique units that I need to pull information from in a single query. My issue is the convoluted way the information is stored.
SELECT date(reports.created_timestamp) as Date,
…

Ikthezeus
- 95
- 5
0
votes
1 answer
SQL grouping unique occurrences by date
I am looking for a MySQL query to count how many people visited a web-app. I have a database storing information about visitors, they type and when they tried to login:
+-----+-----------+--------+---------+---------------------+
| id | person_id |…

tomasz74
- 16,031
- 10
- 37
- 51
0
votes
3 answers
Oracle sql combine table and do counting
How to count the total project participation (it means the project count) for the project Name Lakers and participate in Client meeting between 12/25-27/2022 for all employee display with the UserAc and their total PJCount?
Also, if the employee…

coder
- 29
- 6
0
votes
0 answers
How to transform a row in a column based on duplicated data?
I have been trying to do this since a while now but haven't been successful so far.
I'm currently trying to translate a row into a single cell of another row in SQL Server.
Example:
I have 4…

Laernyl
- 61
- 6
0
votes
2 answers
SQL table join - 1 column data split to 2 columns depending on another column
I have some data in x2 tables (d and i) that I want to join and split 1 table's data (i) in to 2 columns depending on data in another column of table i.
Example of the data I have is (I have cut out multiple fields as both tables hold a lot more…

Ikthezeus
- 95
- 5
0
votes
1 answer
Weird MySQL JOIN where I want to include matches AND no matches from join
I have a need to get a specific result for a query... I believe the solution is a self-join of the same table... but it's not working as I would expect.
A bit of background: the "timeframe" column denotes a time increment dataset. "15m" is a dataset…

thequeue
- 67
- 1
- 8
0
votes
1 answer
Optimize Nested MySQL Query
My Application is database-driven. I have written a query that is taking more than 15-20 minutes to execute. I have proper indexes, but still, it is taking too much time to execute. Below is my query, if anyone can help me optimize it, I will be…

Sharique Anwer
- 129
- 2
- 13
0
votes
2 answers
How to improve SQL query performance containing partially common subqueries
I have a simple table tableA in PostgreSQL 13 that contains a time series of event counts. In stylized form it looks something like this:
event_count sys_timestamp
100 167877672772
110 167877672769
121 …

Peter K.
- 517
- 3
- 19
0
votes
2 answers
SETUP multiple sums with multiple date ranges
Here is the query I am trying to complete.
SELECT salesrep,
SUM(lines_total) AS OTSUM,
SUM(order_type = 'NEW') OTNEW,
SUM(order_type = 'REPEAT') OTREPEAT,
SUM(lines_total
WHERE order_closed BETWEEN '2020-11-01' AND…

flying23x
- 31
- 1
- 7
0
votes
2 answers
SUM Values within SQL based on a column
I have a table like below:
I want to transform the data so that the end product actually looks like this:
This is easily done within Excel but i would like to be able to do this via SQL so i can automate a report.
I have tried the below code…

JK1993
- 148
- 11
0
votes
2 answers
Sum a column when another column is true in SQL Server
I have a table with a a name, a score and a flag:
Name Score Flag
----------------------
Fred 10 true
Fred 10 false
Bob 10 false
I'm trying to group by the name, and in two columns get the sum of the score, and the…

user43107
- 365
- 3
- 13
0
votes
1 answer
Conditional aggregation efficiency
Let us have two tables.
A(id int primary key, groupby int, fkb int, search int, padding varchar(1000))
B(id int primary key, groupby int, search int)
They are created using the following scripts. The first table is large (1M rows) and the second…

Radim Bača
- 10,646
- 1
- 19
- 33
0
votes
1 answer
How do I transpose or pivot subgroups into to a single row?
I have a group-by-top-n-results query that is shown in the example input data. The subgroups (grouped by ID) are limited to the top 10 results and they are sorted ASC by rank. How do I go from the input example to the output example?
I was thinking…

italiansoda
- 462
- 8
- 12
0
votes
2 answers
How to use sum (case ... when ... then ...) properly?
I want to sum rows with a specific value as a particular column.
Earlier I did something like this:
SELECT date AS 'Date',
sum(CASE license_id WHEN 'a' THEN data.Amount ELSE 0 END) AS 'a',
sum(CASE…

KnechtRootrecht
- 493
- 1
- 4
- 18