基于fenotipos与atributos表统计符合多条件的女性样本数
Hey there! Let's break down how to calculate the number of samples that meet your criteria. First, let's recap the tables we're working with, then build the solution step by step.
Step 1: Table Data Overview
First, here's the data from both tables for reference:
fenotipos Table
| id | DTHHRDX | sex |
|---|---|---|
| GTEX-1117F | 0 | 2 |
| GTEX-ZE9C | 2 | 1 |
| K-562 | 1 | 2 |
atributos Table
| SAMPID | SMTS |
|---|---|
| K-562-SM-26GMQ | Blood vessel |
| K-562-SM-2AXTU | Blood_dry |
| GTEX-1117F-0003-SM-58Q7G | Brain |
| GTEX-ZE9C-0006-SM-4WKG2 | Brain |
| GTEX-ZE9C-0008-SM-4E3K6 | Urine |
| GTEX-ZE9C-0011-R11a-SM-4WKGG | Urine |
Step 2: Key Conditions to Apply
We need to:
- Join the tables: The
idinfenotiposis the prefix forSAMPIDinatributos(e.g.,K-562matches allSAMPIDvalues starting withK-562). - Filter for female samples:
sex = 2 - Filter for DTHHRDX value 1:
DTHHRDX = 1 - Filter for blood-related SMTS: Match any
SMTSvalue that contains "blood" (case-insensitive to cover bothBlood vesselandBlood_dry).
Step 3: The SQL Query
Here's the query that implements all these rules:
SELECT COUNT(DISTINCT a.SAMPID) AS sample_count FROM fenotipos f JOIN atributos a ON a.SAMPID LIKE CONCAT(f.id, '%') WHERE f.sex = 2 AND f.DTHHRDX = 1 AND LOWER(a.SMTS) LIKE '%blood%';
Let's break this down:
JOIN atributos a ON a.SAMPID LIKE CONCAT(f.id, '%'): Links eachfenotiposrecord to all matchingatributossamples using the prefix match.WHERE f.sex = 2 AND f.DTHHRDX = 1: Applies the filters for female samples and the requiredDTHHRDXvalue.LOWER(a.SMTS) LIKE '%blood%': Ensures we catch anySMTSvalue with "blood" regardless of capitalization.COUNT(DISTINCT a.SAMPID): Counts each unique sample once, even if there were multiple matches (though in this case, each sample is unique).
Step 4: Verify the Result
Running this query will return 2 as expected. The matching samples are:
K-562-SM-26GMQ(SMTS: Blood vessel)K-562-SM-2AXTU(SMTS: Blood_dry)
Both are linked to K-562 in fenotipos, which meets sex=2 and DTHHRDX=1, and their SMTS values include "blood".
内容的提问来源于stack exchange,提问作者Jose Gracia Rodriguez
相关产品推荐
相关产品推荐

