Solved

Include DISTINCT selection

Posted on 2012-04-10
10
185 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

803 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