SQL Statement Issue - mysql

The following SQL code comes back with everything selected being NULL and the event_size being 0. My tables are set up correctly for the following criteria. What am I doing wrong here? Thank you for your help
SELECT table_one.event_id AS event_id, table_one.event_name AS event_name, table_one.event_address AS event_address, COUNT( table_two.user_id ) AS event_size
FROM table_one
JOIN table_two
ON table_one.event_id = table_two.event_id
WHERE (
table_one.event_start_date = '5/10/2012'
OR table_one.event_mid_date = '5/10/2012'
OR table_one.event_end_date = '5/10/2012'
)
ORDER BY event_size DESC

Try adding a group by clause:
group by table_one.event_id, table_one.event_name, table_one.event_address
This should do what you want.

Related

Getting Newest Record from a table using mysql

This has to be a no brainer, but I am stumped. I'm used to using aggregate 'FIRST' in MsAccess, but MySql has no such thing.
Here is a simple table. I want to return the most recent record based on the date,
for each unique 'group ID'. I need the three records in yellow.
I was asked to add my full query. I tried one of the suggestions using the JOIN feature replacing 't' with the temp table name, but it failed to work. "Can't reopen table 't'"
The code is below. I know it's ugly, but it does return the correct data set.
I cleaned up the code a bit and added the JOIN code. Error: "Can't reopen table 't'"
enter code here
DROP TABLE IF EXISTS `tmpMaxLookupResults`;
create temporary table tmpMaxLookupResults
as
SELECT
REPORTS.dtmReportCompleted,
RESULTS.lngMainReport_ID, RESULTS.lngLocationGroupSub_ID
FROM
(tbl_010_040_ProcedureVsTest_Sub as ProcVsSub
INNER JOIN tbl_010_050_CheckLog_RESULTS as RESULTS
ON (ProcVsSub.lngLocationGroupSub_ID = RESULTS.lngLocationGroupSub_ID)
AND (ProcVsSub.lngProcedure_ID = RESULTS.lngProcedure_ID)
AND (ProcVsSub.lngItemizedTestList_ID = RESULTS.lngItemizedTestList_ID)
AND (ProcVsSub.strPasscodeAdmin = RESULTS.strPasscodeAdmin)
AND (ProcVsSub.strCFICode = RESULTS.strCFICode))
INNER JOIN
tbl_000_010_MAIN_REPORT_INFO as REPORTS ON (RESULTS.lngPCC_ID =
REPORTS.lngPCC_ID)
AND (RESULTS.lngProcedure_ID = REPORTS.lngProcedure_ID)
AND (RESULTS.lngMainReport_ID = REPORTS.idMainReport_ID)
AND (RESULTS.strPasscodeAdmin = REPORTS.strPasscodeAdmin)
AND (RESULTS.strCFICode = REPORTS.strCFICode)
WHERE
(((RESULTS.lngProcedure_ID) = 143)
AND ((RESULTS.dtmExpireDate) IS NOT NULL)
AND ((RESULTS.strCFICode) = 'ems'))
GROUP BY RESULTS.lngMainReport_ID, RESULTS.lngLocationGroupSub_ID
ORDER BY (REPORTS.dtmReportCompleted) DESC;
SELECT t.*
FROM tmpMaxLookupResults AS t
JOIN (
SELECT lngLocationGroupSub_ID,
MAX(dtmReportCompleted) AS max_date_completed
FROM tmpMaxLookupResults
GROUP BY lngLocationGroupSub_ID ) AS dt
ON dt.lngLocationGroupSub_ID = t.lngLocationGroupSub_ID AND
dt.max_date_completed = t.dtmReportCompleted
enter code here
Try this
SELECT
tn.*
FROM
tableName tn
RIGHT OUTER JOIN
(
SELECT
groupId, MAX(date_completed) as max_date_completed
FROM
tableName
GROUP BY
groupId
) AS gt
ON
(gt.max_date_completed = nt.date_completed AND gt.groupId = nt.groupId)
You can use the following SQL.
select * from table1 order by date_completed desc Limit 1;
Use Order By
SELECT *
FROM table_name
ORDER BY your_date_column_name
DESC
LIMIT 1
In a Derived Table, get the maximum date_completed value for every group_id.
Join this result-set back to the main table, in order to get the complete row corresponding to maximum date_completed value for every group_id
Try the following query:
SELECT t.*
FROM your_table_name AS t
JOIN (
SELECT group_id,
MAX(date_completed) AS max_date_completed
FROM your_table_name
GROUP BY group_id
) AS dt
ON dt.group_id = t.group_id AND
dt.max_date_completed = t.date_completed

Selection of the maximum deleted_data from duplicate rows

