MYSQL query and compare values if multiple rows found - mysql

Im not too mysql savvy and i'm having issues creating a select that fits with my needs.
I have a database table that looks similar to this:
registrar
id balance date
---------------------
1 500.00 2013-01-01
2 402.00 2013-01-01
3 396.00 2013-01-02
4 394.00 2013-01-02
I have a query that I use to pull the data into a script and display the data:
SELECT balance, date FROM $registrar WHERE date BETWEEN '$starting_date' AND '$ending_date'
However, it appears that when querying, mysql is only returning the newest entry for that date. Id like if possible to return the lower balance amount of that date if found multiple rows matching the date criteria.

try ORDER BY
SELECT balance, date FROM $registrar WHERE date BETWEEN '$starting_date' AND '$ending_date' ORDER BY balance DESC

You have to use order by class in the end of query
SELECT balance, date FROM $registrar WHERE date BETWEEN '$starting_date' AND '$ending_date' ORDER BY balance DESC
By default it id , so u have to put balance .

Related

How can i find minimum count in a given date range

I'm trying to allocate workers for a job for a specific date range and wanted to find out the minimum number of workers allocated for the given date range.
For example, my table contains
startDate endDate No.of.Workers
--------- --------- ---------------
1-1-2019 10-1-2019 1
11-1-2019 20-1-2019 1
now, i wanted to find out the minimum no of workers working in date range 1-1-2019 to 20-1-2019.
The output should be 1.
Suppose my table looks like,
startDate endDate No.of.Workers
--------- --------- ---------------
1-1-2019 10-1-2019 1
11-1-2019 20-1-2019 1
11-1-2019 15-1-2019 1
The output should be 2.
Is there any query for this in sql or i need to write an algorithm?
I am using mysql database.
You can get the number of workers needed by splitting the data, aggregating and using cumulative sums:
with dtes as (
select startDate as dte, numworks
from t
union all
select endDate as dte, - numworks
from t
)
select dte, sum(numworks),
sum(sum(numworks)) over (order by dte) as needed
from dtes
group by dte
order by dte;
To get the maximum, you can do something like this:
select dte, sum(numworks),
sum(sum(numworks)) over (order by dte) as needed
from dtes
group by dte
order by needed desc
fetch first 1 row only;
You don't specify the database, fetch first is ISO/ANSI standard SQL.
Also, it is not clear if the end date counts as one of the days. This can affect the results. If it is included, then you need to add one day to the "endDate" part of the logic. How you do that depends on your database.

Get the max value of different sum values in sql

I have a table called "Sold_tickets" with attributes "Ticket_id" and "Date_sold". I want to find the day when the most tickets have been sold and the amount of tickets that were sold.
ticket_id date_sold
1 2017-02-15
2 2017-02-15
3 2017-02-14
In this case I want my output to look like this:
date_sold amount
2017-02-15 2
I know you can use a query like this
SELECT Count(ticket_id)
FROM Sold_tickets
WHERE date_sold = '2017-02-15';
to get an output of 2. The same can of course be done for 2017-02-14 to get an output of 1. However, then I have to manually check all the dates and compare them myself. Does a function exist (in sqlite) that counts the tickets sold for all the dates and then shows you only the maximum value?
Try using a GROUP BY aggregation query, then retain only the record having the maximum number of sales.
SELECT date_sold, COUNT(*)
FROM Sold_tickets
GROUP BY date_sold
ORDER BY COUNT(*) DESC
LIMIT 1
This solution would work well assuming that you don't have two or more dates tied for the greatest number of sales, or, if there is a tie, that you don't mind choosing just one date group.

Mysql grabbing correct price depending on date

I have a table that looks like the following:
The query below obviously returns all records in the table
SELECT * FROM pricing
My problem I have is that I want to display the correct price based on todays date. I know I can grab todays date using CURDATE() but how do I grab the row that shows the price of 70.00 as this is the correct price until today is equal to or greater than 2017-02-01?
Thanks in advance.
John
select price from table where product = YourProduct and date <= YourDate order by date desc limit 1

Query to find total records inserted in a range of dates

