Home » SQL & PL/SQL » SQL & PL/SQL » SQL count with comma delimit (19c)
SQL count with comma delimit [message #690153] |
Wed, 06 November 2024 15:38 |
|
born2achieve
Messages: 20 Registered: July 2012
|
Junior Member |
|
|
Hello,
I am using oracle 19c.
Below the same data and sample expected result. Basically i need to check if the MFR_TYPE_ID count > then 1 then print "Multiple and have the MFR_NAME as comma delimited. If the MFR_TYPE_ID count = then 1 then print “Single and have the MFR_NAME as comma delimited. How to achieve this result? can this be implemented without group by? please show me a sample query to achieve this result.
WITH
Data (MFR_TYPE_ID, MFR_NAME)
AS
(SELECT '100-1234', 'Apple' FROM DUAL
UNION ALL
SELECT '100-1234', 'Nokia' FROM DUAL
UNION ALL
SELECT '100-1244', 'Samsung' FROM DUAL
UNION ALL
SELECT '100-1245', 'Facebook' FROM DUAL
UNION ALL
SELECT '100-1245', 'Microsoft' FROM DUAL
UNION ALL
SELECT '100-1245', 'DELL' FROM DUAL)
SELECT *
FROM Data;
--- Expected Result
WITH
RESULT (MFR_TYPE, MFR_NAME)
AS
(SELECT 'Multiple', 'Apple,Nokia' FROM DUAL
UNION ALL
SELECT 'Single', 'Samsung' FROM DUAL
UNION ALL
SELECT 'Multiple', 'Facebook,Microsoft,DELL' FROM DUAL)
SELECT MFR_TYPE, MFR_NAME
FROM Result;
|
|
|
Re: SQL count with comma delimit [message #690154 is a reply to message #690153] |
Wed, 06 November 2024 16:05 |
|
Michel Cadot
Messages: 68729 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
SQL> col MFR_NAMES format a50
SQL> WITH
2 Data (MFR_TYPE_ID, MFR_NAME)
3 AS
4 (SELECT '100-1234', 'Apple' FROM DUAL
5 UNION ALL
6 SELECT '100-1234', 'Nokia' FROM DUAL
7 UNION ALL
8 SELECT '100-1244', 'Samsung' FROM DUAL
9 UNION ALL
10 SELECT '100-1245', 'Facebook' FROM DUAL
11 UNION ALL
12 SELECT '100-1245', 'Microsoft' FROM DUAL
13 UNION ALL
14 SELECT '100-1245', 'DELL' FROM DUAL)
15 SELECT case
16 when count(*) = 1 then 'Single'
17 when count(*) > 1 then 'Multîple'
18 end MFR_TYPE,
19 listagg(MFR_NAME,',') within group (order by MFR_NAME) MFR_NAMES
20 FROM Data
21 group by MFR_TYPE_ID
22 /
MFR_TYPE MFR_NAMES
-------- --------------------------------------------------
Multîple Apple,Nokia
Single Samsung
Multîple DELL,Facebook,Microsoft
3 rows selected.
|
|
|
|
Re: SQL count with comma delimit [message #690156 is a reply to message #690155] |
Wed, 06 November 2024 19:32 |
Solomon Yakobson
Messages: 3303 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
What's wrong with GROUP BY? You could use analytic functions:
WITH
Data (MFR_TYPE_ID, MFR_NAME)
AS
(SELECT '100-1234', 'Apple' FROM DUAL
UNION ALL
SELECT '100-1234', 'Nokia' FROM DUAL
UNION ALL
SELECT '100-1244', 'Samsung' FROM DUAL
UNION ALL
SELECT '100-1245', 'Facebook' FROM DUAL
UNION ALL
SELECT '100-1245', 'Microsoft' FROM DUAL
UNION ALL
SELECT '100-1245', 'DELL' FROM DUAL
),
T AS (
SELECT ROW_NUMBER() OVER(PARTITION BY MFR_TYPE_ID ORDER BY 1) RN,
CASE COUNT(*) OVER(PARTITION BY MFR_TYPE_ID)
WHEN 1 THEN 'Single'
ELSE 'Multiple'
END MFR_TYPE,
LISTAGG(MFR_NAME,',') WITHIN GROUP(ORDER BY MFR_NAME) OVER(PARTITION BY MFR_TYPE_ID) MFR_NAME
FROM DATA
)
SELECT MFR_TYPE,
MFR_NAME
FROM T
WHERE RN = 1
/
MFR_TYPE MFR_NAME
-------- ------------------------------
Multiple Apple,Nokia
Single Samsung
Multiple DELL,Facebook,Microsoft
SQL>
SY.
|
|
|
|
|
Goto Forum:
Current Time: Thu Jan 02 14:46:57 CST 2025
|