Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using EXISTS within a GROUP BY clause

Is it possible to do the following:
I have a table that looks like this:

declare @tran_TABLE TABLE(
EOMONTH DATE,
AccountNumber INT,
CLASSIFICATION_NAME VARCHAR(50),
Value Float

)

INSERT INTO @tran_TABLE VALUES('2018-11-30','123','cat1',10)
INSERT INTO @tran_TABLE VALUES('2018-11-30','123','cat1',15)
INSERT INTO @tran_TABLE VALUES('2018-11-30','123','cat1',5 )
INSERT INTO @tran_TABLE VALUES('2018-11-30','123','cat2',10)
INSERT INTO @tran_TABLE VALUES('2018-11-30','123','cat3',12)
INSERT INTO @tran_TABLE VALUES('2019-01-31','123','cat1',5 )
INSERT INTO @tran_TABLE VALUES('2019-01-31','123','cat2',10)
INSERT INTO @tran_TABLE VALUES('2019-01-31','123','cat2',15)
INSERT INTO @tran_TABLE VALUES('2019-01-31','123','cat3',5 )
INSERT INTO @tran_TABLE VALUES('2019-01-31','123','cat3',2 )
INSERT INTO @tran_TABLE VALUES('2019-03-31','123','cat1',15)


EOMONTH     AccountNumber   CLASSIFICATION_NAME     Value
2018-11-30  123                     cat1                10
2018-11-30  123                     cat1                15
2018-11-30  123                     cat1                5
2018-11-30  123                     cat2                10
2018-11-30  123                     cat3                12
2019-01-31  123                     cat1                5
2019-01-31  123                     cat2                10
2019-01-31  123                     cat2                15
2019-01-31  123                     cat3                5
2019-01-31  123                     cat3                2
2019-03-31  123                     cat1                15

I want to produce a result where it will check whether in each month, for each AccountNumber (just one in this case) there exists a CLASSIFICATION_NAME cat1, cat2, cat3.
If all 3 exist for the month, then return 1 but if any are missing return 0.

The result should look like:

EOMONTH     AccountNumber   CLASSIFICATION_NAME
2018-11-30    123                   1                               
2019-01-31    123                   1                       
2019-03-31    123                   0   

But I want to do it as compactly as possible, without first creating a table that groups everything by CLASSIFICATION_NAME, EOMONTH and AccountNumber and then selects from that table.
For example, in the pseudo code below, is it possible to use maybe an EXISTS statement to do the group by?

SELECT 
    EOMONTH
    ,AccountNumber
    ,CASE WHEN EXISTS (CLASSIFICATION_NAME = 'cat1' AND 'cat2' AND 'cat3') THEN 1 ELSE 0 end 
    ,SUM(Value) AS totalSpend
FROM @tran_TABLE
GROUP BY 
    EOMONTH
    ,AccountNumber
like image 759
jmich738 Avatar asked May 16 '19 08:05

jmich738


People also ask

Can we use exists in SELECT statement?

The result of EXISTS is a boolean value True or False. It can be used in a SELECT, UPDATE, INSERT or DELETE statement.

How do you use exists instead of in?

IN can be used as a replacement for multiple OR operators. To determine if any values are returned or not, we use EXISTS. 2. IN works faster than the EXISTS Operator when If the sub-query result is small.

How do you use exists clause?

The SQL EXISTS OperatorThe EXISTS operator is used to test for the existence of any record in a subquery. The EXISTS operator returns TRUE if the subquery returns one or more records.

What is the difference between having and GROUP BY clause?

The GROUP BY Clause is used to group rows with same values. The GROUP BY Clause is used together with the SQL SELECT statement. The SELECT statement used in the GROUP BY clause can only be used contain column names, aggregate functions, constants and expressions. The HAVING clause is used to restrict the results returned by the GROUP BY clause.

What happens if there is no group by clause in SQL?

The ORDER BY clause then sorts the rows within each group. If you have no GROUP BY clause, then the statement considers the entire table as a group, and the ORDER BY clause sorts all its rows according to the column (or columns) that the ORDER BY clause specifies. To illustrate this point, consider the data in the SALES table.

What is the use of group by in SQL?

The GROUP BY clause is a SQL command that is used to group rows that have the same values. The GROUP BY clause is used in the SELECT statement. Optionally it is used in conjunction with aggregate functions to produce summary reports from the database.

What is the difference between distinct clause and GROUP BY clause?

The distinct clause is used to filter unique records out of the duplicate records that satisfy the query criteria. The Group by clause is often used to arrange the identical duplicate data into groups with the select statement. This clause works with the select specific list of items, for that we can use HAVING, and ORDER BY clauses.


1 Answers

You could emulate this behavior by counting the distinct classifications that answer this condition (per group):

SELECT 
    EOMONTH
    ,AccountNumber
    ,CASE COUNT(DISTINCT CASE WHEN classification_name IN ('cat1', 'cat2', 'cat3') THEN classification_name END) 
          WHEN 3 THEN 1 
          ELSE 0 
     END
    ,SUM(Value) AS totalSpend
FROM @tran_TABLE
GROUP BY 
    EOMONTH
    ,AccountNumber
like image 194
Mureinik Avatar answered Sep 21 '22 02:09

Mureinik