I am using SQL Server 2008 R2 and trying to find total records inserted between a range of dates grouped by insertdate. Since my insertdate contains hour,min and sec, all the records are being counted individually with same date but different time.
I want to keep the time information in my database but only in the query, I want to group by date omitting time.
#Created Date TotalRegistration
2014-10-20 14:40:47.757 1
2014-10-20 12:27:27.923 1
2014-10-20 12:25:25.613 1
should be
#Created Date TotalRegistration
2014-10-20 3
This is what I tried. Any suggestion will be really appreciated.
Thank You
Select Insertdate, COUNT(Insertdate) as TotalDailyRegistration from Customer where Insertdate between '2014-10-01 00:00:00.001' and '2014-11-08 23:59:59.743' group by Insertdate
Update: Followed Lamak's suggestion and it worked out fine. Since the dates were not ordered correctly, I also added order by CONVERT(DATE,Insertdate) after group by and its perfect.
It should be better to just group by the date part of your column:
SELECT CONVERT(DATE,Insertdate) InsertDate,
COUNT(Insertdate) as TotalDailyRegistration
FROM Customer
WHERE Insertdate >= '20141001'
AND InsertDate < '20141002'
GROUP BY CONVERT(DATE,Insertdate)
Just try this:
create table #test( createdDate datetime,totalRegistration int)
insert into #test values('2014-10-20 14:40:47.757',1)
insert into #test values('2014-10-20 12:27:27.923',1)
insert into #test values(' 2014-10-20 12:25:25.613',1)
select *From test
Select convert(nvarchar(10),createdDate,103), sum(totalRegistration) as TotalDailyRegistration

how to count records with newest date only

How do I modify this MySQL query to only count leadIDs from table leads where column 'Date' contains the newest (youngest) date?
SELECT COUNT(leadID) as accepted FROM leads WHERE change like '%OK%'
The problem is that leadID can have multiple instances in table leads. The original query result is "4" because of one duplicate. The correct result is "3".
The date is stored in this format: 2011-10-26 18:23:52. The result should take hours and minutes into consideration when determining the youngest date.
TABLE leads:
leadID | date | change
1 | 2011-10-26 18:23:52 | BAD
1 | 2011-10-26 17:00:00 | OK
2 | 2011-10-26 19:23:52 | OK
3 | 2011-10-26 20:23:52 | OK
4 | 2011-10-26 21:23:52 | OK
5 | 2011-10-26 22:23:52 | BAD
I think this is what you're looking for:
select count(distinct l1.leadId) as accepted from leads l1
left join leads l2
on l1.leadId = l2.leadId and l1.date < l2.date
where l2.date is null and l1.`change` like '%OK%'
You must decide what you mean by newest date: the single latest? yesterday? today?
if yesterday, then add this to your query clause
select * from mytable where date >= date_sub(now(), interval 1 day)
if you are using oracle database you can use max() function to extract newest date from the table, further to check with the table for this newest date :-
SELECT COUNT(leadID) as accepted FROM leads WHERE change like '%OK%'
and date_col = (select max(date_col) from leads)
I am assuming that with newest date your mean is about newest in the table data..
changes :- as per changes in question and as per mentioned in commends ..
I think you want to take newest date among the records having "change" column value like '%OK%' and want to count distinct leadId
please try the following query-
SELECT COUNT(distinct leadID) as accepted FROM leads WHERE change like '%OK%'
and date_col = (select max(date_col) from leads WHERE change like '%OK%')
You can try (in case your date is a int like return by time() function)
$sql = "SELECT COUNT(leadID) as accepted FROM leads WHERE change like '%OK%' ORDER BY Date DESC LIMIT 1"
You will only extract the newest entry.
Edit: This shouldalso works for your date format YYYY-MM-DD hh:mm:ss
Edit 2: Okay, I did not understood your question.
You have a table lead: leadid date
You want to count the number of row for the newset date.
Like another pointed out you can use the MAX operator:
SELECT COUNT(distinct leadid)
FROM LEAD AS l,
( SELECT MAX(Date) mdate FROM Lead ) AS MaxDate
WHERE l.date = MaxDate.mdate
AND l.change like '%OK%'