Get the Count of Concatenating Column in sql
up vote
1
down vote
favorite
i have Concatenating the multiple Columns and i want the count of how many columns are Concatenated
the query output and expected output are
for Concatenation
select ConcateColumn = STUFF(
COALESCE('* ' + RTRIM(col1),'')
+COALESCE('* ' + RTRIM(col2),'')
+COALESCE('* ' + RTRIM(col4),'')
+COALESCE('* ' + RTRIM(col3),'')
, 1, 2, '')
table
===================================
| col1 | col2 | col3 | col4 |
===================================
| 1 | 2 | NULL | NULL |
| NULL | NULL | NULL | NULL |
| 1 | NULL | NULL | NULL |
| NULL | 2 | 3 | 4 |
| | NULL | NULL | NULL |
==================================
the resulted output
==============
|ConcateColumn|
==============
|1*2 |
|NULL |
|1 |
|2*3*4 |
| |
===============
Expected Output
------------------------
| Count | ConcateColumn |
-------------------------
| 2 | 1*2 |
| 0 | NULL |
| 1 | 1 |
| 3 | 2*3*4 |
| 0 | |
-------------------------
if i get the count that is more than enough
c# sql .net sql-server model-view-controller
add a comment |
up vote
1
down vote
favorite
i have Concatenating the multiple Columns and i want the count of how many columns are Concatenated
the query output and expected output are
for Concatenation
select ConcateColumn = STUFF(
COALESCE('* ' + RTRIM(col1),'')
+COALESCE('* ' + RTRIM(col2),'')
+COALESCE('* ' + RTRIM(col4),'')
+COALESCE('* ' + RTRIM(col3),'')
, 1, 2, '')
table
===================================
| col1 | col2 | col3 | col4 |
===================================
| 1 | 2 | NULL | NULL |
| NULL | NULL | NULL | NULL |
| 1 | NULL | NULL | NULL |
| NULL | 2 | 3 | 4 |
| | NULL | NULL | NULL |
==================================
the resulted output
==============
|ConcateColumn|
==============
|1*2 |
|NULL |
|1 |
|2*3*4 |
| |
===============
Expected Output
------------------------
| Count | ConcateColumn |
-------------------------
| 2 | 1*2 |
| 0 | NULL |
| 1 | 1 |
| 3 | 2*3*4 |
| 0 | |
-------------------------
if i get the count that is more than enough
c# sql .net sql-server model-view-controller
Off the top of my head, you could do something usingCASE
statements and a subquery to achieve this. What have you already tried to calculate this?
– Martin Parkin
Nov 11 at 10:59
please note that the no of columns are dynamic
– Ramapriyan C
Nov 11 at 10:59
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
i have Concatenating the multiple Columns and i want the count of how many columns are Concatenated
the query output and expected output are
for Concatenation
select ConcateColumn = STUFF(
COALESCE('* ' + RTRIM(col1),'')
+COALESCE('* ' + RTRIM(col2),'')
+COALESCE('* ' + RTRIM(col4),'')
+COALESCE('* ' + RTRIM(col3),'')
, 1, 2, '')
table
===================================
| col1 | col2 | col3 | col4 |
===================================
| 1 | 2 | NULL | NULL |
| NULL | NULL | NULL | NULL |
| 1 | NULL | NULL | NULL |
| NULL | 2 | 3 | 4 |
| | NULL | NULL | NULL |
==================================
the resulted output
==============
|ConcateColumn|
==============
|1*2 |
|NULL |
|1 |
|2*3*4 |
| |
===============
Expected Output
------------------------
| Count | ConcateColumn |
-------------------------
| 2 | 1*2 |
| 0 | NULL |
| 1 | 1 |
| 3 | 2*3*4 |
| 0 | |
-------------------------
if i get the count that is more than enough
c# sql .net sql-server model-view-controller
i have Concatenating the multiple Columns and i want the count of how many columns are Concatenated
the query output and expected output are
for Concatenation
select ConcateColumn = STUFF(
COALESCE('* ' + RTRIM(col1),'')
+COALESCE('* ' + RTRIM(col2),'')
+COALESCE('* ' + RTRIM(col4),'')
+COALESCE('* ' + RTRIM(col3),'')
, 1, 2, '')
table
===================================
| col1 | col2 | col3 | col4 |
===================================
| 1 | 2 | NULL | NULL |
| NULL | NULL | NULL | NULL |
| 1 | NULL | NULL | NULL |
| NULL | 2 | 3 | 4 |
| | NULL | NULL | NULL |
==================================
the resulted output
==============
|ConcateColumn|
==============
|1*2 |
|NULL |
|1 |
|2*3*4 |
| |
===============
Expected Output
------------------------
| Count | ConcateColumn |
-------------------------
| 2 | 1*2 |
| 0 | NULL |
| 1 | 1 |
| 3 | 2*3*4 |
| 0 | |
-------------------------
if i get the count that is more than enough
c# sql .net sql-server model-view-controller
c# sql .net sql-server model-view-controller
asked Nov 11 at 10:53
Ramapriyan C
206
206
Off the top of my head, you could do something usingCASE
statements and a subquery to achieve this. What have you already tried to calculate this?
– Martin Parkin
Nov 11 at 10:59
please note that the no of columns are dynamic
– Ramapriyan C
Nov 11 at 10:59
add a comment |
Off the top of my head, you could do something usingCASE
statements and a subquery to achieve this. What have you already tried to calculate this?
– Martin Parkin
Nov 11 at 10:59
please note that the no of columns are dynamic
– Ramapriyan C
Nov 11 at 10:59
Off the top of my head, you could do something using
CASE
statements and a subquery to achieve this. What have you already tried to calculate this?– Martin Parkin
Nov 11 at 10:59
Off the top of my head, you could do something using
CASE
statements and a subquery to achieve this. What have you already tried to calculate this?– Martin Parkin
Nov 11 at 10:59
please note that the no of columns are dynamic
– Ramapriyan C
Nov 11 at 10:59
please note that the no of columns are dynamic
– Ramapriyan C
Nov 11 at 10:59
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
accepted
You can count the number of *
in the concatenated string:
with cte as
(
select
ConcateColumn = STUFF( COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
, 1, 2, '')
FROM T
)
select ConcateColumn,
-- how many '*' have been removed?
coalesce(len(ConcateColumn) - len(replace(ConcateColumn, '*', '')) + 1, 0)
from cte
;
Of course, this will return wrong numbers if your data contains *
if the column is empty i need count as 0 , but im getting as 1
– Ramapriyan C
Nov 11 at 11:18
What do you mean by empty? Empty string''
? It works with the D-Shih's dbfiddle.uk/…
– dnoeth
Nov 11 at 11:22
yes '' will return count as 1
– Ramapriyan C
Nov 11 at 11:23
check it in fiddle in last insert column
– Ramapriyan C
Nov 11 at 11:23
1
Well, an empty string is not the same as a NULL, of course it's counted.
– dnoeth
Nov 11 at 11:24
|
show 4 more comments
up vote
2
down vote
You can try this.
select (SELECT count(NULLIF(val,''))
FROM (VALUES(col1),(col2),(col3), (col4)) v (val)) 'COUNT',
ConcateColumn = STUFF(
COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
, 1, 2, '')
FROM T
If you want to count without NULL
and ''
you can try to use NULLIF
function.
sqlfiddle
1
count
instead ofsum
, but you beat me to it... +1 :-)
– Zohar Peled
Nov 11 at 11:01
@ZoharPeled Yes Thanks
– D-Shih
Nov 11 at 11:02
please note that the columns may a string, just for understanding i gave it as numbers
– Ramapriyan C
Nov 11 at 11:04
if i give string it is not working pls check
– Ramapriyan C
Nov 11 at 11:08
1
If you want empty strings treated as NULLs, you must addNULLIF
, e.g. `RTRIM(NULLIF(col1),''))
– dnoeth
Nov 11 at 11:27
|
show 8 more comments
up vote
1
down vote
In SQL Server 2017+, you can simply do:
select v.*
from t cross apply
(select count(*) as cnt,
string_agg(rtrim(col), '* ') within group (order by ord) as ConcateColumn
from (values (1, col1), (2, col2), (3, col4), (4, col3)
) v(col, ord)
where col is not null
) v;
In earlier versions, I would probably follow the same structure that you have:
select ( (case when col1 is not null then 1 else 0 end) +
(case when col2 is not null then 1 else 0 end) +
(case when col4 is not null then 1 else 0 end) +
(case when col3 is not null then 1 else 0 end)
) as cnt
add a comment |
up vote
0
down vote
select sum(
case when col1 = '' then 0 when col1 is null then 0 else 1 end +
case when col2 = '' then 0 when col2 is null then 0 else 1 end +
case when col3 = '' then 0 when col3 is null then 0 else 1 end +
case when col4 = '' then 0 when col4 is null then 0 else 1 end ) 'COUNT',
ConcateColumn = STUFF(
COALESCE('* ' + RTRIM(col1),'')
+COALESCE('* ' + RTRIM(col2),'')
+COALESCE('* ' + RTRIM(col4),'')
+COALESCE('* ' + RTRIM(col3),'')
, 1, 2, '') from temp1
group by col1,col2,col3,col4
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You can count the number of *
in the concatenated string:
with cte as
(
select
ConcateColumn = STUFF( COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
, 1, 2, '')
FROM T
)
select ConcateColumn,
-- how many '*' have been removed?
coalesce(len(ConcateColumn) - len(replace(ConcateColumn, '*', '')) + 1, 0)
from cte
;
Of course, this will return wrong numbers if your data contains *
if the column is empty i need count as 0 , but im getting as 1
– Ramapriyan C
Nov 11 at 11:18
What do you mean by empty? Empty string''
? It works with the D-Shih's dbfiddle.uk/…
– dnoeth
Nov 11 at 11:22
yes '' will return count as 1
– Ramapriyan C
Nov 11 at 11:23
check it in fiddle in last insert column
– Ramapriyan C
Nov 11 at 11:23
1
Well, an empty string is not the same as a NULL, of course it's counted.
– dnoeth
Nov 11 at 11:24
|
show 4 more comments
up vote
1
down vote
accepted
You can count the number of *
in the concatenated string:
with cte as
(
select
ConcateColumn = STUFF( COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
, 1, 2, '')
FROM T
)
select ConcateColumn,
-- how many '*' have been removed?
coalesce(len(ConcateColumn) - len(replace(ConcateColumn, '*', '')) + 1, 0)
from cte
;
Of course, this will return wrong numbers if your data contains *
if the column is empty i need count as 0 , but im getting as 1
– Ramapriyan C
Nov 11 at 11:18
What do you mean by empty? Empty string''
? It works with the D-Shih's dbfiddle.uk/…
– dnoeth
Nov 11 at 11:22
yes '' will return count as 1
– Ramapriyan C
Nov 11 at 11:23
check it in fiddle in last insert column
– Ramapriyan C
Nov 11 at 11:23
1
Well, an empty string is not the same as a NULL, of course it's counted.
– dnoeth
Nov 11 at 11:24
|
show 4 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can count the number of *
in the concatenated string:
with cte as
(
select
ConcateColumn = STUFF( COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
, 1, 2, '')
FROM T
)
select ConcateColumn,
-- how many '*' have been removed?
coalesce(len(ConcateColumn) - len(replace(ConcateColumn, '*', '')) + 1, 0)
from cte
;
Of course, this will return wrong numbers if your data contains *
You can count the number of *
in the concatenated string:
with cte as
(
select
ConcateColumn = STUFF( COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
, 1, 2, '')
FROM T
)
select ConcateColumn,
-- how many '*' have been removed?
coalesce(len(ConcateColumn) - len(replace(ConcateColumn, '*', '')) + 1, 0)
from cte
;
Of course, this will return wrong numbers if your data contains *
edited Nov 11 at 11:29
answered Nov 11 at 11:11
dnoeth
44k31838
44k31838
if the column is empty i need count as 0 , but im getting as 1
– Ramapriyan C
Nov 11 at 11:18
What do you mean by empty? Empty string''
? It works with the D-Shih's dbfiddle.uk/…
– dnoeth
Nov 11 at 11:22
yes '' will return count as 1
– Ramapriyan C
Nov 11 at 11:23
check it in fiddle in last insert column
– Ramapriyan C
Nov 11 at 11:23
1
Well, an empty string is not the same as a NULL, of course it's counted.
– dnoeth
Nov 11 at 11:24
|
show 4 more comments
if the column is empty i need count as 0 , but im getting as 1
– Ramapriyan C
Nov 11 at 11:18
What do you mean by empty? Empty string''
? It works with the D-Shih's dbfiddle.uk/…
– dnoeth
Nov 11 at 11:22
yes '' will return count as 1
– Ramapriyan C
Nov 11 at 11:23
check it in fiddle in last insert column
– Ramapriyan C
Nov 11 at 11:23
1
Well, an empty string is not the same as a NULL, of course it's counted.
– dnoeth
Nov 11 at 11:24
if the column is empty i need count as 0 , but im getting as 1
– Ramapriyan C
Nov 11 at 11:18
if the column is empty i need count as 0 , but im getting as 1
– Ramapriyan C
Nov 11 at 11:18
What do you mean by empty? Empty string
''
? It works with the D-Shih's dbfiddle.uk/…– dnoeth
Nov 11 at 11:22
What do you mean by empty? Empty string
''
? It works with the D-Shih's dbfiddle.uk/…– dnoeth
Nov 11 at 11:22
yes '' will return count as 1
– Ramapriyan C
Nov 11 at 11:23
yes '' will return count as 1
– Ramapriyan C
Nov 11 at 11:23
check it in fiddle in last insert column
– Ramapriyan C
Nov 11 at 11:23
check it in fiddle in last insert column
– Ramapriyan C
Nov 11 at 11:23
1
1
Well, an empty string is not the same as a NULL, of course it's counted.
– dnoeth
Nov 11 at 11:24
Well, an empty string is not the same as a NULL, of course it's counted.
– dnoeth
Nov 11 at 11:24
|
show 4 more comments
up vote
2
down vote
You can try this.
select (SELECT count(NULLIF(val,''))
FROM (VALUES(col1),(col2),(col3), (col4)) v (val)) 'COUNT',
ConcateColumn = STUFF(
COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
, 1, 2, '')
FROM T
If you want to count without NULL
and ''
you can try to use NULLIF
function.
sqlfiddle
1
count
instead ofsum
, but you beat me to it... +1 :-)
– Zohar Peled
Nov 11 at 11:01
@ZoharPeled Yes Thanks
– D-Shih
Nov 11 at 11:02
please note that the columns may a string, just for understanding i gave it as numbers
– Ramapriyan C
Nov 11 at 11:04
if i give string it is not working pls check
– Ramapriyan C
Nov 11 at 11:08
1
If you want empty strings treated as NULLs, you must addNULLIF
, e.g. `RTRIM(NULLIF(col1),''))
– dnoeth
Nov 11 at 11:27
|
show 8 more comments
up vote
2
down vote
You can try this.
select (SELECT count(NULLIF(val,''))
FROM (VALUES(col1),(col2),(col3), (col4)) v (val)) 'COUNT',
ConcateColumn = STUFF(
COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
, 1, 2, '')
FROM T
If you want to count without NULL
and ''
you can try to use NULLIF
function.
sqlfiddle
1
count
instead ofsum
, but you beat me to it... +1 :-)
– Zohar Peled
Nov 11 at 11:01
@ZoharPeled Yes Thanks
– D-Shih
Nov 11 at 11:02
please note that the columns may a string, just for understanding i gave it as numbers
– Ramapriyan C
Nov 11 at 11:04
if i give string it is not working pls check
– Ramapriyan C
Nov 11 at 11:08
1
If you want empty strings treated as NULLs, you must addNULLIF
, e.g. `RTRIM(NULLIF(col1),''))
– dnoeth
Nov 11 at 11:27
|
show 8 more comments
up vote
2
down vote
up vote
2
down vote
You can try this.
select (SELECT count(NULLIF(val,''))
FROM (VALUES(col1),(col2),(col3), (col4)) v (val)) 'COUNT',
ConcateColumn = STUFF(
COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
, 1, 2, '')
FROM T
If you want to count without NULL
and ''
you can try to use NULLIF
function.
sqlfiddle
You can try this.
select (SELECT count(NULLIF(val,''))
FROM (VALUES(col1),(col2),(col3), (col4)) v (val)) 'COUNT',
ConcateColumn = STUFF(
COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
, 1, 2, '')
FROM T
If you want to count without NULL
and ''
you can try to use NULLIF
function.
sqlfiddle
edited Nov 11 at 11:33
answered Nov 11 at 11:00
D-Shih
24.4k61431
24.4k61431
1
count
instead ofsum
, but you beat me to it... +1 :-)
– Zohar Peled
Nov 11 at 11:01
@ZoharPeled Yes Thanks
– D-Shih
Nov 11 at 11:02
please note that the columns may a string, just for understanding i gave it as numbers
– Ramapriyan C
Nov 11 at 11:04
if i give string it is not working pls check
– Ramapriyan C
Nov 11 at 11:08
1
If you want empty strings treated as NULLs, you must addNULLIF
, e.g. `RTRIM(NULLIF(col1),''))
– dnoeth
Nov 11 at 11:27
|
show 8 more comments
1
count
instead ofsum
, but you beat me to it... +1 :-)
– Zohar Peled
Nov 11 at 11:01
@ZoharPeled Yes Thanks
– D-Shih
Nov 11 at 11:02
please note that the columns may a string, just for understanding i gave it as numbers
– Ramapriyan C
Nov 11 at 11:04
if i give string it is not working pls check
– Ramapriyan C
Nov 11 at 11:08
1
If you want empty strings treated as NULLs, you must addNULLIF
, e.g. `RTRIM(NULLIF(col1),''))
– dnoeth
Nov 11 at 11:27
1
1
count
instead of sum
, but you beat me to it... +1 :-)– Zohar Peled
Nov 11 at 11:01
count
instead of sum
, but you beat me to it... +1 :-)– Zohar Peled
Nov 11 at 11:01
@ZoharPeled Yes Thanks
– D-Shih
Nov 11 at 11:02
@ZoharPeled Yes Thanks
– D-Shih
Nov 11 at 11:02
please note that the columns may a string, just for understanding i gave it as numbers
– Ramapriyan C
Nov 11 at 11:04
please note that the columns may a string, just for understanding i gave it as numbers
– Ramapriyan C
Nov 11 at 11:04
if i give string it is not working pls check
– Ramapriyan C
Nov 11 at 11:08
if i give string it is not working pls check
– Ramapriyan C
Nov 11 at 11:08
1
1
If you want empty strings treated as NULLs, you must add
NULLIF
, e.g. `RTRIM(NULLIF(col1),''))– dnoeth
Nov 11 at 11:27
If you want empty strings treated as NULLs, you must add
NULLIF
, e.g. `RTRIM(NULLIF(col1),''))– dnoeth
Nov 11 at 11:27
|
show 8 more comments
up vote
1
down vote
In SQL Server 2017+, you can simply do:
select v.*
from t cross apply
(select count(*) as cnt,
string_agg(rtrim(col), '* ') within group (order by ord) as ConcateColumn
from (values (1, col1), (2, col2), (3, col4), (4, col3)
) v(col, ord)
where col is not null
) v;
In earlier versions, I would probably follow the same structure that you have:
select ( (case when col1 is not null then 1 else 0 end) +
(case when col2 is not null then 1 else 0 end) +
(case when col4 is not null then 1 else 0 end) +
(case when col3 is not null then 1 else 0 end)
) as cnt
add a comment |
up vote
1
down vote
In SQL Server 2017+, you can simply do:
select v.*
from t cross apply
(select count(*) as cnt,
string_agg(rtrim(col), '* ') within group (order by ord) as ConcateColumn
from (values (1, col1), (2, col2), (3, col4), (4, col3)
) v(col, ord)
where col is not null
) v;
In earlier versions, I would probably follow the same structure that you have:
select ( (case when col1 is not null then 1 else 0 end) +
(case when col2 is not null then 1 else 0 end) +
(case when col4 is not null then 1 else 0 end) +
(case when col3 is not null then 1 else 0 end)
) as cnt
add a comment |
up vote
1
down vote
up vote
1
down vote
In SQL Server 2017+, you can simply do:
select v.*
from t cross apply
(select count(*) as cnt,
string_agg(rtrim(col), '* ') within group (order by ord) as ConcateColumn
from (values (1, col1), (2, col2), (3, col4), (4, col3)
) v(col, ord)
where col is not null
) v;
In earlier versions, I would probably follow the same structure that you have:
select ( (case when col1 is not null then 1 else 0 end) +
(case when col2 is not null then 1 else 0 end) +
(case when col4 is not null then 1 else 0 end) +
(case when col3 is not null then 1 else 0 end)
) as cnt
In SQL Server 2017+, you can simply do:
select v.*
from t cross apply
(select count(*) as cnt,
string_agg(rtrim(col), '* ') within group (order by ord) as ConcateColumn
from (values (1, col1), (2, col2), (3, col4), (4, col3)
) v(col, ord)
where col is not null
) v;
In earlier versions, I would probably follow the same structure that you have:
select ( (case when col1 is not null then 1 else 0 end) +
(case when col2 is not null then 1 else 0 end) +
(case when col4 is not null then 1 else 0 end) +
(case when col3 is not null then 1 else 0 end)
) as cnt
answered Nov 11 at 11:36
Gordon Linoff
748k34285391
748k34285391
add a comment |
add a comment |
up vote
0
down vote
select sum(
case when col1 = '' then 0 when col1 is null then 0 else 1 end +
case when col2 = '' then 0 when col2 is null then 0 else 1 end +
case when col3 = '' then 0 when col3 is null then 0 else 1 end +
case when col4 = '' then 0 when col4 is null then 0 else 1 end ) 'COUNT',
ConcateColumn = STUFF(
COALESCE('* ' + RTRIM(col1),'')
+COALESCE('* ' + RTRIM(col2),'')
+COALESCE('* ' + RTRIM(col4),'')
+COALESCE('* ' + RTRIM(col3),'')
, 1, 2, '') from temp1
group by col1,col2,col3,col4
add a comment |
up vote
0
down vote
select sum(
case when col1 = '' then 0 when col1 is null then 0 else 1 end +
case when col2 = '' then 0 when col2 is null then 0 else 1 end +
case when col3 = '' then 0 when col3 is null then 0 else 1 end +
case when col4 = '' then 0 when col4 is null then 0 else 1 end ) 'COUNT',
ConcateColumn = STUFF(
COALESCE('* ' + RTRIM(col1),'')
+COALESCE('* ' + RTRIM(col2),'')
+COALESCE('* ' + RTRIM(col4),'')
+COALESCE('* ' + RTRIM(col3),'')
, 1, 2, '') from temp1
group by col1,col2,col3,col4
add a comment |
up vote
0
down vote
up vote
0
down vote
select sum(
case when col1 = '' then 0 when col1 is null then 0 else 1 end +
case when col2 = '' then 0 when col2 is null then 0 else 1 end +
case when col3 = '' then 0 when col3 is null then 0 else 1 end +
case when col4 = '' then 0 when col4 is null then 0 else 1 end ) 'COUNT',
ConcateColumn = STUFF(
COALESCE('* ' + RTRIM(col1),'')
+COALESCE('* ' + RTRIM(col2),'')
+COALESCE('* ' + RTRIM(col4),'')
+COALESCE('* ' + RTRIM(col3),'')
, 1, 2, '') from temp1
group by col1,col2,col3,col4
select sum(
case when col1 = '' then 0 when col1 is null then 0 else 1 end +
case when col2 = '' then 0 when col2 is null then 0 else 1 end +
case when col3 = '' then 0 when col3 is null then 0 else 1 end +
case when col4 = '' then 0 when col4 is null then 0 else 1 end ) 'COUNT',
ConcateColumn = STUFF(
COALESCE('* ' + RTRIM(col1),'')
+COALESCE('* ' + RTRIM(col2),'')
+COALESCE('* ' + RTRIM(col4),'')
+COALESCE('* ' + RTRIM(col3),'')
, 1, 2, '') from temp1
group by col1,col2,col3,col4
answered Nov 11 at 12:28
Hitham Yosri
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248008%2fget-the-count-of-concatenating-column-in-sql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Off the top of my head, you could do something using
CASE
statements and a subquery to achieve this. What have you already tried to calculate this?– Martin Parkin
Nov 11 at 10:59
please note that the no of columns are dynamic
– Ramapriyan C
Nov 11 at 10:59