0

I am newbie to Stack overflow and also SQL server reporting services. So please excuse me for the format of the question.

So here is the situation:

I am developing a SSRS report which needs to be grouped by an Count of Distinct product names as shown below.

I created a text box called ProdCount with an expression

COUNTDISTNCT(Fields!Product.value,"DataSet1") 

which gives me the count 63 within the scope of DataSet1.

Now i need to group the data by taking product names where the above formula is >1 .

=IIF(ProdCount>1,Fields!Product.value,Nothing)

My Problem:

  1. I tried to call the ProdCount from the calculated field since i cant use the aggregate functions in Calculated Fields and use the second expression by using

    = ReportItems!ProdCount.value

    which gives me an error FieldValue Denying ReportItems

  2. I tried to combine the above two expressions by creating a calculated field by

    IIF(CountDistinct(Fields!Product.Value,"DataSet1")>1,Fields!Product.Value,Nothing)
    which gives me an error Calculated fields cannot have expressions

  3. I tried to use Report Variables in the same way as above(1) which was not working either.

  4. I also tried to use CROSS JOIN in the query

Select Count(Distinct(Product Name)
from Query1
Cross join
My Main Query which give me the data

which is taking more time to execute.

So Can anyone help me with solution where i can group the data by combining the above two expressions.

Please excuse me for the format. I was confused with framing question. I accept all your edits , so that i can learn in future.

Here is my code:

    SELECT * FROM 

--Query1 which counts the number of distinct products) 
(SELECT DISTINCT COUNT(gproduct.ProductName) AS ProdCount
 FROM Table1 
 LEFT JOIN Table4
  ON Table1.column=Table1.column
 LEFT JOIN Table2
  ON Table3.Column = TTable1.Column
 LEFT JOIN
 (
    SELECT Distinct Table6.Name AS ProductName,Table9.ColumnId
    FROM Table6
    INNER JOIN Table7
     ON Table6.Column=Table7.Column
    INNER JOIN Table8
     ON Table7.Column=Table8.Column
    INNER JOIN Table9 
     ON Table9.Column=Table8.Column
 )gproduct
  ON Table1.ColumnId=gproduct.ColumnId
 GROUP BY gproduct.ColumnId,
)qProduct
CROSS JOIN
--My main Query which get data from different table including Product name
(SELECT

       Upper(CASE WHEN (CASE WHEN Table4.Column =1 THEN 'Yes' ELSE 'NO' END)='YES' 
           THEN gab.ProductName
      ELSE
           Table2.productName
      END) AS Product, 
FROM Table1 AS ec
LEFT JOIN Table2 AS ep 
  ON --
LEFT JOIN Table3 AS ebrd
 ON --
Left JOIN Table4 AS etpc
  ON --
LEFT  JOIN Table5 AS gst 
  ON --
LEFT JOIN
( 
 SELECT Distinct Table6.Name AS ProductName,Table9.ColumnId
    FROM Table6
    INNER JOIN Table7
     ON Table6.Column=Table7.Column
    INNER JOIN Table8
     ON Table7.Column=Table8.Column
    INNER JOIN Table9 
     ON Table9.Column=Table8.Column
) gab
ON Table1.ColumnId=gab.ColumnId
)QMain
lvars
  • 77
  • 1
  • 10
  • Can you post your SQL Query as well? The one you are using. – glaeran Apr 15 '14 at 17:35
  • glaeran-- Sorry i cant share my query as its corporate data. Thats why i have given a basic idea how my query looks like – lvars Apr 15 '14 at 18:28
  • The SQL not the data. At any rate, can you move the distinct into the SQL? – D.S. Apr 15 '14 at 18:39
  • I cant really share my query as its confidential. I cant move distinct because if i use the above formulas within my main query i get count as 1 which i dont want. The scope of the count is for full dataset . As i said i can use cross join but it takes more time to execute in sql server itself. then we can imagine how much it takes to generate report. – lvars Apr 15 '14 at 18:45
  • Try using windowing functions; they should get you what you need, and probably be the best performing solution from the options mentioned so far. I'd also really advise posting your query if you can - if you're concerned about sensitive information (field/table names?), just change those words. If your corporate policies leave you unable to share sanitized code snippets with peers, you're going to have a very rough future at that company. – kyzen Apr 15 '14 at 19:08
  • Any kind of data would be really usefull.. If you can't provide us exact data just prepare some looking similar, it will really help to solve your problem as fast as possible and in the best way for you. – glaeran Apr 15 '14 at 19:09
  • Ok, i will try to prepare some snippet of code which resembles my Main query in few minutes – lvars Apr 15 '14 at 19:31

1 Answers1

0

Personally I would try to solve the problem in query itself instead of SSRS report. According the data you provided it would be something like:

SELECT
ProductName,
count(distinct Product)
from
YourTable
group by 
ProductName
having count(distinct product) > 1

Later on creating SSRS report should be quite easy.

glaeran
  • 410
  • 4
  • 14
  • I did just did the same thing for my Query1, but still i need to cross join it with my main query to get the required count. The product name is taken from two tables, using case statement when product name is null in first table then it takes from second table. Can we use the case statement in Having count expression?. I tried your query but it doesn't give the required count value. it gives count per row. – lvars Apr 15 '14 at 19:28
  • You can replace ProductName in above query with `CASE` function. Try to avoid using `CROSS JOIN` wouldn't it be simple just to `INNER JOIN` your table and give it another alias? – glaeran Apr 15 '14 at 19:34