Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

java - Selecting parent records of children that meet multiple conditions (but not together) with SQLite

I don't know why I can wrap my head around this query. I think I am not asking the correct question which is why I am struggling to find an answer.

I have two tables. items and skus. I am trying to identify items that have skus that meet both the following conditions:

  1. Has a SKU with a price_date > specified date
  2. Has a SKU with a custom = 1.

Using the below example:

items


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of JOIN skus, we need to first figure out which ItemID-s qualify.. I am using MCNT as table prefix Multiple Conditions Not Together...

SELECT [ItemID]
  FROM [StackOver].[dbo].[MCNTskus]
  Group By [ItemID]
  Having 
       max([price_date]) > ('2020-12-01')
     and 
       max([custom])  = 1

Then use that as FROM and do LeftJoin of items

With qualSKUs as
(SELECT [ItemID]
  FROM [StackOver].[dbo].[MCNTskus]
  Group By [ItemID]
  Having 
       max([price_date]) > ('2020-12-01')
     and 
       max([custom])  = 1
)
SELECT MCNTitems.ID, MCNTitems.Name
    FROM qualSKUs
LEFT JOIN MCNTitems ON MCNTitems.ID=qualSKUs.ItemID
GROUP BY qualSKUs.ItemID,MCNTitems.ID, MCNTitems.Name

Resulting in

ID  Name
1   My Item 1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...