2017-02-14 45 views
4

我敢肯定,我可以做這個職位的標題更好的工作。我不知道如何將這個問題縮小到一個工作的標題...計數()項目計數是一個聯接回到另一個表

我有兩個表。 BillItems和SubItems。

子項:

SubItemId ItemId MasterItemId 
----------------------------------- 
    1   50   10 
    2   50   11 
    3   60   10 
    4   60   12 
    5   70   10 

BillItems:

BillItemId ItemId 
--------------------- 
    1   10 
    2   11 
    3   50 
    4   60 
    5   70 

好了,現在,我需要知道,如果BillItems包含的孩子一個以上的MasterItem,其中MasterItem也是任何物品在BillItems表內。我知道這聽起來令人困惑,所以我舉個例子:

項目50是項目10和項目11的子項目。項目10和項目11都在BillItems表格中。所以,我需要項目50以顯示在查詢中。

項目60是項目10和項目12的子項。項目10在BillItems表中,但項目12不在項目10中。所以我不希望項目60出現在查詢中。

項目10和11不是子項目中的子項目。所以我不希望它出現在查詢中。

編輯:

所需的輸出,與上述數據,將是簡單的:

ItemId 
------ 
    50 
+0

也許還顯示所需的輸出? –

+0

好主意。完成。 –

+0

這兩個表都與ItemId相關,對吧? – octavioccl

回答

3

我相信這就是你要找的東西:

SELECT si.ItemId 
FROM SubItems si 
WHERE EXISTS (SELECT 1 -- This EXISTS may be omitted if SubItems.ItemId has an enforced FOREIGN KEY reference to BillItems.ItemId 
       FROM BillItems bi 
       WHERE bi.ItemId = si.ItemId) 
    AND EXISTS (SELECT 1 
       FROM BillItems bi 
       WHERE bi.ItemId = si.MasterItemId) 
GROUP BY si.ItemId 
HAVING COUNT(DISTINCT si.MasterItemId) > 1; 
+0

謝謝!測試 –

3

你可以做這樣的事情:

var result=from b in context.BillItems 
      let masters=context.SubItems.Where(s=>s.ItemId==b.ItemId).Select(s=>s.MasterItemId) 
      where masters.All(e=>context.BillItems.Any(x=>x.ItemId==e)) 
      select b.ItemId; 

我注意到後面這個是一個sql問題:),但@Casey想要看看如何在Linq中。 (使用EF和導航性能的情況下),另一種解決辦法是:

var result=context.BillItems.Where(b=>b.SubItems.All(s=>context.BillItems.Any(x=>x.ItemId==s.MasterItemId)) 
          .Select(e=>e.ItemId); 

或者也做得組加入:

var result=from b in context.BillItems 
      join s in context.SubItems on b.ItemId equals s.ItemId into g 
      where g.All(e=>context.BillItems.Any(x=>x.ItemId==e.MasterItemId)) 
      select b.ItemId; 
1

你需要的東西是這樣的:

SELECT * FROM 
    (SELECT DISTINCT ItemId FROM TABLE1 t WHERE t.MasterId IN (SELECT ItemId FROM TABLE2)) as MasterInTable2 
    EXCEPT 
    (SELECT DISTINCT ItemId FROM TABLE1 t WHERE t.MasterId NOT IN (SELECT ItemId FROM TABLE2)) as 
    MasterNotInTable2 
+0

謝謝!測試 –

+0

我沒有檢查語法,但你應該很好,我認爲 – URWelcome

+0

對不起,我沒有仔細閱讀這個問題,你需要添加計數部分,以檢查它是否有超過1個masterId – URWelcome

3

您可以使用相關的子查詢來檢查billitems表中是否存在masteritemid。對於不存在的ID,您可以獲得0的計數。然後您可以group by itemid,並消除所有這些物品存在缺少物品且itemid組中有多個物品的情況。

select itemid 
from (select itemid,(select count(*) from billitems where s.masteritemid=itemid) as present_or_not 
     from subitems s 
    ) x 
group by itemid 
having count(case when present_or_not=0 then 1 end)=0 and count(*) > 1 
1
CREATE TABLE #SubItems (
          id INT IDENTITY(1,1), 
          subItemID INT, 
          ItemID  INT, 
          MasterItemID INT 
         ) 

INSERT INTO #SubItems 
VALUES(1,50,10) 
INSERT INTO #SubItems 
VALUES(2,50,11) 
INSERT INTO #SubItems 
VALUES(3,60,10) 
INSERT INTO #SubItems 
VALUES(4,60,12) 
INSERT INTO #SubItems 
VALUES(5,70,10) 

CREATE TABLE #BillItems (
          id INT IDENTITY(1,1), 
          BillItemID INT, 
          ItemID  INT 
         ) 

INSERT INTO #BillITems 
VALUES(1,10)  
INSERT INTO #BillITems 
VALUES(2,11)  
INSERT INTO #BillITems 
VALUES(3,50)  
INSERT INTO #BillITems 
VALUES(4,60)  
INSERT INTO #BillITems 
VALUES(5,70)  


SELECT A.ItemID 
FROM (
     SELECT bi.ItemID, COUNT(*) AS CountBill 
     FROM #BillItems bi 
     JOIN #SubItems si ON 
      bi.ItemID = si.ItemID 
     GROUP BY bi.ItemID 
    ) A 
     JOIN #SubItems si ON 
      A.ItemID = si.ItemID 
WHERE si.MasterItemID IN (SELECT ItemID FROM #BillItems) 
GROUP BY A.ItemID 
HAVING COUNT(*) > 1 



DROP TABLE #SubItems 
DROP TABLE #BillItems 
3

希望我理解正確你的問題。

請檢查下面的查詢

select a.ItemId ItemId_a 
    --, count(distinct a.MasterItemId) , count(distinct b.ItemId) 
    from SubItems a left join BillItems b 
    on a.MasterItemId = b.ItemId 
    group by a.ItemId 
    having count(distinct a.MasterItemId) = count(distinct b.ItemId) 
    and count(distinct a.MasterItemId)>1 
    ; 
+0

這將無法返回具有多於兩個MasterItemId的項目的ItemId,其中MasterItemId也不在BillItems中。示例:INSERT @SubItems VALUES (1,50,10), (2,50,11), (3,60,10), (4,60,12), (5,70,10 ), (6,60,80); INSERT @BillItems VALUES (1,10), (2,11), (3,50), (4,60), (5,70), (6,80);' –

+0

好的,謝謝我正在檢查。請分享您提到的輸入內容。 – Tajinder

2

我的兩分錢:

/* 
create table BillItems (BillItemId int, ItemId int) 
create table Subitems (SubitemId int, ItemId int, MasterItemId int) 

insert BillItems values (1,10) 
insert BillItems values (2,11) 
insert BillItems values (3,50) 
insert BillItems values (4,60) 
insert BillItems values (5,70) 

insert Subitems values(1,50,10) 
insert Subitems values(2,50,11) 
insert Subitems values(3,60,10) 
insert Subitems values(4,60,12) 
insert Subitems values(5,70,10) 
*/ 

;with x as (select itemId from subitems group by itemId having count(*) > 1) 
, y as (select s.ItemId, b.BillItemId from x join subitems s on x.itemid = s.itemid left join Billitems b on s.MasterItemId = b.ItemID) 
select distinct itemid from y 
except 
select itemid from y where billitemid is null 
相關問題