SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SELECT - CASE - GROUP
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

carl6885
Starting Member

United Kingdom
2 Posts

Posted - 08/12/2012 :  16:18:16  Show Profile  Reply with Quote
Hi

I am new to this forum, so hopefully im in the right place and thank you in advance.

Overview: An error is entered into the table, across two tables - tblErrors_ER and tblPolicyNumbers_ER - each error generates a PK (ErrorID) and can have any number of policy numbers which will be referenced by its own PK but linked to each error by its FK (ErrorID).

I want to display each error in a Gridview in ASP.Net - columns included will be ErrorID, ErrorType, DateLogged from tblErrors_ER and PolicyNumber from tblPolicyNumbers_ER.

If an Error has more than one policy number I only want to show the error once in the GridView with the word MULTIPLE under policy number.

The scripts for creating the two tables are as follows:

tblErrors_ER
---------------
CREATE TABLE tblErrors_ER
{
ErrorID int,
ErrorType varchar(255),
DateLogged datetime,
}

tblPolicyNumbers
----------------------

CREATE TABLE tblPolicyNumbers_ER
{
PolicyNumberID int,
ErrorID int,
PolicyNumber varchar(10)
}

My ASP.Net page is titled Dashboard.aspx which contains the Gridview - I configure the datasource using the smart tags. When given the option I write a custom SQL string.

My first SQL string was:

SELECT tblErrors_ER.ErrorID, tblErrors_ER.ErrorType, tblErrors_ER.DateLogged, CASE WHEN Count(*) = 1 THEN tblPolicyNumbers.PolicyNumber ELSE 'MULTIPLE' END
FROM tblErrors_ER INNER JOIN tblPolicyNumbers_ER ON
tblErrors_ER.ErrorID = tblPolicyNumbers_ER.ErrorID
GROUP BY tblErrors_ER.ErrorID, tblErrors_ER.ErrorType, tblErrors_ER.DateLogged, tblPolicyNumbers_ER.PolicyNumbers.PolicyNumbers

This generates the following results in the GridView:

ErrorID ErrorType DateLogged PolicyNumber
---------------------------------------------------------------
1 Test 08/08/2012 1234567xx
2 Test 08/08/2012 123458xx
2 Test 08/08/2012 999999xx

The desired results would be:

ErrorID ErrorType DateLogged PolicyNumber
---------------------------------------------------------------
1 Test 08/08/2012 1234567xx
2 Test 08/08/2012 Multiple

I have changed the Count(*) to Count(tblPolicyNumbers_ER.POlicyNUmber) which gives me the same undesired result as above. I have also left it as Count(*) and the entire CASE expression within the GROUP BY statement as suggest above which generated an error saying I can not use an expression in a group by clause.

If I leave Count(*) = 1 where it is in the original SELECT statement but swap the = for > then something happens, close to what I require but not as intended. It returns:

ErrorID ErrorType DateLogged PolicyNumber
---------------------------------------------------------------
1 Test 08/08/2012 Multiple
2 Test 08/08/2012 Multiple

this would suggest the original syntax is close to being accurate but I can not get it to work.


Any help would be greatly appreciated.

Thanks

Carl

visakh16
Very Important crosS Applying yaK Herder

India
48059 Posts

Posted - 08/12/2012 :  17:31:53  Show Profile  Reply with Quote

SELECT tblErrors_ER.ErrorID, tblErrors_ER.ErrorType, tblErrors_ER.DateLogged, 
CASE WHEN Count(tblPolicyNumbers.PolicyNumber) = 1 THEN MAX(tblPolicyNumbers.PolicyNumber) ELSE 'MULTIPLE' END
FROM tblErrors_ER 
INNER JOIN tblPolicyNumbers_ER ON
tblErrors_ER.ErrorID = tblPolicyNumbers_ER.ErrorID
GROUP BY tblErrors_ER.ErrorID, tblErrors_ER.ErrorType, tblErrors_ER.DateLogged


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

carl6885
Starting Member

United Kingdom
2 Posts

Posted - 08/13/2012 :  01:07:35  Show Profile  Reply with Quote
Thank you so much!! Cant believe all I was missing was the MAX(XXX), MIN(XXX) also works!! Thank you!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48059 Posts

Posted - 08/13/2012 :  10:11:42  Show Profile  Reply with Quote
welcome. yep both will work so far as you've single value within a group

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000