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...
Related
What I have tried:
SELECT requestformtbl.employee_name, requestformtbl.request_type, requestformtbl.total_day,
requestformtbl.request_status, requestformtbl.admin_remark, requestformtbl.confirmed_by, requestformtbl.date_confirmed, requesttbl.max_allotment,
(requesttbl.max_allotment - sum(requestformtbl.total_day)) as Available from requestformtbl inner join requesttbl on
requestformtbl.request_type = requesttbl.request_type;
error: Column 'requestformtbl.employee_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
If requestttbl.request_type="Vacation Leave" has requesttbl.max_allotment=20,
when a new entry in requestformtbl is inserted with requestformtbl.request_type="Vacation Leave" and requestformtbl.total_day=5
I want to get the remaining available leave
You can get the results you want without using a GROUP BY by using a correlated subquery to get the sum of the employees used days:
SELECT rft.employee_name,
rft.request_type,
rft.total_day,
rft.request_status,
rft.admin_remark,
rft.confirmed_by,
rft.date_confirmed,
rt.max_allotment,
rt.max_allotment - (select sum(total_day)
from requestformtbl rft2
where rft2.employee_name = rft.employee_name) as Available
from requestformtbl rft
inner join requesttbl rt on rft.request_type = rt.request_type;
Note I've used table aliases to make the query more readable.
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.
I have these 2 queries:
$sql = "SELECT *
FROM ultrait_wpl_properties
LEFT JOIN ultrait_wpl_property_types
ON ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
ORDER BY ultrait_wpl_properties.id ";
$sql2 = "SELECT *
FROM ultrait_wpl_properties, ultrait_wpl_property_types
WHERE ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
ORDER BY ultrait_wpl_properties.id";
For some odd reason when the IDs are output some are duplicated? By my reseaning these queries should get everything from the table in the first part and join the second table based on the WHERE condition.
<property><id>13</id></property>
<property><id>6</id></property>
<property><id>6</id></property>
<property><id>6</id></property>
<property><id>6</id></property>
<property><id>7</id></property>
This may be slightly unclear but for some reason I'm getting duplicate IDs, all i want really is to be able to access the property type which links to the ID in the second table.
I have tested both queries in phpMyAdmin and they yeild the desired result, however when I use the queries in my php script they return unexpected results.
You are getting one row for each row in table ultrait_wpl_properties. What else do you expect? If it is just one record per type, then you would have to re-write your query accordingly. You select * from both tables. But is it only the type ID you need? Then why join the tables at all?
Get all type IDs:
select id from ultrait_wpl_property_types;
Get all type IDs in table ultrait_wpl_properties:
select distinct property_type from ultrait_wpl_properties;
Get all type data for types in ultrait_wpl_properties:
select * from ultrait_wpl_property_types
where id in (select property_type from ultrait_wpl_properties);
You are getting a Cartesian result in the case the ultrait_wpl_property_types table has multiple records for a single property. Such as a property type could be Type A, Type B, Type C which might be descriptive "types". So a single property would be accounted for each entry.
You might just need to do SELECT DISTINCT, or GROUP BY ultrait_wpl_properties.id to make sure only one record per ID, but with generic "Select * ", I would first try with GROUP BY.
The result of this SQL query should return a set of 6 rows.
SELECT baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted
FROM feedFinishes
LEFT OUTER JOIN baseFeeds
ON feedFinishes.GUID = baseFeeds.GUID
WHERE feedFinishes.nowDate = baseFeeds.nowDate
AND baseFeeds.siteURL LIKE '%www.example.com%'
Currently it returns 18 rows, the correct 6 rows are being repeated 3 times. I figure a solution to this is to have another WHERE clause something similar to:
WHERE DISTINCT feedFinishes.ID
After researching this I have tried:
SELECT baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted
FROM feedFinishes
JOIN baseFeeds
ON feedFinishes.GUID = baseFeeds.GUID
WHERE baseFeeds.siteURL LIKE '%www.example.com%' AND feedFinishes.ID in
(SELECT ID FROM feedFinishes GROUP BY ID HAVING COUNT(ID)=1);
After the discussion found here
Alas, this still returns 18 rows. I believe the answer is similar to here
Any advice would be appreciated.
You should apply group by clause.
SELECT
baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted
FROM feedFinishes
JOIN baseFeeds
ON feedFinishes.GUID = baseFeeds.GUID
WHERE baseFeeds.siteURL LIKE '%www.example.com%'
GROUP BY
baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted
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.