Select only distinct values for a particular column mysql - mysql

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.

Related

How do I add a criteria to a query on one table but keep all the records from another?

I have the following query:
SELECT games_atp.ID1_G, odds_atp.K1
FROM games_atp LEFT JOIN odds_atp ON (games_atp.ID1_G = odds_atp.ID1_O) AND (games_atp.ID2_G = odds_atp.ID2_O) AND (games_atp.ID_T_G = odds_atp.ID_T_O) AND (games_atp.ID_R_G = odds_atp.ID_R_O)
I know the joining is convoluted but the original db is built without a primary key. The above works fine and importantly pulls all the records from games_atp. I now want to add a criteria into this to pull only certain K1 records from odds_atp. I added a WHERE clause as follows:
SELECT games_atp.ID1_G, odds_atp.K1
FROM games_atp LEFT JOIN odds_atp ON (games_atp.ID1_G = odds_atp.ID1_O) AND (games_atp.ID2_G = odds_atp.ID2_O) AND (games_atp.ID_T_G = odds_atp.ID_T_O) AND (games_atp.ID_R_G = odds_atp.ID_R_O)
WHERE (((odds_atp.ID_B_O)=2));
However, this overides the left join and only pulls records from games_atp where there is a corresponding record in odds_atp with ID_B_O = 2. How do I keep the criteria and all the records in games_atp? Thanks in advance.
Your current where condition will filter your final result, hence you are only seeing id_B_O = 2.
However, you could also add the wehre condition directly into your left join.
something like this.
SELECT
games_atp.ID1_G, odds_atp.K1
FROM
games_atp
LEFT JOIN odds_atp ON
(
(odds_atp.ID_B_O =2)
AND
(
(games_atp.ID1_G = odds_atp.ID1_O)
AND (games_atp.ID2_G = odds_atp.ID2_O)
AND (games_atp.ID_T_G = odds_atp.ID_T_O)
AND (games_atp.ID_R_G = odds_atp.ID_R_O)
)
);
or you could also take advantage of sub-queries

Create array that has data from two tables

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

count records is not giving the right number

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...

Using results of one query into the next with unlike column names

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 )

Counting the number min records within groups

I'm building a report for a database where I need to determine the number of "first scans" grouping by company, job, and date.
The scan table can contain multiple scans for the same item, however I only want to include the original scan in my COUNT, which can only be identified as being the scan with the earliest date that matches a particular item.
My first attempt at this was:
SELECT
_item_detail.job_id,
_item_group.group_id,
_scan.company_id,
DATE(scan_date_time) as scan_date,
COUNT(1)
FROM _scan
INNER JOIN _item_detail ON _item_detail.company_id = _scan.company_id
AND
_item_detail.serial_number = _scan.serial_number
INNER JOIN _item_group ON _item_group.group_id = _item_detail.group_id
WHERE _item_detail.job_id = '0326FCM' AND _scan.company_id = '152345' AND _item_group.group_id = 13
GROUP BY
_item_detail.job_id,
_item_group.group_id,
_scan.company_id, scan_date -- first_scan_count
HAVING min(scan_date_time);
This is giving me incorrect results, though (about 3x too many). I am assuming it's because the MIN record is being recalculated for each date, so if the min was found on day 1, it may also be found on day 3 and counted again.
How can I modify my query to achieve the desired results?
Something similar to this should work... I'm not completely sure of how your tables are laid out or how the data relates them together, but this is the general idea:
SELECT
_item_detail.job_id,
_item_group.group_id,
_scan.company_id,
DATE(scan_date_time) as scan_date,
COUNT(1)
FROM
_scan s1
INNER JOIN _item_detail
ON _item_detail.company_id = s1.company_id
AND _item_detail.serial_number = s1.serial_number
AND _item_detail.job_id = '0326FCM'
INNER JOIN _item_group
ON _item_group.group_id = _item_detail.group_id
AND _item_group.group_id = 13
WHERE
s1.company_id = '152345'
AND s1.scan_date_time = (
SELECT MIN(s2.scan_date_time)
FROM _scan s2
WHERE
s2.company_id = s1.company_id
AND s2.serial_number = s1.serial_number
)
GROUP BY
_item_detail.job_id,
_item_group.group_id,
s1.company_id
I don't quite follow your query, but based on the description of the problem, I'd say create a subquery that gives the min scan date for for each item, group by items, the perform your outer select on that.