|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Monday, April 15, 2013 8:48 AM
Points: 74,
Visits: 170
|
|
Hi - I have two tables in the same database. Table1 has a structure like this:
AID IsStaffTask 1 0 2 1 3 1 4 0
Table2 has a structure like this: DID AID 14 1 14 2 15 1 15 2 15 3
What I need to do is verify, for a particular DID, that Table2 has all AID's where IsStaffTask = 1. So, for example, DID 14 does not meet the requirement: it has AID 2, but not 3. DID 15 does meet the requirement: both AID 2 and 3 exist for DID 15. Note: I'm not just concerned with the COUNT of AID's for a DID. I need to verify that there is an AID = 2 and an AID = 3. Also, I can't hard code the values, since Table1's data could change (e.g., we could add to Table1 an AID 5 where IsStaffTask = 1). How do I query this? Thanks!
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, February 27, 2013 6:54 AM
Points: 40,
Visits: 168
|
|
Hi,
Try:
with CTE_T as ( select COUNT(*) as TotIsStaffTask from Table1 where IsStaffTask = 1 ), CTE_D as ( select b.DID, COUNT(*) as TotIsStaffTask from Table2 as b join Table1 as a on a.AID = b.AID and a.IsStaffTask = 1 group by b.DID ) select d.DID from CTE_D as d join CTE_T as t on t.TotIsStaffTask = d.TotIsStaffTask Hope this helps.
http://www.imoveisemexposicao.com.br
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Monday, April 15, 2013 8:48 AM
Points: 74,
Visits: 170
|
|
Hmmm.... Thank you, but if I understand this script, it's just comparing the *number* of total AID's where IsStaffTask = 1 in both tables. I don't just want to compare the totals, I need to verify that each AID in Table1 where IsStaffTask = 1 has a corresponding AID in Table2 for a given DID. So I need to verify that in my example, Table2 has an AID = 2 and an AID = 3 where DID = 15. I'm beginning to think I should just pull the 2 tables into my C# layer and do the comparison there.
If I'm misunderstanding your script, feel free to correct me! And thanks!
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Today @ 8:51 AM
Points: 4,425,
Visits: 7,187
|
|
Table DDL, sample data in the form of INSERT statements, and expected results would have been helpful. In their absence, this is untested:
SELECT AID FROM Table1 t1 LEFT JOIN Table2 t2 IN t1.AID = t2.AID WHERE t1.IsStaffTeak = 1 AND t2.AID IS NULL John
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 9:30 AM
Points: 1,500,
Visits: 18,174
|
|
Try this
DECLARE @Table1 TABLE(AID INT, IsStaffTask INT) INSERT INTO @Table1(AID,IsStaffTask) SELECT 1, 0 UNION ALL SELECT 2, 1 UNION ALL SELECT 3, 1 UNION ALL SELECT 4, 0;
DECLARE @Table2 TABLE(DID INT, AID INT) INSERT INTO @Table2(DID,AID) SELECT 14, 1 UNION ALL SELECT 14, 2 UNION ALL -- 14 is missing 3 SELECT 15, 1 UNION ALL -- 15 has all of 2 and 3 SELECT 15, 2 UNION ALL SELECT 15, 3;
SELECT t2.DID FROM @Table2 t2 INNER JOIN @Table1 t1 ON t1.AID=t2.AID AND t1.IsStaffTask=1 GROUP BY t2.DID HAVING COUNT(*) < (SELECT COUNT(*) FROM @Table1 WHERE IsStaffTask=1);
____________________________________________________
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537
Never approach a goat from the front, a horse from the rear, or a fool from any direction.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Monday, April 15, 2013 8:48 AM
Points: 74,
Visits: 170
|
|
Ten Centuries - thanks, but this is still just comparing COUNTs. I need to verify for DID = 15 not just that there are 2 rows where AID's IsStaffTask = 1, but that those two AID's are 2 and 3. I need to verify that, for example, there aren't 2 AID's = 3, due to some programming bug.
SSCarpal Tunnel - I'd love to send you DDL's and INSERT statements, but everytime I try, my browser crashes. I'm enclosing my code in [code="sql"] blocks, so I don't know what the deal is. Any suggestions would be most welcome.
Thanks, everyone!
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Today @ 8:51 AM
Points: 4,425,
Visits: 7,187
|
|
It may be that your websweeper blocks certain keywords. I've had that happen to me before. Try attaching a .txt file instead.
Please make sure that your provide enough sample data to cover every case (what to do if there are no AIDS, exactly one, more than one, etc).
John
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 9:30 AM
Points: 1,500,
Visits: 18,174
|
|
Melanie Peterson (11/14/2012) Ten Centuries - thanks, but this is still just comparing COUNTs. I need to verify for DID = 15 not just that there are 2 rows where AID's IsStaffTask = 1, but that those two AID's are 2 and 3. I need to verify that, for example, there aren't 2 AID's = 3, due to some programming bug.
Thanks, everyone!
Couple of DISTINCTs should help
DECLARE @Table1 TABLE(AID INT, IsStaffTask INT) INSERT INTO @Table1(AID,IsStaffTask) SELECT 1, 0 UNION ALL SELECT 2, 1 UNION ALL SELECT 3, 1 UNION ALL SELECT 3, 1 UNION ALL SELECT 3, 1 UNION ALL SELECT 4, 0;
DECLARE @Table2 TABLE(DID INT, AID INT) INSERT INTO @Table2(DID,AID) SELECT 14, 1 UNION ALL SELECT 14, 2 UNION ALL SELECT 14, 2 UNION ALL SELECT 14, 2 UNION ALL SELECT 15, 1 UNION ALL SELECT 15, 1 UNION ALL SELECT 15, 2 UNION ALL SELECT 15, 2 UNION ALL SELECT 15, 3;
SELECT t2.DID FROM @Table2 t2 INNER JOIN @Table1 t1 ON t1.AID=t2.AID AND t1.IsStaffTask=1 GROUP BY t2.DID HAVING COUNT(DISTINCT t2.AID) < (SELECT COUNT(DISTINCT AID) FROM @Table1 WHERE IsStaffTask=1);
____________________________________________________
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537
Never approach a goat from the front, a horse from the rear, or a fool from any direction.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Monday, April 15, 2013 8:48 AM
Points: 74,
Visits: 170
|
|
OK, I've attached a file with DDL's for the two tables. I've dropped Table1 and Table2 and given them their real names. Same for column names. Makes it more comprehensible, I think.
With the data attached, LitHoldDetailsID = 14 from the AuditTrail table has two ActionID's, 1 and 2. Only ActionID 2 in the Actions table has IsStaffTask = 1, so it LitHoldDetailsID = 14 does not meet the application requirements (LitHoldDetailsID with ALL IsStaffTask = 1 ActionID's). LitHoldDetailsID = 15, however, does meet the requirements, since it has both ActionID's 2 and 3 and they are all of the Actions in the Actions table where IsStaffTask = 1. What I want to do is input a LitHoldDetailsID and find out if each ActionID from the Actions table where IsStaffTask = 1 has a corresponding row in the AuditTrail table for that LitHoldDetailsID. Hope this makes sense.
Thanks!
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, February 27, 2013 6:54 AM
Points: 40,
Visits: 168
|
|
There may be more than one row in Table2 with the same DID and AID? If may be necessary to adapt the script I suggested. If not, I think the script I suggested is correct, because totals are equal only if there are all AIDs in Table2.
http://www.imoveisemexposicao.com.br
|
|
|
|