Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Stuck on Query to Conditionally Exclude Data Expand / Collapse
Author
Message
Posted Monday, August 25, 2014 1:30 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, September 3, 2014 1:50 PM
Points: 20, Visits: 34
Eirikur, with this test data the expected result set would be:

C1473045850  T1954  3730  ''
C1473045850 T1954 3731 ''
C1473045850 T1954 3732 ''
C1473045850 T1954 3733 ''

(not sure how to indicate the blanks so I used empty quotes, hope that's OK).
Post #1607217
Posted Monday, August 25, 2014 1:35 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 4:14 PM
Points: 1,024, Visits: 3,076
What would you expect for the following data?
INSERT INTO PRE_LOAD
(KitID, BatteryID, TestID, LBSTAT)
SELECT 'C1473045850','T1878','3730','NOT DONE' UNION ALL
SELECT 'C1473045850','T1878','3731','NOT DONE' UNION ALL
SELECT 'C1473045850','T1878','3732','NOT DONE' UNION ALL
SELECT 'C1473045850','T1878','3733','NOT DONE' UNION ALL
SELECT 'C1473045850','T1954','3730','' UNION ALL
SELECT 'C1473045850','T1954','3731',''

Post #1607222
Posted Monday, August 25, 2014 1:40 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:12 PM
Points: 12,928, Visits: 12,345
jkalmar 43328 (8/25/2014)
Eirikur, with this test data the expected result set would be:

C1473045850  T1954  3730  ''
C1473045850 T1954 3731 ''
C1473045850 T1954 3732 ''
C1473045850 T1954 3733 ''

(not sure how to indicate the blanks so I used empty quotes, hope that's OK).


Seems that what I posted should work for you then?


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1607223
Posted Monday, August 25, 2014 1:44 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 12:03 PM
Points: 1,911, Visits: 5,264
jkalmar 43328 (8/25/2014)
Eirikur, with this test data the expected result set would be:

C1473045850  T1954  3730  ''
C1473045850 T1954 3731 ''
C1473045850 T1954 3732 ''
C1473045850 T1954 3733 ''

(not sure how to indicate the blanks so I used empty quotes, hope that's OK).


Good stuff, think I might be missing something though as the following query will produce this results but feels kind of "too simple" given the original requirements


SELECT
*
FROM dbo.PRE_LOAD PL
WHERE PL.LBSTAT <> 'NOT DONE'



Sean Lange already posted a solution that produces the same results, is that what you need?
Post #1607224
Posted Monday, August 25, 2014 1:52 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 12:02 PM
Points: 3,575, Visits: 8,017
Here's an extended set of sample data and the solutions posted. I guess Micky's got the correct formula.
CREATE TABLE PRE_LOAD(
KitID varchar(20) NULL,
BatteryID varchar(20) NULL,
TestID varchar(20) NULL,
LBSTAT varchar(8) NULL
)

INSERT INTO PRE_LOAD
(KitID, BatteryID, TestID, LBSTAT)
SELECT 'C1473999999','T5555','3730','NOT DONE' UNION ALL
SELECT 'C1473999999','T5555','3731','NOT DONE' UNION ALL
SELECT 'C1473045850','T1878','3730','NOT DONE' UNION ALL
SELECT 'C1473045850','T1878','3731','NOT DONE' UNION ALL
SELECT 'C1473045850','T1878','3732','NOT DONE' UNION ALL
SELECT 'C1473045850','T1878','3733','NOT DONE' UNION ALL
SELECT 'C1473045850','T1954','3730','' UNION ALL
SELECT 'C1473045850','T1954','3731','' UNION ALL
SELECT 'C1473045850','T1954','3732','' UNION ALL
SELECT 'C1473045850','T1954','3733','' UNION ALL
SELECT 'C1111111111','T1234','3737','' UNION ALL
SELECT 'C1111111111','T1234','3737','' UNION ALL
SELECT 'C1111111111','T4444','3738',''

select *
from PRE_LOAD
where BatteryID not in
(
select BatteryID
from PRE_LOAD
where LBSTAT = 'NOT DONE'
)

select *
from PRE_LOAD ex
where EXISTS
(
select 1
from PRE_LOAD i
where LBSTAT <> 'NOT DONE'
AND ex.kitID = i.KitID
)

SELECT p.KitID,
p.BatteryID,
p.TestID,
p.LBSTAT
FROM PRE_LOAD p
CROSS APPLY (
SELECT COUNT(*) chk
FROM PRE_LOAD c
WHERE p.KitID = c.KitID AND
p.TestID = c.TestID AND
c.LBSTAT <> 'NOT DONE'
) x
WHERE LBSTAT <> 'NOT DONE' OR
x.chk = 0
GO
DROP TABLE PRE_LOAD




Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1607228
Posted Monday, August 25, 2014 2:25 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, September 3, 2014 1:50 PM
Points: 20, Visits: 34
Actually Luis, yours gives the closest to the needed result on the actual data. With some tuning I think it's going to work for me. Thanks to everyone for their help!
Post #1607241
Posted Monday, August 25, 2014 2:35 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 4:14 PM
Points: 1,024, Visits: 3,076
Can you post up your solution when you have it tuned thanks.
Post #1607247
Posted Tuesday, August 26, 2014 9:42 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, September 3, 2014 1:50 PM
Points: 20, Visits: 34
The solution is going to look very different because they changed the requirements on me a bit. Here's the final code:

WITH ctePLC (cKitID, cStdBatteryName)
AS
(
SELECT KitID, StdBatteryName
FROM (
SELECT KitId
,StdBatteryName
,COUNT(DISTINCT StdBatteryId) AS bCount
FROM PRE_LOAD
GROUP BY KitId, StdBatteryName
) AS t2
WHERE t2.bCount > 1
)
,
ctePLD (dKitID, dStdBatteryName)
AS
(
SELECT DISTINCT t1.KitID, t1.StdBatteryName FROM PRE_LOAD AS t1
INNER JOIN ctePLC
ON t1.KitID = ctePLC.cKitID
AND t1.StdBatteryName = ctePLC.cStdBatteryName
AND t1.TestStatus = 'D'
)
,
ctePLN (nKitID, nStdBatteryName)
AS
(
SELECT DISTINCT t1.KitID, t1.StdBatteryName FROM PRE_LOAD AS t1
INNER JOIN ctePLC
ON t1.KitID = ctePLC.cKitID
AND t1.StdBatteryName = ctePLC.cStdBatteryName
AND t1.TestStatus = 'N'
)
,
ctePLX (xKitID, xStdBatteryName)
AS
(
SELECT DISTINCT t1.KitID, t1.StdBatteryName FROM PRE_LOAD AS t1
INNER JOIN ctePLC
ON t1.KitID = ctePLC.cKitID
AND t1.StdBatteryName = ctePLC.cStdBatteryName
AND t1.TestStatus = 'X'
)

SELECT *
FROM PRE_LOAD AS t1
INNER JOIN ctePLC AS c
ON t1.KitID = c.cKitID
AND t1.StdBatteryName = c.cStdBatteryName
LEFT JOIN ctePLD AS d
ON c.cKitID = d.dKitID
AND c.cStdBatteryName = d.dStdBatteryName
LEFT JOIN ctePLN AS n
ON c.cKitID = n.nKitID
AND c.cStdBatteryName = n.nStdBatteryName
LEFT JOIN ctePLX AS x
ON c.cKitID = x.xKitID
AND c.cStdBatteryName = x.xStdBatteryName
WHERE
t1.TestStatus = 'N'
AND (
EXISTS (
SELECT t1.KitID, t1.StdBatteryName
FROM PRE_LOAD AS t1
WHERE t1.KitID = d.dKitID
AND t1.StdBatteryName = d.dStdBatteryName
)
OR
EXISTS (
SELECT t1.KitID, t1.StdBatteryName
FROM PRE_LOAD AS t1
WHERE t1.KitID = x.xKitID
AND t1.StdBatteryName = x.xStdBatteryName
)
)
OR
t1.TestStatus = 'X'
AND (
EXISTS (
SELECT t1.KitID, t1.StdBatteryName
FROM PRE_LOAD AS t1
WHERE t1.KitID = d.dKitID
AND t1.StdBatteryName = d.dStdBatteryName
)
AND
NOT EXISTS (
SELECT t1.KitID, t1.StdBatteryName
FROM PRE_LOAD AS t1
WHERE t1.KitID = n.nKitID
AND t1.StdBatteryName = n.nStdBatteryName
)
)
ORDER BY t1.KitID, t1.stdbatteryname,t1.stdbatteryid,t1.TestStatus

It selects all batteries under the same KitID where the TestStatus is 'N' and there exists a battery with the same name under that same KitID with a TestStatus of 'D' or 'X', and all batteries under the same KitID where the TestStatus is 'X' and there exists a battery with the same name under that same KitID with a TestStatus of 'D' but NO battery with the same name with a TestStatus of 'N'.

The ultimate goal is to delete all 'N' if there's a 'D' or an 'X' and all 'X' if there's a 'D' but no 'N'. I'm having trouble getting my DELETE statement to work, but I'll post that as a new thread.

Again, thanks to all!
Post #1607508
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse