WHERE/GROUP By Condition - One Name but multiple values

sql group by multiple columns
sql group by multiple columns having count
group by having sql
group by multiple columns mysql
sql group by count
group by clause in sql
not a group by expression
sql query for finding records where count > 1

I have the following table:

Name          Product         
Bob           Car              
Bob           Apples
Bob           Pears
Bob           Car
John          Apples
John          Pears

Whoever has bought a Product Car, I want to keep separate from everyone else. So, I create a flag:

Name          Product       Flag        
Bob           Car             1  
Bob           Apples          0
Bob           Pears           0  
Bob           Car             1
John          Apples          0
John          Pears           0 

But the problem with my flag is that even if I do a where condition and say, show me the consumer WHERE flag !=1, it'll pick Bob. Which is incorrect as Bob owns a car.

I would still like to GROUP by Product.

How do I separate the above table into two groups?

Thanks!

Use below query :-

select name from table where flag!=1
and name not in (select name from table where flag = 1)
group by name

SQL, SELECT NAME, SUM(SALARY) FROM Employee GROUP BY NAME; Group By multiple columns: Group by multiple column is say for example, GROUP the rows with same values of both the columns column1 and column2 in one group. We know that WHERE clause is used to place conditions on columns but what if  To search for values that meet several conditions, you specify an AND condition. Specifying an OR Condition. Using an OR condition enables you to specify several alternative values to search for in a column. This option expands the scope of the search and can return more rows than searching for a single value.

"show me the consumer WHERE flag !=1, it'll pick Bob" that is because you are asking for rows where flag != 1. Instead you'll need something a little more complicated, like:

SELECT DISTINCT Name
FROM tableTable
WHERE Name NOT IN (SELECT Name FROM theTable WHERE Product = 'Car')

alternatively, you can do a LEFT JOIN, which may or may not be faster depending on the amount of data you have and how its values are distributed.

SELECT DISTINCT a.Name
FROM theTable a
LEFT JOIN theTable b ON a.Name = b.Name AND b.Product = 'Car'
WHERE a.Product != 'Car' AND b.Product IS NULL
;

This gets all the rows with products other than cars, and then uses the LEFT JOIN in conjunction with the IS NULL condition to find which did not also have a 'Car' row.

Care To Know Clauses: All About SELECT, FROM, WHERE , SQL is a language of databases and PostgreSQL is our chosen one. When a GROUP BY clause is present (or aggregate functions) in the query, SELECT Multiple boolean conditions may be checked in the WHERE clause by The HAVING clause can reference those columns named in aggregate  Let's say that you need to sum values with more than one condition, such as the sum of product sales in a specific region. This is a good case for using the SUMIFS function in a formula. Have a look at this example in which we have two conditions: we want the sum of Meat sales (from column C) in the South region (from column A).

I think you want your table's data displayed, just with "People who bought cars" partitioned (not grouped) separately somehow - this could be done with an ORDER BY OwnsACar clause, for example.

Step 1: Identify the people who have bought cars:

SELECT DISTINCT
    Name
FROM
    yourTable
 WHERE
    Product = 'Car'

Step 2: Join on this data to generate a calculated "OwnsACar" column:

SELECT
    yourTable.Name,
    yourTable.Product,
    ISNULL( carowners.Name ) AS OwnsACar
FROM
    yourTable
    LEFT OUTER JOIN
    (
        SELECT DISTINCT
        Name
    FROM
        yourTable
    WHERE
        Product = 'Car'
    ) AS carowners ON carowners.Name = yourTable.Name
ORDER BY
    OwnsACar ASC,
    yourTable.Name ASC

GROUP BY and HAVING Clause in SQL, In SQL, GROUP BY Clause is one of the tools to summarize or aggregate the data series. For example, sum up the daily sales and combine in a single quarter and show it to the count(), and sum() to combine into single or multiple columns​. and Department), joined table grouped by Department name. SPSS Tutorials: Descriptive Stats by Group (Compare Means) Compare Means is best used when you want to compare several numeric variables with respect to one or more categorical variables. It is especially useful for summarizing numeric variables simultaneously across categories.

You can use these two queries. The additional Flag column is not required.

-- do not have Car
SELECT * 
FROM products
WHERE Name not in (SELECT DISTINCT Name 
                   FROM products 
                   WHERE Product='Car');

-- have Car
SELECT * 
FROM products
WHERE Name in (SELECT DISTINCT Name 
               FROM products 
               WHERE Product='Car');