Table structure:
It is not possible to select records with the maximum delete date, which are grouped by one code, with the condition that they are all deleted. If the records have the same code, but have different delete statuses, then you do not need to select these records.
In this example, you select records with id = 3, id = 4.
SELECT * FROM analyzes_test WHERE code IN (SELECT code FROM analyzes_test GROUP BY code HAVING count(code)>1) AND deleted = (max deleted_date)
But I do not know how to substitute the longest possible date for deletion.
Please tell me who has more experience with sql.
Try the following simple query-:
select code,max(deleted_date) MAX_DeletedDate
from analyzes_test
group by code
having count(deleted_date)>1
you could use an inner join on max date group by code
select *
FROM analyzes_test a
inner join (
select code, max(deleted_date) max_date
FROM analyzes_test
group by code
) t on t.max_date = a.deleted_date and t.code = a.code
or if you don't want thw result for code with only a rows a could use
select *
FROM analyzes_test a
inner join (
select code, max(deleted_date) max_date
FROM analyzes_test
group by code
having count(*)>1
) t on t.max_date = a.deleted_date and t.code = a.code
TRY THIS: GROUP BY and HAVING will help to retrieve the max date of each code and use it as a subquery to retrieve all the information for the table as below:
SELECT ant.*
FROM analyzes_test ant
INNER JOIN (SELECT code, MAX(deleted_date) max_date
FROM analyzes_test
WHERE deleted_date IS NOT NULL
GROUP BY code
HAVING COUNT(deleted_date) > 1) t ON t.code = ant.code
AND t.max_date = ant.deleted_date

Getting 1 Value to 1 column

Good day.
I need to output look like this, is it possible this output.
I have my script:
myoutput is this
SELECT q_type,q_title,qcategory_question
FROM `survey_question` as sq LEFT JOIN
(SELECT id,qid,qcategory_question
FROM survey_category_question
) scq ON sq.id = scq.qid
Thanks in advance.
Your query is fine. You should handle the formatting in the application. Now, I am going to make a different suggestion, which is to bring all the categories on a single row using group by:
SELECT sq.q_type, sq.q_title, GROUP_CONCAT(scq.qcategory_question)
FROM survey_question sq LEFT JOIN
survey_category_question scq
ON sq.id = scq.qid
GROUP BY sq.q_type, sq.q_title;
Notes:
Qualify all column names in the query, especially when you have more than one table reference.
The subquery is unnecessary and probably detrimental in MySQL (most databases would ignore it, but MySQL may materialize it).
The resulting categories will be in a comma-delimited list for output.
Although i will not suggest you to do this in query as you could do that in php easily, you can try this..
SET #row_number = 0;
SELECT q_type, CASE
WHEN A.mxnum = AA.num
THEN q_title
ELSE ' '
END AS q_title, qcategory_question
FROM (
SELECT q_type, q_title, qcategory_question, (#row_number: = #row_number + 1)
AS num FROM `survey_question` AS sq
LEFT JOIN (
SELECT id, qid, qcategory_question
FROM survey_category_question
) AA
LEFT JOIN (
SELECT q_title, max(num) AS mxnum
FROM (
SELECT q_type, q_title, qcategory_question, (#row_number: = #row_number + 1) AS num
FROM `survey_question` AS sq
LEFT JOIN (
SELECT id, qid, qcategory_question
FROM survey_category_question
) scq
ON sq.id = scq.qid
)
GROUP BY q_title
) A
ON num = mxnum
);
hope this helps...

LEFT JOIN SUM with WHERE clause

The following query always outputs SUM for all rows instead of per userid. Not sure where else to look. Please help.
SELECT * FROM assignments
LEFT JOIN (
SELECT SUM(timeworked) AS totaltimeworked
FROM time_entries
) assignments ON (userid = assignments.userid AND ticketid = ?)
WHERE ticketid = ?
ORDER BY assigned,scheduled
If you want to keep the SELECT *, you would have to add a group by clause in the subquery. Something like this
SELECT * FROM assignments
LEFT JOIN (
SELECT SUM(timeworked) AS totaltimeworked
FROM time_entries
GROUP BY userid
) time_entriesSummed ON time_entriesSummed.userid = assignments.userid
WHERE ticketid = ?
ORDER BY assigned,scheduled
But a better way would be to change the SELECT * to instead select the fields you want a add a group by clause directly. Something like this
SELECT
assignments.id,
assignments.assigned,
assignments.scheduled,
SUM(time_entries.timeworked) AS totalTimeworked
FROM assignments
LEFT JOIN time_entries
ON time_entries.userid = assignments.userid
GROUP BY assignments.id, assignments.assigned, assignments.scheduled
Edit 1
Included table names in query 2 as mentioned in chameera's comment below

Mysql: WHERE not working in subquery?

I'm trying to get the WHERE part of my subquery to work below. I can see that 'where event_id=..' is ambiguous because the parent query is looking at the same table.
Is it even possible to have a WHERE in a same-table subquery?
UPDATE tickets SET tickets.ticket_number = (
SELECT max_ticket
FROM (
SELECT (MAX(ticket_number)+1) AS max_ticket
FROM tickets
WHERE event_id=10045
)
AS sub_table
)
WHERE ticket_id=68
Any help really appreciated.
Possibly try it as a join
UPDATE tickets a
INNER JOIN
(
SELECT (MAX(ticket_number)+1) AS max_ticket
FROM tickets
WHERE event_id = 10045
) b
SET a.ticket_number = b.max_ticket
WHERE a.ticket_id = 68