Solved

Include DISTINCT selection

Posted on 2012-04-10
10
186 Views
Last Modified: 2012-04-12
I have the following sql string:
SELECT tblJJDP.ID,Max(LTRIM(RTRIM(Nu)))Nu, Sum(Total)TotalsperStudent   FROM AttendanceGrid INNER JOIN tblJJDP ON AttendanceGrid.[ID]=tblJJDP.[ID] Where tblJJDP.[CSW/TitleXX] = 'I' And Combo6 = 'Alternatives' And [Month]= 'January' Group by tblJJDP.ID

I want to modify it so that I only select DISTINCT Activity from my table called Attendance Grid.

How can I do that?
0
Comment
Question by:al4629740
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 7

Accepted Solution

by:
Cboudroz earned 333 total points
ID: 37828928
SELECT 
	tblJJDP.ID
	,Max(LTRIM(RTRIM(Nu)))Nu
	, Sum(Total)TotalsperStudent   
FROM 
	(
		SELECT DISTINCT
			*
		FROM 
			AttendanceGrid 
	) AS A
	INNER JOIN tblJJDP 
		ON A.[ID]=tblJJDP.[ID] 
WHERE 
	tblJJDP.[CSW/TitleXX] = 'I' 
	And Combo6 = 'Alternatives' 
	And [Month]= 'January' 
Group by 
	tblJJDP.ID

Open in new window

0
 
LVL 14

Assisted Solution

by:nishant joshi
nishant joshi earned 167 total points
ID: 37828958
check delow code you will get distinct activity from attendancegrid


SELECT a.id,a.Activity 
       MAX(Ltrim(Rtrim(nu))) as nu, 
       SUM(total)   as totalsperstudent 
FROM   attendancegrid a
INNER JOIN tbljjdp  t
ON a.[ID] = t.[ID] 
WHERE  t.[CSW/TitleXX] = 'I' 
       AND combo6 = 'Alternatives' 
       AND [Month] = 'January' 
GROUP  BY a.Activity,a.id  

Open in new window


Regards,
nishant
0
 

Author Comment

by:al4629740
ID: 37829436
This is what I have.  Its NOT working

SELECT tblJJDP.ID,Max(LTRIM(RTRIM(Nu)))Nu, Sum(Total)TotalsperStudent   FROM (SELECT DISTINCT Activity FROM AttendanceGrid ) as a INNER JOIN tblJJDP ON AttendanceGrid.[ID]=tblJJDP.[ID] Where tblJJDP.[CSW/TitleXX] = 'I' And Combo6 = 'Alternatives' And [Month]= 'January' Group by tblJJDP.ID
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 7

Expert Comment

by:Cboudroz
ID: 37829456
error message?
0
 

Author Comment

by:al4629740
ID: 37830103
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "AttendanceGrid.ID" could not be bound.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Month'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Nu'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Total'.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37830696
This is what I have.  Its NOT working
Is there any reason you cannot use Cboudroz solution posted here: http:#a37828928
0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 37830928
SELECT a.id,a.Activity 
       MAX(Ltrim(Rtrim(a.nu))) as nu, 
       SUM(a.total)   as totalsperstudent 
FROM   attendancegrid a
INNER JOIN tbljjdp  t
ON a.[ID] = t.[ID] 
WHERE  t.[CSW/TitleXX] = 'I' 
       AND a.combo6 = 'Alternatives' 
       ANDa.[Month] = 'January' 
GROUP  BY a.Activity,a.id  

Open in new window

0
 

Author Comment

by:al4629740
ID: 37831083
Sorry, I was confused a bit.  what are the letters like "a" and "t"

are they just abbreviations not meant to be typed that way in actual code?
0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 37831171
a which is an alias of attendancegrid table and t which is an alias of tbljjdp .
0
 
LVL 7

Assisted Solution

by:Cboudroz
Cboudroz earned 333 total points
ID: 37832750
-->Is there any reason you cannot use Cboudroz solution posted here: http:#a37828928

You change the select distinct "*" by "Activity", so now the other column in AttendanceGrid are not return anymore : (ID,Month, NU, Total)

	(
		SELECT DISTINCT
			*
		FROM 
			AttendanceGrid 
	) AS A

Open in new window


	(
		SELECT DISTINCT
			Activity
		FROM 
			AttendanceGrid 
	) AS A

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question