Select a column only if id is distinct MySQL - php

I have the following table:
AMAZON_ID | DATE | STATUS
1 | 01/03/2014 | Pending
1 | 01/03/2014 | Shipped
2 | 01/04/2014 | Pending
3 | 01/05/2014 | Cancelled
4 | 01/06/2014 | Pending
How can I select the earliest date from table where status is equals Pending where the count of id is not more then one, it should be like the following:
AMAZON_ID | DATE | STATUS
2 | 01/04/2014 | Pending
I can not figure that out, this is what I have so far but its not working:
SELECT date
FROM table
WHERE status = 'Pending'
AND COUNT(id) < 2
ORDER BY date ASC
LIMIT 1;

Use a subquery to GROUP BY the id's that have COUNT of 1. Make sure your id is IN the results of this subquery, with a status of pending. Then ORDER BY date and LIMIT to the first result.
SELECT date
FROM table
WHERE
status = 'Pending' AND
id IN (
SELECT id
FROM table
GROUP BY id
HAVING COUNT(*) = 1
)
ORDER BY date ASC
LIMIT 1;

One thing you can do is use a WHERE IN, and use a select statement to populate the WHERE clause with distinct ids
SELECT date
FROM table
WHERE status = 'Pending'
AND id IN (SELECT DISTINCT id FROM table)
ORDER BY date ASC
LIMIT 1;

It depends on what you really mean by "count of id is not more than one".
I have renamed your column date to ts to make the queries valid SQL (as DATE is a SQL function so it may confuse parsers). See the SQL fiddle for the complete schema I used and the example queries.
Option 2: one row per ID total
In this case you have a problem because you want a query to get one piece of information (the minimum date) that is based on a different piece of information about the same table. This means that you need to queries, luckily you can just join the data of the two queries and filter appropriately.
SELECT data.id, status, MIN(ts)
FROM data
JOIN (SELECT id, COUNT(*) AS amount FROM data GROUP BY id) AS totals
ON totals.id = data.id
WHERE
status = "Pending"
AND totals.amount < 2
GROUP BY data.status;
Option 1 (easy): only one pending row per ID
NOTE: this answer does not answer the OP's question
In this case, the situation is quite easy as you can just filter all your rows with status "pending", the minimum date and the total amount of rows that match your result and, after the aggregation (i.e.: using HAVING) filter those that have less than 2 amounts.
In SQL that would be:
SELECT id, status, MIN(ts) AS minTS, COUNT(*) AS amount
FROM data
WHERE
status = "Pending"
GROUP BY id, status
HAVING amount < 2

Related

Combining sum() of similar entry's, and including a WHERE clause SQL

I am trying to add up the the numbers from amount, if the booking_id matches. I have been having trouble with summing these numbers as well as adding in a WHERE clause.
This is my code so far
At this time it is summing the numbers correctly, although if I add in the where it doesn't show any results
select *, Sum(amount) FROM payment group by booking_id where booking_id = 1
I have tried some methods like adding queries in queries but I have had no luck. The results I'm attempting to get are below.
booking_id amount
----------------------
1 | 5
2 | 6
1 | 6
3 | 2
3 | 3
4 | 4
Output should be:
booking_id amount
----------------------
1 | 11
2 | 6
3 | 5
4 | 4
I am attempting to group the results so that booking_id with a value of 1 will return a sum of 5+6.
My goal is to be able to add together the amounts that have the same booking id. As well as to be able to include a WHERE clause within this query
To get all booking_id's as requested, simply skip the WHERE clause:
select booking_id, Sum(amount)
FROM payment
group by booking_id
To get all booking_id's for a specific ident (as later requested), add the WHERE clause:
select booking_id, Sum(amount)
FROM payment
where client_id = 123
group by booking_id
Where should be before group by
select booking_id , Sum(amount) FROM payment
where booking_id = 1
group by booking_id
you did wrong only in order to write sql
select booking_id, Sum(amount) FROM payment
where client_id = 1 -- your condition
group by booking_id
Check this link for order

Select distinct by highest or lowest value

I'm working on a track and field ranking database in MySQL/PHP5 whereby I'm struggling to find the best way to query results per unique athlete by highest value.
just
SELECT distinct name, event
FROM results
sample database
name | event | result
--------------------------
athlete 1 | 40 | 7.43
athlete 2 | 40 | 7.66
athlete 1 | 40 | 7.33
athlete 1 | 60 | 9.99
athlete 2 | 60 | 10.55
so let's say that in this case I'd like to rank the athletes on the 40m dash event by best performance I tried
SELECT distinct name, event
FROM results
WHERE event = 40
ORDER by result DESC
but the distinct only leaves the first performance (7.43) of the athlete which isn't the best (7.33). Is there an easy way other than creating a temp table first whereby the results are ordered first and performing a select on the temp table afterwards?
You might be interested in group by:
SELECT name, min(result) as result
FROM results
WHERE event = 40
GROUP BY name
This gives you the best result per athlete.
As suggested by spencer, you can also order the list by appending this:
ORDER BY min(result) ASC
The problem is that the columns used in the ORDER BY aren't specified in the DISTINCT. To do this, you need to use an aggregate function to sort on, and use a GROUP BY to make the DISTINCT work.
SELECT distinct name, event
FROM results
WHERE event = 40
GROUP BY name
ORDER by result DESC

MySQL: Aggregation the similar value fields from Table 1

I'm trying to create a query from two tables, Table 1 contains a contract between the company and the customer.
Table 2 contains a statement payments.
What I want to do is: assembling late payments from Table 2 values, so that the value of each batch 250$ and displayed as follows:
____________________________________
Client | late payments | total
____________________________________
John | 2 | 500 (250*2)
____________________________________
Table2 like this:enter image description here
First use Concat for create date and cast string as date and compare with paydate at last use group by and calculate sum or count of late paid
SELECT contract_id,count(contract_id) late_payments,sum(paid) total FROM `table`
where cast(Concat(year.'-',month,'-',day) as date) < paydate
Group by contract_id

How to check the greatest number in table in SQL using PHP

I was just wondering that, how to get the greatest number in the table.
I mean i have a table called: hits; in that their are 2 columns:
1. id
2. hit
and their are many ids in the table and all have more than 10 hits, now what i want to do is to get the greatest id of the greatest hit
PS: See below:
id | hit
---|----
1 | 10
2 | 15
3 | 45
4 | 9
yes you can use MAX function to use like below
Select Id,Max(hit) from yourtableName group by id having hit=Max(hit)
Select Id,
Max(Hit)
from tableName
group by id
having Max(hit)=(Select Max(Hit) from TableName)
SQL FIDDLE Demo
Wouldn't it be faster to do this:
SELECT * FROM table WHERE 1 ORDER BY hit DESC, id DESC LIMIT 1
Rather than using MAX, Especially if you have a larger table
http://www.witti.ws/blog/2011/04/06/mysqls-max-slow-5-years-later

Mysql interupt query and start from latest ID another query

I got a set of queries that will be interupted. Therefore I need to stop the query, save the last added ID and continue adding contents from the last ID on another day.
Szenario:
1) I query data from another table (Table-A) like this:
SELECT *
FROM $table_A
ORDER BY uID ASC
2) Then I add to a table (Table-B) that "looks" like this:
|¯¯¯¯¯|¯¯¯¯¯¯¯¯|¯¯¯¯¯¯|¯¯¯¯¯|¯¯¯¯¯|
| uID | SubID* | Name | Foo | Bar |
|-----|--------|------|-----|-----|
| 1 | 23 | Boo | Goo | Shu |
|_____|________|______|_____|_____|
3) For each row I query from the Table-A table I add data to the Table-B table. (Note: This table already has the uID, SubID and Name when I add the data.)
4) The query gets interrupted/stops. Then I save the uID* of the latest added row - before the queries stop - to a separate table (Table-C).
Question: How can I start the query - after interruption (let's say on the next day) - again, but starting from the next row after last I saved on the previous day (The uID will be available).
Example:
SELECT *
FROM $table_A
ORDER BY uID ASC
LIMIT $last_uID, COUNT $table_A (a.k.a "the last ID in the table")
Thanks!
Notes:
The SubID connect rows to other tables & can exist more than once.
UID = Unique Identifier (Auto Increment).
Well you know what the last ID added was, because it will be in table_B.
SELECT *
FROM $table_A
WHERE uID > (select max(uID) from table_B)
ORDER BY uID ASC
UPDATE:
Seems this is what is required.
SELECT all columns
FROM $table_A
WHERE uID BETWEEN $last_uID_added AND $last_uID_in_table;

Categories