Solved

MSSQL Query help

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

747 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

13 Experts available now in Live!

Get 1:1 Help Now