Solved

update a column within a join statement and group by clause

Posted on 2013-11-04
6
226 Views
Last Modified: 2013-11-05
I have pulled up the following recordset.

SELECT Max(Text4)Text4,Max(Text5)Text5, Sum(Total)TotalsperStudent FROM AttendanceGrid INNER JOIN tblRegistration ON AttendanceGrid.[ID]=tblRegistration.[ID] Where  AttendanceGrid.Fiscal = '" & Fiscal & "' And AttendanceGrid.[CSW/TitleXX] = 'C' And AttendanceGrid.Month = '" & Com(z) & "' And NewPart = 0 And Combo11 like '" & rec1.Fields(0) & "' Group by tblRegistration.ID HAVING Sum(Total)>0

Open in new window


Within this recordset, I would like to update the column called NewPart = 1 in the table called AttendanceGrid.  

How is this possible?
0
Comment
Question by:al4629740
  • 2
  • 2
  • 2
6 Comments
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39622973
update AttendanceGrid set NewPart = 1 Where AttendanceGrid.[ID] in (SELECT Max(Text4)Text4,Max(Text5)Text5, Sum(Total)TotalsperStudent FROM AttendanceGrid INNER JOIN tblRegistration ON AttendanceGrid.[ID]=tblRegistration.[ID] Where  AttendanceGrid.Fiscal = '" & Fiscal & "' And AttendanceGrid.[CSW/TitleXX] = 'C' And AttendanceGrid.Month = '" & Com(z) & "' And NewPart = 0 And Combo11 like '" & rec1.Fields(0) & "')
0
 

Author Comment

by:al4629740
ID: 39623293
Sorry, I meant to say "NewPart = 1 in the table called tblRegistration.

Would it be:

update AttendanceGrid set NewPart = 1 Where tblRegistration.[ID] in (SELECT Max(Text4)Text4,Max(Text5)Text5, Sum(Total)TotalsperStudent FROM AttendanceGrid INNER JOIN tblRegistration ON AttendanceGrid.[ID]=tblRegistration.[ID] Where  AttendanceGrid.Fiscal = '" & Fiscal & "' And AttendanceGrid.[CSW/TitleXX] = 'C' And AttendanceGrid.Month = '" & Com(z) & "' And NewPart = 0 And Combo11 like '" & rec1.Fields(0) & "')
0
 

Author Comment

by:al4629740
ID: 39623457
I tried the following with no success:

update tblRegistration set NewPart = 1 Where tblRegistration.[ID] in (SELECT * FROM AttendanceGrid INNER JOIN tblRegistration ON AttendanceGrid.[ID]=tblRegistration.[ID] Where  AttendanceGrid.Fiscal = '2013' And AttendanceGrid.[CSW/TitleXX] = 'C' And AttendanceGrid.Month = 'September' And NewPart = 0 And  Total > 0 And Combo11 like 'Alternatives')

Error:
Msg 116, Level 16, State 1, Line 1
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 9

Assisted Solution

by:QuinnDex
QuinnDex earned 100 total points
ID: 39623566
try this

update tblRegistration set NewPart = 1 Where exists tblRegistration.[ID] in (SELECT * FROM AttendanceGrid INNER JOIN tblRegistration ON AttendanceGrid.[ID]=tblRegistration.[ID] Where  AttendanceGrid.Fiscal = '2013' And AttendanceGrid.[CSW/TitleXX] = 'C' And AttendanceGrid.Month = 'September' And NewPart = 0 And  Total > 0 And Combo11 like 'Alternatives')
0
 
LVL 12

Expert Comment

by:Paul_Harris_Fusion
ID: 39623855
update tblRegistration set NewPart = 1
where ID in
(
SELECT tblRegistration.ID
FROM AttendanceGrid INNER JOIN tblRegistration
ON AttendanceGrid.[ID]=tblRegistration.[ID]
Where  
AttendanceGrid.Fiscal = '" & Fiscal & "'
And AttendanceGrid.[CSW/TitleXX] = 'C'
And AttendanceGrid.Month = '" & Com(z) & "'
And NewPart = 0
And Combo11 like '" & rec1.Fields(0) & "'
Group by tblRegistration.ID
HAVING Sum(Total)>0  
)
0
 
LVL 12

Accepted Solution

by:
Paul_Harris_Fusion earned 400 total points
ID: 39623860
Or specifically with your runtime values:

update tblRegistration set NewPart = 1
Where ID in
(
SELECT tblRegistration.ID
FROM AttendanceGrid INNER JOIN tblRegistration
ON AttendanceGrid.[ID]=tblRegistration.[ID]
Where  AttendanceGrid.Fiscal = '2013'
And AttendanceGrid.[CSW/TitleXX] = 'C'
And AttendanceGrid.Month = 'September'
And NewPart = 0
And  Total > 0
And Combo11 like 'Alternatives'
)
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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

856 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