Solved

MSSQL Query help

Posted on 2013-12-29
4
331 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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…
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.

911 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

15 Experts available now in Live!

Get 1:1 Help Now