Solved

MSSQL Query help

Posted on 2013-12-29
4
337 Views
Last Modified: 2013-12-30
Hello there,

I have this 2 tables SupplierOrder and SupplierOrderDetails which are linked by SupplierOrder PK. Now  I have this col called isComplete in the SupplierOrder table which I want o update to true once all the values in the SupplierORderDetails table's isComplete are all true for that supplierOrder ID.see the attachment for the tables. I have tried myseolf with this query but I think it could be a better way or more efficient.

SELECT 1 
        FROM supplierOrder so
        inner JOIN supplierOrderdetails sod 
          ON so.id = sod.supplierOrderID
       WHERE so.id = 1
         AND sod.isComplete= 1

Open in new window

12-30-2013-8-19-54-AM.gif
0
Comment
Question by:zolf
4 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
ID: 39745611
Can you try with this update query to update all the records in the table based upon the statement you have provided.

UPDATE supplierOrder
SET isComplete= 1
WHERE id IN (
SELECT supplierOrderID
FROM supplierOrderdetails
WHERE COUNT(*) = SUM(case when iscomplete = 1 THEN 1 else 0 end)
GROUP BY supplierOrderID)
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39745634
For a one-time fix the query below should work.  For ongoing updates I would recommend a trigger.

UPDATE SupplierOrder
SET IsComplete = CASE WHEN SOD.CompleteCount < SOD.ItemCount THEN 0 ELSE 1 END
FROM SupplierOrder AS SO
INNER JOIN
   (
      SELECT SupplierOrderID, COUNT(1) AS ItemCount, SUM(IsComplete) AS CompleteCount
      FROM SupplierOrderDetails
      GROUP BY SupplierOrderID
   ) AS SOD
   ON SO.ID = SOD.SupplierOrderID
0
 

Author Closing Comment

by:zolf
ID: 39745640
thanks!!
0
 
LVL 4

Expert Comment

by:ItWorked
ID: 39745695
Is this what you were searching for?
Create table #SaleOrder
(
	SaleID int,
	IsCompleted bit
)
Create table #SaleOrderDetails
(
	SaleDetailsID int,
	SaleOrderID int,
	IsCompleted bit
)
insert into #SaleOrder values(1,0)
insert into #SaleOrder values(2,0)

insert into #SaleOrderDetails values(1,1,1)
insert into #SaleOrderDetails values(2,1,1)

insert into #SaleOrderDetails values(3,2,1)
insert into #SaleOrderDetails values(4,2,1)
insert into #SaleOrderDetails values(5,2,0)


Select * from #SaleOrder
Select * from #SaleOrderDetails


update OSO
set OSO.IsCompleted=1
from #SaleOrder as OSO
inner join (
				Select so.SaleID
				from #SaleOrder as so
				inner join #SaleOrderDetails sod on so.SaleID=sod.SaleOrderID
				group by so.SaleID
				having count(so.SaleID)=count(case when sod.iscompleted=1 then so.SaleID
                                            else null end)
           )  t on OSO.SaleID=t.SaleID






Select * from #SaleOrder
Select * from #SaleOrderDetails

drop table #SaleOrder
drop table #SaleOrderDetails

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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…
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…

830 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