I have to join three tables.
1 table is transaction , next table is shops, 3rd is weather.
I want to fetch data from all of these three ,but in case of weather table > id,tamp_c,datetime column name .
The query is:-
select HOUR(transaction.time) as Hour ,
TRUNCATE(sum(transaction.total),2) as Total_Sales,
shops.gstIncludedSales as GST_Inc_Sales,
shops.gstFreeSales as GST_Free,weather.temp_c
from transaction,shops,weather
where transaction.shopid=shops.id and transaction.shopid=7
and transaction.transaction_date ='2015-05-25'
group by hour
ORDER BY hour DESC
The problem is that I want to apply a where clause with weather.datetime table separation for particular date like time(datetime)='2015-05-25', but it's not working.
Try this:
WHERE transaction.transaction_date >= '2015-05-25' AND transaction.transaction_date < '2015-05-26'
That will return rows with timestamp during the whole day (midnight until midnight - time is implied).
If you have SQLServer 2014 you canm use DATE(transaction.transaction_date).
Does this do what you want
SELECT HOUR(transaction.time) as Hour ,
TRUNCATE(sum(transaction.total),2) as Total_Sales,
shops.gstIncludedSales as GST_Inc_Sales,
shops.gstFreeSales as GST_Free,weather.temp_c
FROM transaction
JOIN shops ON transaction.shopid=shops.id
JOIN weather ON DATE(transaction.transaction_date) = DATE(weather.datetime)
WHERE DATE(transaction.transaction_date)='2015-05-25' AND transaction.shopid=7
GROUP by hour
ORDER BY hour DESC
(Assuming you are using MySQL)
Related
in the above table i need sql query by which i can return dates between Leave_from and Leave_to from leave_log Table (eg, Leave_from 30/07/2018 Leave_to 02-08-2018) i need dates like
30/07/2018
01/08/2018
02/08/2018
by which i will try next query
2) is der any query or function which will check before insert into leave_log table whether the leaves applied by the employees for particular date (01-08-2018) does not cross quota of 10% per Team
Thanks in advance
First, you should have a calender table if yes, then you need to JOIN with your actual table to calender table. In other way you can use recursive CTE option :
with t as (
select id, leave_frm, leave_to
from table
union all
select id, dateadd(day, 1, leave_frm), leave_to
from t
where leave_frm < leave_to
)
select *
from t
option (maxrecursion 0);
I have a database that has a row entry for every minute (86,000 values per day) and I am trying to write PHP to select only one row per day. I have a column "timestamp" that has the current timestamp in regular format (2017-12-09 06:49:02).
Does anyone know how to write a select statement to do what I am trying to do?
Example output:
2017-12-09 06:49:02, datavalue
2017-12-10 06:49:02, datavalue
2017-12-11 06:49:02, datavalue
2017-12-12 06:49:02, datavalue
Here is one method:
select t.*
from t join
(select min(t2.timestamp) as min_timestamp
from t t2
group by date(t2.timestamp)
) t2
on t.timestamp = t2.min_timestamp;
I’m designing a program for my school to keep student attendance records. So far I have the following query working fine and now I would like to add an IF statement to perform a percentage operation when a certain condition is given. As it is, the query is using INNER JOIN to search for data from two different tables (oxadmain and stuattend) and it’s displaying the results well on a results table:
SELECT o.name
, o.year
, o.photoID
, o.thumbs
, s.ID
, s.studid
, s.date
, s.teacher
, s.subject
, s.attendance
FROM stuattend s
JOIN oxadmain o
ON s.studid = o.stuid
ORDER
BY name ASC
Now I would like to add an “if” statement that
1) finds when stuattend.attendance is = Absent, calculates the percentage of absences the students may have in any given period of time, and then stores that (%) value in “percentage” and
2) ELSE assigns the value of 100% to “Percentage”.
So far I’ve been trying with the following:
<?php $_GET['studentID'] = $_row_RepeatedRS['WADAstuattend']; ?>
SELECT oxadmain.name , oxadmain.year , oxadmain.photoID , oxadmain.thumbs , stuattend.ID , stuattend.studid , stuattend.date , stuattend.teacher, stuattend.subject , stuattend.attendance
CASE
WHEN stuattend.attendance = Absent THEN SELECT Count (studentID) AS ClassDays, (SELECT Count(*) FROM stuattend WHERE studentID = stuattend.studid AND Absent = 1) AS ClassAbsent, ROUND ((ClassAbsent/ClassDays)*100, 2) AS Percentage
ELSE
Percentage = 100
END
FROM stuattend INNER JOIN oxadmain ON stuattend.studid=oxadmain.stuid
ORDER BY name ASC
Any suggestions on how to do this well?
Thank you for your attention
The base idea would be:
select stuattend.studid, sum(stuattend.attendance = `absent`) / count(*)
from stuattend
group by stuaddend.studid;
This very much depends on exactly one entry per student and per day, and of course gets 0 if no absence and 1 if always absent.
To make this a bit more stable I would suggest to write a calendar day table, which simply keeps a list of all days and a column if this is a school day, so workday=1 means they should have been there and workday=0 means sunday or holiday. Then you could left join from this table to the presence and absence days, and even would give good results when presence is not contained in your table.
Just ask if you decide which way to go.
I am trying to calculate the difference of values list coming from a database.
I would like to achieve it using php or mysql, but I do not know how to proceed.
I have a table named player_scores. One of its rows contains the goals scored.
Ex.
pl_date pl_scores
03/11/2014 18
02/11/2014 15
01/11/2014 10
I would like to echo the difference between the goals scored during the matches played in different dates.
Ex:
pl_date pl_scores diff
03/11/2014 18 +3
02/11/2014 15 +5
01/11/2014 10 no diff
How can I obtain the desired result?
You seem to want to compare a score against the score on a previous row.
Possibly simplest if done using a a sub query that gets the max pl_date that is less than the pl_date for the current row, then joining the results of that sub query back against the player_scores table to get the details for each date:-
SELECT ps1.pl_date, ps1.pl_scores, IF(ps2.pl_date IS NULL OR ps1.pl_scores = ps1.pl_scores, 'no diff', ps1.pl_scores - ps1.pl_scores) AS diff
FROM
(
SELECT ps1.pl_date, MAX(ps2.pl_date) prev_date
FROM player_scores ps1
LEFT OUTER JOIN player_scores ps2
ON ps1.pl_date > ps2.pl_date
GROUP BY ps1.pl_date
) sub0
INNER JOIN player_scores ps1
ON sub0.pl_date = ps1.pl_date
LEFT OUTER JOIN player_scores ps2
ON sub0.prev_date = ps2.pl_date
There are potentially other ways to do this (for example, using variables to work through the results of an ordered sub query, comparing each row with the value stored in the variable for the previous row)
SELECT score FROM TABLE WHERE DATE = TheDateYouWant
$score = $data['score'];
SELECT score FROM TABLE WHERE date = dateYouWant
$difference = $score - $data['score'];
Something like this?
You could use two queries, one to get the value to use in the comparison (in the example below is the smaller number of scores) and the second one to get the records with a dedicated column with the difference:
SELECT MIN(pl_scores);
SELECT pl_date, pl_scores, (pl_scores - minScore) as diff FROM player_scores;
Or, using a transaction (one query execution php side):
START TRANSACTION;
SELECT MIN(Importo) FROM Transazione INTO #min;
SELECT Importo, (Importo - #min) as diff FROM Transazione;
select *,
coalesce(
(SELECT concat(IF(t1.pl_scores>t2.pl_scores,'+',''),(t1.pl_scores-t2.pl_scores))
FROM tableX t2 WHERE t2.pl_date<t1.pl_date ORDER BY t2.pl_date DESC LIMIT 1)
, 'no data' ) as diff
FROM tableX t1
WHERE 1
order by t1.pl_date DESC
This is my problem.
table ns_leagues:
id name
1 League 1
2 League 2
3 League 3
table ns_upcoming:
upID league date
1 1 1410390000
2 2 1411990200
3 3 1412010000
I have this 2 columns and I want to select the leagues from table ns_leagues and order by the row from the ns_upcoming with the date more close to the current time.
I tried several ways but nothings is working so far.
FAIL:
SELECT id
FROM ns_leagues
WHERE id in (
SELECT league
FROM ns_upcoming
WHERE date<='".$now."'
ORDER BY date ASC
SELECT DISTINCT id
from ns_leagues a
LEFT JOIN ns_upcoming v ON a.id = v.league AND v.date<= '".$now."'
ORDER BY v.date ASC
CURRENT:
SELECT * FROM ".PREFIX."leagues ORDER BY id DESC
This is what I have right now (on the right side: PROXIMOS PARTIDOS): http://www.nuno-silva.pt/jobs/mark/index.php
"En vivo" means that date >= time(); and it's not finished.
The times are countdowns for the match.
I need to order the leagues by date (selecting for that the row with the closest date to time(); from column date of table ns_upcoming of the selected league)
Could you give me a help on this one please?
You'd need to sort by the time DIFFERENCE between "now" and the match's time, e.g.
SELECT ...
...
ORDER BY DATEDIFF(ns_upcoming.date, curdate())