SQL get count from one table and split to two columns

Rob

I have a table like this:

Date        Product
1/1/2015    Apples
1/1/2015    Apples
1/1/2015    Oranges
1/2/2015    Apples
1/2/2015    Apples
1/2/2015    Oranges

How can I do a select so I get something like this:

Date      Count of Apples    Count of Oranges
1/1/2015      2                 1
1/2/2015      2                 1

Thanks. I have tried case like this but the error is being thrown:

Select 'Date',
CASE WHEN 'Product' = 'Apples' THEN COUNT(*) ELSE 0 END as 'Count'
FROM #TEMP Group by 1,2

Each GROUP BY expression must contain at least one column that is not an outer reference.

jpw

You can do conditional aggregation like this:

select 
  [date], 
  sum(case when Product = 'Apples'  then 1 else 0 end) as [Count of Apples],
  sum(case when Product = 'Oranges' then 1 else 0 end) as [Count of Oranges]
from #temp
group by [date]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Query to compare two columns with one column from another table (and get two values)

From Dev

SQL - Split values from one column into two separate columns

From Dev

The extract total distinct count for two columns from a table in SQL Server

From Dev

The extract total distinct count for two columns from a table in SQL Server

From Dev

How to get value from one table column when two columns of the same name exist in an sql join

From Dev

How to split data from two columns in one table into multiple columns of a result table

From Dev

SQL - one row to split into two columns snake

From Dev

UPDATE one SQL table based on equality of two columns with two columns from another table

From Dev

How to get COUNT(*) from one partition of a table in SQL Server 2012?

From Dev

SQL - get count from one table based on result of query

From Dev

COUNT values in different columns of one table SQL

From Dev

SQL - Split One Table to Two And Link

From Dev

SQL - Split One Table to Two And Link

From Dev

How to split one column into two columns just by counting the characters and inserting them into another table via a SQL query?

From Dev

sql how to get a count based on two columns

From Dev

SQL Select two columns from one table translated by a column from another table

From Dev

How to split one date column into two columns and get data from the start date to last date

From Dev

How to split one date column into two columns and get data from the start date to last date

From Dev

Merge two columns into one from the same table

From Dev

SQL - COUNT() rows for two tables that are GROUP BY one column in one table

From Dev

Grouping SQL columns from one table

From Dev

Count from two different columns from same table based on range

From Dev

SQL: Count two different columns from two different tables

From Dev

SQL: Count two different columns from two different tables

From Dev

MYSQL join one colum from one table to two count() in another

From Dev

How can I merge rows and columns from two sql tables into one table

From Dev

get the count of records from two tables in sql

From Dev

How to count distinct values from two columns into one number

From Dev

Getting two columns referring to one table from the same row in a table?

Related Related

  1. 1

    SQL Query to compare two columns with one column from another table (and get two values)

  2. 2

    SQL - Split values from one column into two separate columns

  3. 3

    The extract total distinct count for two columns from a table in SQL Server

  4. 4

    The extract total distinct count for two columns from a table in SQL Server

  5. 5

    How to get value from one table column when two columns of the same name exist in an sql join

  6. 6

    How to split data from two columns in one table into multiple columns of a result table

  7. 7

    SQL - one row to split into two columns snake

  8. 8

    UPDATE one SQL table based on equality of two columns with two columns from another table

  9. 9

    How to get COUNT(*) from one partition of a table in SQL Server 2012?

  10. 10

    SQL - get count from one table based on result of query

  11. 11

    COUNT values in different columns of one table SQL

  12. 12

    SQL - Split One Table to Two And Link

  13. 13

    SQL - Split One Table to Two And Link

  14. 14

    How to split one column into two columns just by counting the characters and inserting them into another table via a SQL query?

  15. 15

    sql how to get a count based on two columns

  16. 16

    SQL Select two columns from one table translated by a column from another table

  17. 17

    How to split one date column into two columns and get data from the start date to last date

  18. 18

    How to split one date column into two columns and get data from the start date to last date

  19. 19

    Merge two columns into one from the same table

  20. 20

    SQL - COUNT() rows for two tables that are GROUP BY one column in one table

  21. 21

    Grouping SQL columns from one table

  22. 22

    Count from two different columns from same table based on range

  23. 23

    SQL: Count two different columns from two different tables

  24. 24

    SQL: Count two different columns from two different tables

  25. 25

    MYSQL join one colum from one table to two count() in another

  26. 26

    How can I merge rows and columns from two sql tables into one table

  27. 27

    get the count of records from two tables in sql

  28. 28

    How to count distinct values from two columns into one number

  29. 29

    Getting two columns referring to one table from the same row in a table?

HotTag

Archive