1

I have the following issue. I have declared a variable based on ID value, but there are two different values that have my data. This is what I retreive:

    entry_template_id   entry_template_name book_display_name   entry_count value_count null_value_count    null_percentage followed_value_count    followed_null_value_count   followed_null_percentage    
                   9    Loading             Reporting            10            530           78              0.147169811320      520                      78                         0.150000000000 
                   19   Loading             Reporting            1              53            8              0.150943396226       53                       8                         0.150943396226

I want to retrive just one row with ID like (9, 19), same template_name, same display_name, SUM (entry_count), SUM(value_count), SUM(null_value_count), AVG(null_percentage), SUM(followed_value_count), SUM(followed_nulls_value_count), AVG(followed_null_percentage).

Can you please help me with the syntax?

Thank you!

Dragos D
  • 69
  • 5

1 Answers1

2

Provided that you want to group by name, this should work:

SELECT 
STUFF((SELECT ', ' + CAST(entry_template_id AS varchar(50))
       FROM Entries t2 where t1.entry_template_name = t2.entry_template_name 
       FOR XML PATH('')), 1, 2, '') Ids, 
MAX(entry_template_name) entry_template_name, 
MAX(book_display_name) book_display_name, 
SUM(entry_count) entry_count, SUM(value_count) value_count, 
SUM(null_value_count) null_value_count, 
AVG(null_percentage) null_percentage, 
SUM(followed_value_count) followed_value_count, 
SUM(followed_null_value_count) followed_null_value_count, 
AVG(followed_null_percentage) followed_null_percentage
FROM Entries t1
GROUP BY entry_template_name

See this question for further explanation.

Community
  • 1
  • 1
PHeiberg
  • 29,411
  • 6
  • 59
  • 81