Illustration:

-- table
SELECT * FROM products;
+------+---------+
| Name | Product |
+------+---------+
| Bob  | Car     |
| Bob  | Apples  |
| Bob  | Pears   |
| Bob  | Car     |
| John | Apples  |
| John | Pears   |
+------+---------+

-- query for people that do not have Car
+------+---------+
| Name | Product |
+------+---------+
| John | Apples  |
| John | Pears   |
+------+---------+


-- query for people having 'Car'
+------+---------+
| Name | Product |
+------+---------+
| Bob  | Car     |
| Bob  | Apples  |
| Bob  | Pears   |
| Bob  | Car     |
+------+---------+

MySQL GROUP BY and HAVING Clause with Examples, The GROUP BY clause is a SQL command that is used to group rows that have the is optional; represents other column names when the grouping is done on more than one column. Grouping using multiple columns. Once a condition is satisfied, its corresponding value is returned. Similarly, if we change the condition in a Case statement in SQL, it returns appropriate expression. In the following example, we want to get Product name for ProductID 4.it does not satisfy Case statement condition; therefore, it gave output from Else expression.

Try with :

SELECT `t`.`Name`, `t`.`Product`, SUM(`t`.`Flag`) as hasCar
FROM your_table t
GROUP BY `t`.`Name`
HAVING `t`.`hasCar` = 0;

Although you can go without the flag column by going :

SELECT `t`.`Name`, `t`.`Product`, SUM(IF(`t`.`Product` = 'Car', 1, 0)) as hasCar
FROM your_table t
GROUP BY `t`.`Name`
HAVING `t`.`hasCar` = 0;

SQL GROUP BY, This clause is most often used with aggregations to show one value per We can use a group by and aggregates to collect multiple types of information. A query select statement can have a column name changed and  For example, to count the occurrences of a text or numeric value based on a single or multiple conditions, you can use COUNTIF and COUNTIFS, respectively. To find out a sum of values based on the specified condition(s), use the SUMIF or SUMIFS functions. To calculate the average according to certain criteria, use AVERAGEIF or AVERAGEIFS.

SQL Server GROUP BY, The GROUP BY clause allows you to arrange the rows of a query in groups. The groups The customer with id two placed two orders in 2017 and one order in 2018. Let's add a SQL Server GROUP BY clause - multiple columns example  How to sumif with multiple criteria in one column? In Excel, SUMIF function is a useful function for us to sum cells with multiple criteria in different columns, but with this function, we can also sum cells based on multiple criteria in one column. In this article. I will talk about how to sum values with more than one criteria in same column.

SQL COUNT() with GROUP by, A combination of same values (on a column) will be treated as an with the following condition -. 1. 'working_area' should come uniquely, The default order is ascending if not any keyword or mention ASCE is mentioned. Anyway. What I would do looks pretty different from what you've been trying. Here you go, under the assumption that if none of the checks comes true the default value should be 0. Set up several variable ports one after the other; the first one (let's name it "v_except_help_1") has this expression:

Grouping Rows with GROUP BY, Grouping columns can be column names or derived columns. A group that contains multiple nulls doesn't imply that the nulls equal one another. The GROUP BY clause restricts the rows of the result; only one row  One option is to create it via Value, whereby the people’s names can be checked off. The issue I have come across is that method not cumulative. What I mean is that if the data set is to be populated by Jon, Joe, Bill, Dave, Lisa and Ted, and I wish to only see the data entered by Jon and Joe, I have to create a filter view and then manually go in can check off their respective names.

Comments
  • You don't need the Flag column at all.
  • "How do I separate the above table into two groups?" - What "groups"? Please note that GROUP BY in SQL does not "group" result-sets in the output, instead it's for the application of aggregate functions. Please post some example desired output because your question is unclear.
  • You don't need the Flag column at all. If your purpose is to hide the Name column to specific users, you have to create a VIEW that is basically a SELECT all_columns_except_name FROM your_table, And then set specific permissions: Normal users can't access the table but can access the View
  • Use proper GROUP BY. SELECT * ... GROUP BY t.Name won't even run on 99% of dbms.
  • Didn't get your point at all. Just added backquotes to avoid possible keywords problem, if that was what you were refering to + this is tagged as mysql.
  • You need to list column names when you use GROUP BY.
  • Not needed on mysql in this case though, but whatever, I updated.