Solved

Include DISTINCT selection

Posted on 2012-04-10
10
177 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 7

Expert Comment

by:Cboudroz
Comment Utility
error message?
0
 

Author Comment

by:al4629740
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
-->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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now