Solved

# Include DISTINCT selection

Posted on 2012-04-10
177 Views
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
Question by:al4629740
• 3
• 3
• 3
• +1

LVL 7

Accepted Solution

Cboudroz earned 333 total points
``````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
``````
0

LVL 14

Assisted Solution

nishant joshi earned 167 total points
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
``````

Regards,
nishant
0

Author Comment

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

error message?
0

Author Comment

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

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

``````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
``````
0

Author Comment

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

a which is an alias of attendancegrid table and t which is an alias of tbljjdp .
0

LVL 7

Assisted Solution

Cboudroz earned 333 total points
-->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
``````

``````	(
SELECT DISTINCT
Activity
FROM
AttendanceGrid
) AS A
``````
0

## Featured Post

### Suggested Solutions

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.