I would like to create a table where all data from "treatment_parameters" is used and where parameter_fk=47. Additionally, the table should also include only the count(*) of rows from "treatment_log" where the missed_treatment=' '.
So the table should project out all data from "treatment_parameters" with parameter_fk=47 and the number of rows in "treatment_log" where missed_treatment=' '.
Should also note that: t.id = l.treatment_fk
I have attempted this, however it is partially successful. The table displays all required data, however it does not display every row from "treatment_paratmers" with parameter_fk=47. (There should be two different rows)
SELECT t.id,t.parameter_fk,t.course_name,t.room, t.protocol,t.navigation,t.area,t.coil,t.number_of_treatments,t.motor_threshold,t.threshold_multiplier,t.target_threshold,t.about,t.date,t.created_at, COUNT(*) AS completed_treatments
FROM treatment_parameters t
LEFT JOIN treatment_log l
ON t.id = l.treatment_fk
WHERE t.parameter_fk = 47
AND l.missed_treatment=' '
Below you can see the result of the above SQL query:
Image 1
However, you can see that when the SQL query is modified, there is more than just one row for paratmer_fk=47
SELECT t.id,t.parameter_fk,t.course_name,t.room, t.protocol,t.navigation,t.area,t.coil,t.number_of_treatments,t.motor_threshold,t.threshold_multiplier,t.target_threshold,t.about,t.date,t.created_at
FROM treatment_parameters t
WHERE t.parameter_fk = 47
Image 2
So we need to show both rows, and also have the count(*) for missed_treatment=' ' effect both rows.
Below you can find the entire list from treatment_log.
treatment_log
So you need to self-join:
SELECT t.id,t.parameter_fk,t.course_name,t.room, t.protocol,t.navigation,t.area,
t.coil,t.number_of_treatments,t.motor_threshold,t.threshold_multiplier,t.target_threshold,
t.about,t.date,t.created_at, aa.t_count AS completed_treatments
FROM treatment_parameters t
left join (select treatment_fk, count(*) as t_count from treatment_log l
where l.missed_treatment=' ' group by treatment_fk) aa on aa.treatment_fk=t.id
WHERE t.parameter_fk = 47
Related
When I have such a table,
I want to get the result of a select statement like the picture below.
However, when count is 0, the row is omitted.
This makes me confused.
How can I modify this SQL statement to print with count = 0 rows?
SELECT ca.syear, COUNT(cc.joined),cc.joined
FROM schedules AS cc JOIN
schedule_assignment AS ca
ON ca.idx = cc.assignment
where student = 1 AND cc.joined = 1
GROUP BY ca.syear;
Sorry, my reputation is under 10 so I can't show the image right away. :(
I think you just want a LEFT JOIN:
SELECT sa.syear, COUNT(cc.joined)
FROM schedule_assignment sa LEFT JOIN
schedules s
ON ca.idx = cc.assignment AND
s.student = 1 AND
s..joined = 1
GROUP BY sa.syear;
I did not include joined in the result set It is always 1, so that does not seem useful. You can include it: 1 as joined, if you really want it.
When I type the following query
SELECT
COUNT(*) AS COUNT
FROM
OP_table OP
WHERE
OP.TARGET_ID= 4330000000000369;
I get a count of 55
When I try to use it in a join
SELECT
TS.TARGET_ID, T.TARGET_NAME, T.TARGET_PUBLIC_NAME, count( DISTINCT OP.OP_ID) AS OP_COUNT
FROM
TS_table TS
INNER jOIN
T_table T
ON
T.TARGET_ID = TS.TARGET_ID
OUTER JOIN
OP_TABLE OP
ON
OP.TARGET_ID = T.TARGET_ID
WHERE
TS.TARGET_SERVICE_ID = number
Then I get
TARGET_ID, TARGET_NAME, TARGET_PUBLIC_NAME, OP_COUNT
number, target name, Ebook Central History 33781
with an count of 33781. I want to use the 2nd functions structure but get the right count of 55
for some reason it's getting the count of the Target_id's instead of Target_service ID's
also I noticed if I type the following
SELECT
COUNT(*) AS COUNT
FROM
KB_OBJECT_PORTFOLIOS OP
WHERE
OP.TARGET_ID=4330000000000383;
where that number corresponds will produce the result 33781.
somewhere within the joining it's getting rid of the target ID of 4330000000000369 and using a different target ID 4330000000000383
Ok, I get mixed up from the stupid column names OP.target_ID = TS.target_service_id and not TS.target_ID...
My Platform is MySql
I have two queries that I need to combine, using the first query as a type of filter for the second query.
Query 1:
SELECT * FROM INVENTORY
WHERE INV_ID = 1
AND FSCL_YR = 2017
From this query we will get results back that includes a column named STR_NBR.
Which we then want to use in the second query as 'If the store number appears in the first query, give me the results where it shows in the second'. The second query tables use the column name SND_LOC_NBR instead of STR_NBR.
Query 2:
SELECT * FROM Transfer A
LEFT JOIN Transfer_Detail B
ON A.XFER_NBR = B.XFER_NBR
WHERE A.XFER_NBR = B.XFER_NBR
AND A.XFER_STAT_IND IN ('S','C')
AND (where the SND_LOC_NBR needs to match STR_NBRs found from Query 1)
Try this:
SELECT * FROM Transfer A
LEFT JOIN Transfer_Detail B
ON A.XFER_NBR = B.XFER_NBR
WHERE A.XFER_STAT_IND IN ('S','C')
AND SND_LOC_NBR IN
(SELECT STR_NBR FROM INVENTORY
WHERE INV_ID = 1 AND FSCL_YR = 2017 )
We have two tables in mysql database.Screenshots are attached below.
Given table ads_testTable
here is the screenshot of my dimesnionvalue_flattable
We have to run a query like the one below.
SELECT Quiz_Attempt.L1_Key dimID,
Quiz_Attempt.L1_Label CatVars,
COALESCE(**xyz**,0) AS series0
FROM DSQ_ADSSCHEMA.ADS_TestTable dataTable
RIGHT OUTER JOIN LS_CONFIG.DSQ_DIMENSIONVALUES_FLAT Quiz_Attempt on dataTable.Quiz_Attempt = Quiz_Attempt.L1_Key
WHERE Quiz_Attempt.L0_Key = 'All Levels' AND
Quiz_Attempt.DimensionID = 'Packet'
GROUP BY Quiz_Attempt.L1_Key, Quiz_Attempt.L1_Label;
My motive is to write a query in place of xyz so that I can get avg of obtainedMarks column in testtable according to the value of dimID I get.Each distinct Quiz_Attempt is a different test so If a Packet is repeating for a particular Quiz_Attempt in testTable, it should take only one value for that AttemptID.
I think you query could take the form of:
SELECT
L1_Key dimID,
L1_Label CatVars,
COALESCE('**xyz**',0) AS series0
FROM (
SELECT *
FROM (SELECT * FROM ADS_TestTable GROUP BY ADS_TestTable.Quiz_Attempt) dataTable
RIGHT OUTER JOIN DSQ_DIMENSIONVALUES_FLAT Quiz_Attempt on dataTable.Quiz_Attempt = Quiz_Attempt.L1_Key
WHERE Quiz_Attempt.L0_Key = 'All Levels' AND
Quiz_Attempt.DimensionID = 'Packet'
GROUP BY dataTable.Quiz_Attempt
) A GROUP BY dimID, CatVars;
The JOIN is done in an inner query, and grouped by Quiz_Attempt, so that you get a single row per attempt. This result is then used to compute what you need.
Please can someone talk me through a query that I have inherited through a website I am developing.
The query is returning a random group of 5 products based on the category number 56. There is an issue with the query because it is not restricting the selection based on the product on web and product archive conditions.
AND p.product_OnWeb = 1
AND p.product_Archive = 0
The above lines in the query aren't being adhered to. Instead the query is including all products even when they are marked as p.product_Archive=1 (Archived) and p.product_OnWeb = 0 (Not Online)
If someone could point out where I need to make a change I'd be grateful.
The query in full is:-
SELECT c.prdt_cat_rel_Product_ID,
ROUND(RAND() * x.m_id) 'rand_ind'
FROM tbl_prdtcat_rel c,
tbl_products p,
(SELECT MAX(t.prdt_cat_rel_Cat_ID) 'm_id'
FROM tbl_prdtcat_rel t) x
WHERE c.prdt_cat_rel_Cat_ID = 56
AND p.product_OnWeb = 1
AND p.product_Archive = 0
ORDER BY rand_ind
LIMIT 3
Thankyou
First, to make it easier, convert the query to the newer join syntax
SELECT c.prdt_cat_rel_Product_ID, ROUND(RAND() * x.m_id) 'rand_ind'
FROM tbl_prdtcat_rel c,
JOIN tbl_products p ON p.??? = c.???
JOIN (
SELECT MAX(t.prdt_cat_rel_Cat_ID) 'm_id' FROM tbl_prdtcat_rel t
) x ON 1=1
WHERE c.prdt_cat_rel_Cat_ID = 56
AND p.product_OnWeb = 1
AND p.product_Archive = 0
ORDER BY rand_ind
LIMIT 3
You can see that the query doesn't know how to select match records from P based on C, so it grabs them all.. You need to specify how to match tbl_prdtcat records with tbl_product records (replace the ??? in the above with the appropriate fields)
I am guessing each product in p has some sort of field indicating which category it belong to, use this field to match and your query should work...