This is query 1:
SELECT distinct c.chem_gene, m.String_name, m.ScopeNote
FROM mesher m INNER JOIN chem_base c
ON (c.chem_name = m.String_name AND m.ScopeNote <> '' )
where match (c.chem_gene) against('ACTN3, MTHFR' in boolean mode)
group by c.chem_gene, c.chem_name;
which outputs 3 columns in rows like this:
'ACTN3', 'Estradiol', 'The 17-beta-isomer of estradiol...'
This is query 2 (taking the output from column 2, "Estradiol"):
SELECT String10 FROM mesher where String_name = "Estradiol" AND String10 <>'' LIMIT 1;
which outputs a single row in a single column:
'Estrogens'
How can I modify query 1 so that for each row returned the additional query is made against the result in the second column (i.e.'Estradiol') to produce this output:
'ACTN3', 'Estradiol', 'The 17-beta-isomer of estradiol...', 'Estrogens'
If I understand correctly, you can use a correlated subquery:
SELECT c.chem_gene, m.String_name, m.ScopeNote,
(SELECT mm.String10
FROM mesher mm
WHERE mm.String_name = m.String_name AND mm.String10 <> ''
LIMIT 1
)
FROM mesher m INNER JOIN
chem_base c
ON c.chem_name = m.String_name AND m.ScopeNote <> ''
WHERE match(c.chem_gene) against( 'ACTN3, MTHFR' in boolean mode)
GROUP BY c.chem_gene, c.chem_name, m.ScopeNote ;
The select distinct is not necessary.
Related
My SQL query
SELECT DISTINCT a.*,categories_2625729.title AS category_title,categories_2625729.id AS category,c.id as cid, c.title, '' as `introtext`, 'com_hpj_content.ad' as content_type, '' as banner, '' as `fulltext`, '' as sheettext, c.publish_up, c.publish_down, c.price, c.advertiser_id, c.seller_id, c.sold, c.soldupdated,0 AS is_favorite
FROM `jos_hpj_content_item` AS a
LEFT JOIN jos_categories AS categories_2625729 ON categories_2625729.id = a.category
LEFT JOIN jos_hpj_content_item_ad AS cff ON cff.id = a.ref_id AND a.content_type = 'com_hpj_content.ad'
LEFT JOIN jos_fields_values AS fields_values_min_maxyear ON fields_values_min_maxyear.item_id = a.ref_id AND fields_values_min_maxyear.field_id=8
LEFT JOIN jos_fields_values AS fields_values_min_max_2mileage ON fields_values_min_max_2mileage.item_id = a.ref_id AND fields_values_min_max_2mileage.field_id=10
LEFT JOIN jos_hpj_content_item_ad c ON c.`id` = a.`ref_id` AND a.`content_type`='com_hpj_content.ad'
WHERE a.state = 1 AND (`c`.title LIKE '%22%' OR c.`introtext` LIKE '%22%' OR a.synonym LIKE '%22%') AND a.content_type IN ('com_hpj_content.ad') AND a.ref_id IN (53,307,353,354,572,644,717,934,978,1056,1086,1128,1149,1199,1276,1294,1314,1324,1347,1351,1396,1411,1462,1470,1513,1537,1740,1741,1836,1861,1916,1988,1989,2005,2006,2124,2158,2266,2267,2268,2277,2511,2575,2577,2582,2585,2587,2654,2815,2894,2906,2975,3021,3179,3194,3219,3224,3239,0) AND a.state = 1
UNION (SELECT name FROM jos_users WHERE name LIKE '%22%')
I'm getting error
#1222 - The used SELECT statements have a different number of columns
If I remove UNION the sql query return result but if I keep UNION the error return
I'm trying to search the number in different tables in c.title or in name from jos_user and return the result
As #danblack points out in comments, the error
#1222 - The used SELECT statements have a different number of columns
is saying that there's a different number of columns on the two SELECT statement. In
SELECT DISTINCT a.*,
categories_2625729.title AS category_title,
categories_2625729.id AS category,c.id as cid,
c.title,
'' as `introtext`,
'com_hpj_content.ad' as content_type,
'' as banner,
'' as `fulltext`,
'' as sheettext,
c.publish_up,
c.publish_down,
c.price,
c.advertiser_id,
c.seller_id,
c.sold,
c.soldupdated,0 AS is_favorite
FROM ...
..you're select a lot more columns than in this
SELECT name
FROM ...
The number of columns of those two SQL statements have to match in order for UNION to be able to add the results of the second query to the result of the first.
So this will work:
SELECT DISTINCT c.title
FROM ...
UNION
SELECT name
FROM ...
because now both individual SELECT statements return just one column.
I want to include duplicates into my query. I havent succeeded in changing my "in" statements to "joins".
My expected result is a row count of 115.
My result is a row count of 108.
If i do a "Group by" in my first subquery, i get a row count of 108.
select match_id item_0, item_1, item_2, item_3, item_4, item_5, purchase_log
from player_matches where match_id IN
(select x.match_id from (select matches.match_id, picks_bans.team from matches, picks_bans where picks_bans.hero_id = 1 and picks_bans.match_id = matches.match_id and is_pick = true and start_time > 1483228800 ORDER BY start_time DESC) as x
inner join
(select matches.match_id, picks_bans.team from matches, picks_bans where picks_bans.hero_id
/*this is the statement that needs to be tweaked/changed */
IN (2,12,47,4,99) and picks_bans.match_id = matches.match_id and is_pick = true and start_time > 1483228800 ORDER BY start_time DESC) as y on y.match_id=x.match_id and x.team!=y.team)
and hero_id = 1
You can use the opendota inbrowser data-explorer to get a better understanding of my problem.
My subquery (returns 115)
My final Query (returns 108)
How do i get my final query to return 115 row counts?
My query is also really slow, is this because im using "in ()"?
It is because the IN will just check if the value is present. Use INNER JOIN instead:
select a.match_id, a.item_0, a.item_1, a.item_2, a.item_3, a.item_4, a.item_5, a.purchase_log
from player_matches a
INNER JOIN
(
select x.match_id from (select matches.match_id, picks_bans.team from matches, picks_bans where picks_bans.hero_id = 1 and picks_bans.match_id = matches.match_id and is_pick = true and start_time > 1483228800 ORDER BY start_time DESC) as x inner join (select matches.match_id, picks_bans.team from matches, picks_bans where picks_bans.hero_id in (2,12,47,4,5) and picks_bans.match_id = matches.match_id and is_pick = true and start_time > 1483228800 ORDER BY start_time DESC) as y on y.match_id=x.match_id and x.team!=y.team
) b ON a.match_id = b.match_id
WHERE hero_id = 1
Here's a demo from your original link.
I am trying to find count of field by another field in same table on MySQL.My Table Like This:
Id DrgId CodeType IsPrincipal Code
182250051 48261836 I 1 T151
182250055 48261836 I 2 U739
182250059 48261836 I 3 Y929
182250061 48261836 I 4 W444
182250062 48261836 A 2 3006104
So I want to find used helper codes for T151 which is IsPrincipal equals 1.
Output should like this:
Code Helper_I_Count Helper_A_Count
T151 3 1
So I tried Like this:
SELECT t.`Code`,COUNT(v1.`Code`) AS EkTaniSay,COUNT(v2.`Code`) AS IslemSay
FROM TIGPatientCode t,
(
SELECT DRGPatientId,`Code`
FROM TIGPatientCode
WHERE IsPrincipal<>'1' AND CodeType='I'
) v1,
(
SELECT DRGPatientId,`Code`
FROM TIGPatientCode
WHERE IsPrincipal<>'1' AND CodeType='A'
) v2
WHERE t.IsPrincipal='1' AND t.DRGPatientId=v1.DRGPatientId AND t.DRGPatientId=v2.DRGPatientId
GROUP BY t.`Code`
But it wont get actual count.
How can I Do this?
Thanks
SELECT t2.Code,
SUM(t1.CodeType = 'I') AS EkTaniSay,
SUM(t1.CodeType = 'A') AS IslemSay
FROM TIGPatientCode AS t1
RIGHT JOIN TIGPatientCode AS t2 ON t1.DrgPatientId = t2.DrgPatientId
WHERE t1.isPrincipal != 1 AND t2.isPrincipal = 1
GROUP BY t1.DrgPatientId;
The first part of the query is based on multiple query same table but in different columns mysql. Then I join this with the table again to get the code for the principal row.
The problem with your query is that joining the two subqueries creates a cross-product of all the rows, which causes the counts to be multiplied. Also, if there's any group that doesn't have one of the codes, that subquery will return no rows, so the join will be empty for that code. You could fix the first problem by doing the counts in the subqueries rather than the main query. The second problem can be fixed by using LEFT JOIN. So the fixed version of your query would look like:
SELECT t.Code, v1.EkTaniSay, v2.IslemSay
FROM TIGPatientCode t
LEFT JOIN (
SELECT DRGPatientId, COUNT(*) AS EkTaniSay
FROM TIGPatientCode
WHERE IsPrincipal<>'1' AND CodeType='I'
GROUP BY DRGPatientId
) AS v2 ON t.DRGPatientId = v2.DRGPatientId
LEFT JOIN (
SELECT DRGPatientId, COUNT(*) AS IslemSay
FROM TIGPatientCode
WHERE IsPrincipal<>'1' AND CodeType='A'
GROUP BY DRGPatientId
) AS v1 ON t.DRGPatientId = v1.DRGPatientId
WHERE t.IsPrincipal = 1
DEMO
I'm having trouble adding a condition on aliases is_paid, is_overdue and is_outstanding in the following query:
SELECT r.doc_number,
r.doc_date,
r.due_date,
r.currency,
r.amount,
r.vat,
r.vatammount,
(r.amount + r.vatammount) final_amount,
r.currency,
b.boq_id,
b.boq_comp_id,
b.boq_client_id,
b.boq_agency,
b.boq_date,
b.boq_orders,
b.receivable_id,
c.comp_name,
crm.`cn-name-first`,
crm.`cn-name-last`,
bi.inv_path,
(SELECT SUM(amount_recieved)
FROM receivables_payments
WHERE r_id = b.receivable_id) total_amount_received,
IF (r.amount + r.vatammount =
(SELECT SUM(amount_recieved)
FROM receivables_payments
WHERE r_id = b.receivable_id),
'1',
'0') AS is_paid,
IF (CURRENT_DATE >= r.due_date
AND r.amount + r.vatammount !=
(SELECT SUM(amount_recieved)
FROM receivables_payments
WHERE r_id = b.receivable_id),
'1',
'0') AS is_overdue,
IF (r.due_date < CURRENT_DATE
AND r.amount + r.vatammount !=
(SELECT SUM(amount_recieved)
FROM receivables_payments
WHERE r_id = b.receivable_id),
'1',
'0') AS is_outstanding
FROM receivables r
LEFT JOIN boq b ON b.receivable_id = r.id
LEFT JOIN boq_invoices bi ON bi.inv_boq_id = b.boq_id
LEFT JOIN comp_companies c ON c.comp_id = b.boq_comp_id
LEFT JOIN crm_contacts crm ON crm.contact_id = b.boq_client_id
WHERE r.status = 'active'
AND r.doc_type = 'inv'
AND b.boq_status = 'active'
AND is_paid = '1'
ORDER BY r.doc_date DESC LIMIT 10
Is there any way to modify this query and to make it possible to add a condition on those three aliases?
use alias in where condition .. is not allowed . because .is not possibile
the query code is evaluted based on a specified order .. starting from FROM then
WHERE clause and last the SELECT and the column alias so .. when the where is performed the column alias is not available at the query
You could try with an having condition because having work on the result of the query and not on the raw rows value .. (this could have effect on performance ..because all the query is performed and only the result is filtered by having)
With this query, I am getting result with null values and duplicate Ids...
SELECT QuesId,QuesName,[Ketan Mevada],[Parvej],[Parvez Vahora]
FROM (
SELECT tbl_EvolutionAnswer.QuesId,tbl_QuestionMaster.Name as QuesName,
dbo.Evaluation_Calculation_CourseWise(tbl_EvolutionAnswer.QuesId,34,'Course-Green Course-2045',1065, tbl_EvolutionAnswer.TrainerId ) as Average,
tbl_EvolutionAnswer.TrainerId,
tbl_TrainerMaster.Name as TrName
from tbl_EvolutionAnswer
inner join tbl_TrainerMaster
on tbl_EvolutionAnswer.TrainerId = tbl_TrainerMaster.Id
inner join tbl_QuestionMaster
on tbl_EvolutionAnswer.QuesId = tbl_QuestionMaster.QuestionId
where tbl_EvolutionAnswer.EvolId =34
and tbl_EvolutionAnswer.TrainerId <> 0
and tbl_EvolutionAnswer.CourseId = 'Course-Green Course-2045'
and tbl_EvolutionAnswer.SchID = 1065
) as Books
PIVOT (
MAX(Average) FOR TrName IN ([Ketan Mevada],[Parvej],[Parvez Vahora])
) as Result
I need Following Output
QuesId QuesName Ketan Mevada Parvej Parvez Vohra
122 Did your trainer answer... 2 3 2
123 was your trainer activ.. 1 4 3
It appears that you have a column inside your subquery that is unique and it is causing the grouping of the aggregate function to be skewed. When you are using the PIVOT function, you should only include the columns needed for the PIVOT and the final select list, otherwise you run the risk of the end result being spilt over multiple rows.
It looks like the column you need to remove is tbl_EvolutionAnswer.TrainerId. Making your actual query:
SELECT QuesId,QuesName,[Ketan Mevada],[Parvej],[Parvez Vahora]
FROM
(
SELECT tbl_EvolutionAnswer.QuesId,
tbl_QuestionMaster.Name as QuesName,
dbo.Evaluation_Calculation_CourseWise(tbl_EvolutionAnswer.QuesId,34,'Course-Green Course-2045',1065, tbl_EvolutionAnswer.TrainerId ) as Average,
tbl_TrainerMaster.Name as TrName
from tbl_EvolutionAnswer
inner join tbl_TrainerMaster
on tbl_EvolutionAnswer.TrainerId = tbl_TrainerMaster.Id
inner join tbl_QuestionMaster
on tbl_EvolutionAnswer.QuesId = tbl_QuestionMaster.QuestionId
where tbl_EvolutionAnswer.EvolId =34
and tbl_EvolutionAnswer.TrainerId <> 0
and tbl_EvolutionAnswer.CourseId = 'Course-Green Course-2045'
and tbl_EvolutionAnswer.SchID = 1065
) as Books
PIVOT (
MAX(Average) FOR TrName IN ([Ketan Mevada],[Parvej],[Parvez Vahora])
) as Result