'How to improve the count of unique values to illustrate cleaner format
Column order_cd has two unique values = SGL, DBL. I am trying to get the count of these for each store number in a location. I am able to get the count with my code below but would like to learn how to get it into the desired format in sql.
SELECT
Location, store_nm,
,SUM(CASE WHEN order_cd = 'SGL' THEN 1 ELSE 0 end) AS Single
,SUM(CASE WHEN order_cd = 'DBL' THEN 1 ELSE 0 end) AS Double
From Table
**Current state
+----------+----------+--------+---------+
| Location | Store_nm | Single | Double |
+----------+----------+--------+---------+
| E | 10 | 123 | 0 |
| E | 10 | 0 | 2 |
| E | 30 | 55 | 0 |
| E | 30 | 0 | 65 |
| E | 45 | 1456 | 0 |
| E | 45 | 0 | 800 |
+----------+----------+--------+---------+
**DESIRED**
+----------+----------+--------+---------+
| Location | Store_nm | Single | Double |
+----------+----------+--------+---------+
| E | 10 | 123 | 2 |
| E | 30 | 55 | 65 |
| E | 45 | 1456 | 800 |
+----------+----------+--------+---------+
Solution 1:[1]
Answer provided by user FlexYourData.
Add GROUP BY Location, store_nm to the end of your query.
Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Number1Rock |
