excuse me, I'm still confused by the use of sub queries to be implemented in the following table.
I have 2 tables like the following with relation table id_activity
Table 1 for main table
Table 2 for detail table 1
the results I want are as follows
Note*
column date_yesterday, yesterday, interaksi and status are searched from the column today but based on the previous day.
column date_today and today are searched by today
the inprogress and issue columns are taken from the column that contains the value in that column, if the column has no value then it is not included.
I have tried with queries like this, but not according to what I want
SELECT detail_activity.id as id, header_activity.tanggal, detail_activity.today, detail_activity.issue, detail_activity.inprogress, detail_activity.interaksi, detail_activity.status (SELECT tanggal from header_activity left join detail_activity on detail_activity.id_activity = header_activity.id_activity WHERE header_activity.tanggal = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 DAY),"%Y-%m-%d") AND header_activity.id_karyawan = '23') as tanggal_kemarin from header_activity left join detail_activity on detail_activity.id_activity = header_activity.id_activity WHERE header_activity.tanggal = CURDATE() AND header_activity.id_karyawan = '23'
maybe someone can help me to resolve this case
Thanks
Related
I have MySQL table with employees attendance. first row of a day of employee treating as in time and last row of a day of employee treating as out time. I am trying to select first and last (min time and max time) from attendance table. It should give me two row sets. but my query not giving me as i expecting the result.
Table (Attendance)
My Query
select *, min(attdate) as intime, max(attdate) as outtime from attendance where empid=1
But above query not giving me as expected result. My output should be in below image. Please suggest me the query or give me hint to achieve given output.
this can be done by sub queries in where conditions.
SELECT * FROM attendance AS c WHERE empid=1 and (
attdate=( select min(attdate) from attendance where attendance.empid=c.empid )
or attdate=( select max(attdate) from attendance where attendance.empid=c.empid )
);
Unfortunately, MySQL doesn't offer window functions, so it's a bit more difficult here. You can use exists :
Select * from yourtable t
Where not exists (select 1 from yourtable s
Where t.empid = s.empid and
(s.attndate < t.attndate or s.attndate > t.attndate))
Though it seems you need to add another condition t.date = s.date unless you have only 1 day records stored there
I have a mysql table which collects daily sales data (date, sale, gst etc). I need a select statement to compare daily sales for any given date with same date last year and year before, to see the how much we made on that day in previous years.
To give an idea of how the output table will look, I have an example table in jsfiddle.
And here is the sql table data in sqlfiddle.
I tried this statement:
SELECT * FROM sales_chc WHERE MONTH(sale_date) = '1'
It of course gives me the filtered results that I want but I couldn't figure out how to display the results in the give table example.
By changing year and month in the WHERE statement, you can get a report for whatever month/year combination you need.
SELECT sn.sale_date AS SaleY0 , sn.EFTPOS AS EFTPOSY0, sn.total AS totalY0,
s1.sale_date AS SaleY1 , s1.EFTPOS AS EFTPOSY1, s1.total AS totalY1,
s2.sale_date AS SaleY2 , s2.EFTPOS AS EFTPOSY2, s2.total AS totalY2
FROM sales_chc sn
LEFT JOIN sales_chc S1
ON s1.sale_date=(sn.sale_date -INTERVAL 1 YEAR)
LEFT JOIN sales_chc S2
ON s2.sale_date=(sn.sale_date -INTERVAL 2 YEAR)
WHERE YEAR(SN.sale_date)=2016 AND MONTH(SN.sale_date)=1
SQL fiddle
I need to retrieve values that doesn't exist in another table. I'm working with Shares and Share_types tables.
Currently, I have this working with PHP but I'm always looping all over the 2,500~ rows of Share_types and I think it is possible to solve the amount of rows with a query.
The user currently goes through this process:
Select the type of share - Anual share
Select the year that will be extracted - 2016
Code will generate all shares until the year of 2016 that weren't yet generated. This means, that the years behind of 2016 will also be generate if they don't exist.
That said, my PHP code is like the following:
// Retrieves always the 2,500~ rows
$listOfShareTypes = "SELECT user_id, date_start FROM share_types WHERE type = 'Anual share'";
foreach($listOfShareTypes as $type)
{
// Now is where I validate if the share already exists
$hasShare = "SELECT COUNT(*) FROM shares WHERE the_year = $yearSelectedByUser, user_id = $type->user_id, share_type_id = $type->id";
if($hasShare == TRUE)
continue;
else
// Create the share..
}
So usually, to retrieve via query the results that doesn't exist in another table I would do two select in a query, but after a few searches it points to the use of LEFT JOIN. However I have no clue how to accomplish this since I need to match several fields (user_id, share_type_id, year, etc.)
Looking at this example I created on SQLFiddle, the result should be:
(5, 3, 'Anual', '2015-06-28')
And with this result and since the user selected the year of 2016 I should loop (in PHP) from 2015 until 2016.
You were using the wrong column in the join condition. The tables should be joined on user_id.
SQL Fiddle
SELECT stype.id, stype.user_id, stype.type, stype.date_start
FROM share_types AS stype
LEFT JOIN shares AS share ON share.user_id = stype.user_id
WHERE share.share_type_id IS NULL
try this query
SELECT user_id, date_start FROM share_types st,shares sh on sh.share_type_id=st.user_id WHERE type = 'Anual share' and sh.user_id=st.id and sh.the_year=$yearSelectedByUser and Having COUNT(sh.user_id) < 1;
I am trying to figure out how to do this SQL query, but I'm at a loss. I think I am getting in over my head.
I have three tables:
1. datetable, which contains lists of month/year up to year 2020. column is 'thedate'
2. A table containing a list of lectures. A date() field is one column, named "month"
3. A log table, which contains individual rows of data on a month to month basis. i.e Each user has their own row of data for each month of the year.
I would like to take table #2, find if there is a lecture assigned for month x, then take table #3, find the row which corresponds to month x and particular user, then see if column 'hopkins' (a part of table 3) is null or not. I hope this makes a little bit of sense. I've figured out how to use the datetable to find missing rows in table #2, but I haven't figured out how to do this above.
Thanks!
I think you just want a left join from eval to hopkins and then a test on the field:
select e.uid,
max(case when h.name is null then 0 else 1 end) as isInHopkins
from eval e left outer join
hopkins h
on year(e.month) = year(h.month) and
month(e.month) = month(h.month)
group by e.uid;
If the hopkins.id is supposed to related to the user, then you would want to add and e.uid = h.id to the on clause.
If this doesn't give you what you want, then please edit your question to provide sample results for the SQL Fiddle data.
I have a table which contains related records (multiple revisions of the same record). Each record has a string field that resembles a date (date, time, and microtime). I want to select all records that are older than a specific date. If a record has a related record newer than the specific date, I do not want to select any of those related records. Any ideas for that select statement? Eventually, it will be a REMOVE statement.
Edit: Some Sample Rows
id shared_id date type other_data...
1 2 2010-01-01 01:02:03.1234567 original ...
2 3 2010-01-15 11:12:03.1234733 original ...
3 2 2010-02-01 03:04:04.5465654 amendment ...
If my cut-off date was "2010-01-31", I would want to select id #2 only because id #1 has an amendment newer than the cut-off date.
I found this link helping me generate the select statement.
SELECT DISTINCT T.shared_id,T.date,T.id
FROM table T WHERE T.date = (
SELECT MAX( date ) FROM table WHERE shared_id = T.shared_id )
AND T.date < 'my_cut_off_date_string'
This seems to work for me. Thanks for everyone's help.
Maybe you can try the DATEDIFF() function, check this out:
Link 1
Or this one: Link 2
Or maybe you can try the classic query (SELECT FROM table WHERE data <= anotherdata), but in this case you need to convert both data in timestamp format
DELETE from tablename where relatedField = 'relatedValue' AND dateField <= dateToDeleteFrom
Something along those lines should do what you need it to do, if I understand your scenario. If you provide a sample data set I can adjust the statement to more accurately represent your need, but I think this is a good starting point.
HTH,
Jc