I will be covering three problems here
1. Fetch Latest record of each group
2. Fetch N Latest records of each group
3. Delete all records of a group except the Latest ones.
Say we have a table Products as
First, We wish to write a query that fetches the latest product shipped by each supplier. After scratching head for a while, I could find two ways to do it.
1. Write a Nested Query:
Fetch the results grouped by SupplierId and for each group, pass the ProductID that is maximum for that group. Query for this would be.
select *
from Products as a
where ProductId = (select MAX(ProductId)
from Products as b
where a.SupplierID = b.SupplierID)
group by SupplierID
Below image shows the execution for this query.
2) Self Join:
Another way is to perform a left join of the Products Table with itself. We will have to place two conditions on join. First will be on SupplierID, as we want to find the latest product by each supplier and second would be on ProductID with the constrain that first table's productID is lesser than second. Doing so, the maximum ProuductID in table 1 will not be matched to any in table 2.
For example, if we have 3 products for a supplier as
-----------------------------------
|ProductID | Supplier ID|
-----------------------------------
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
-----------------------------------
If we perform a self join with the conditions that their Supplier Ids are equal and first table's ProductID is lesser than second , we will get
---------------------------------------------------------------------------
| a.ProductID | a.SupplierID | b.productID | b.supplierid |
---------------------------------------------------------------------------
| 1 | 1 | 2 | 1 |
| 1 | 1 | 3 | 1 |
| 2 | 1 | 3 | 1 |
| 3 | 1 | NULL | NULL |
---------------------------------------------------------------------------
Now, If we place where clause saying that b.productID should be NULL, we will get the desired result.
select a.*
from Products as a left join Products as b on (a.SupplierID=b.SupplierID
and a.ProductID < b.ProductID)
where b.ProductID IS NULL
Below image shows the execution for this query.
Fetch N Latest Records
Similarly, in order to fetch N latest records we can write
select a.*
from Products as a left join Products as b on (a.SupplierID=b.SupplierID
and a.ProductID < b.ProductID)
group by b.ProductID
having count(*) < 2
Below image shows the execution for this query.
Delete records except latest in each Group
Continuing the above logic, query for deletion would be
delete
from Products as a left join Products as b on (a.SupplierID=b.SupplierID AND a.ProductID < b.ProductID)
where b.ProductID IS NOT NULL
1. Fetch Latest record of each group
2. Fetch N Latest records of each group
3. Delete all records of a group except the Latest ones.
Say we have a table Products as
First, We wish to write a query that fetches the latest product shipped by each supplier. After scratching head for a while, I could find two ways to do it.
1. Write a Nested Query:
Fetch the results grouped by SupplierId and for each group, pass the ProductID that is maximum for that group. Query for this would be.
select *
from Products as a
where ProductId = (select MAX(ProductId)
from Products as b
where a.SupplierID = b.SupplierID)
group by SupplierID
Below image shows the execution for this query.
2) Self Join:
Another way is to perform a left join of the Products Table with itself. We will have to place two conditions on join. First will be on SupplierID, as we want to find the latest product by each supplier and second would be on ProductID with the constrain that first table's productID is lesser than second. Doing so, the maximum ProuductID in table 1 will not be matched to any in table 2.
For example, if we have 3 products for a supplier as
-----------------------------------
|ProductID | Supplier ID|
-----------------------------------
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
-----------------------------------
If we perform a self join with the conditions that their Supplier Ids are equal and first table's ProductID is lesser than second , we will get
---------------------------------------------------------------------------
| a.ProductID | a.SupplierID | b.productID | b.supplierid |
---------------------------------------------------------------------------
| 1 | 1 | 2 | 1 |
| 1 | 1 | 3 | 1 |
| 2 | 1 | 3 | 1 |
| 3 | 1 | NULL | NULL |
---------------------------------------------------------------------------
Now, If we place where clause saying that b.productID should be NULL, we will get the desired result.
select a.*
from Products as a left join Products as b on (a.SupplierID=b.SupplierID
and a.ProductID < b.ProductID)
where b.ProductID IS NULL
Below image shows the execution for this query.
Fetch N Latest Records
Similarly, in order to fetch N latest records we can write
select a.*
from Products as a left join Products as b on (a.SupplierID=b.SupplierID
and a.ProductID < b.ProductID)
group by b.ProductID
having count(*) < 2
Below image shows the execution for this query.
Delete records except latest in each Group
Continuing the above logic, query for deletion would be
delete
from Products as a left join Products as b on (a.SupplierID=b.SupplierID AND a.ProductID < b.ProductID)
where b.ProductID IS NOT NULL
http://www.dailymotion.com/video/x20einz_get-tm-watch-usa-vs-belgium-live-fifa-world-cup-2014-streaming-in-brazil_videogames
ReplyDeletehttp://www.dailymotion.com/video/x20eihn_watch-fifa-world-cup-2014-live-stream-all-match-live-action-in-brazil_travel
http://www.dailymotion.com/video/x20ei7c_fi%E1%B4%B4fa-tm-united-states-vs-belgium-live-stream-2014_fun
http://www.dailymotion.com/video/x20ehv8_watch-tm-usa-vs-belgium-live-stream-online-fifa-soccer-world-cup-2014_shortfilms
http://www.dailymotion.com/video/x20ehnn_watch-belgium-vs-usa-live-stream-online-fifa-soccer-world-cup-2014_sport
http://www.dailymotion.com/video/x20ehf9_fifa-world-cup-argentina-vs-switzerland-live-stream-watch-online-2014_people
http://www.dailymotion.com/video/x20eh76_now-tm-switzerland-vs-argentina-live-stream-watch-online-fifa-world-cup-2014_music
http://www.dailymotion.com/video/x20egws_watch-argentina-vs-switzerland-live-stream-online-fifa-world-cup-2014_sport
http://www.dailymotion.com/video/x20egpt_fifa-world-cup-2014-germany-vs-algeria-live-stream-watch-online_tech
http://www.dailymotion.com/video/x20egli_%E1%B4%B4%E1%B4%B0now-tm-watch-algeria-vs-germany-live-stream-online-fifa-world-cup-2014_school
http://www.dailymotion.com/video/x20egbt_watch-germany-vs-algeria-live-stream-online-free-fifa-world-cup-2014_sport
http://www.dailymotion.com/video/x20efwb_fifa-world-cup-greece-vs-costa-rica-live-stream-watch-online-free-2014_sport
http://www.dailymotion.com/video/x20efqf_watch-costa-rica-vs-greece-live-stream-online-free-fifa-world-cup-2014_news
http://www.dailymotion.com/video/x20efis_watch%E1%B4%B4%E1%B4%B0-mexico-vs-netherlands-live-stream-online-free-fifa-world-cup-2014_music
http://www.dailymotion.com/video/x20efao_now-tm-netherlands-vs-mexico-live-stream-watch-online-free-fifa-world-cup-2014_sport
http://www.dailymotion.com/video/x20eeow_watch-brazil-vs-chile-live-stream_lifestyle
http://www.dailymotion.com/video/x20eevq_now-tm-watch-colombia-vs-uruguay-live-stream-online-free-fifa-world-cup-2014_fun
http://www.dailymotion.com/video/x20ef12_hq-hd-uruguay-vs-colombia-live-stream-watch-online-free-fifa-world-cup-2014_sport
http://www.dailymotion.com/video/x20eein_hd-fifa-world-cup-2014-watch-brazil-vs-chile-live-stream-online_lifestyle
http://www.dailymotion.com/video/x20ee7e_now-tm-watch-brazil-vs-chile-live-stream-online-fifa-world-cup-2014_sport
http://steamcommunity.com/id/chilevsbrazillivestreamespn/
https://www.surveymonkey.com/s/KD2HKYY
https://www.surveymonkey.com/s/KD3X57C
https://www.surveymonkey.com/s/KYK77PB
http://www.kidsfront.com/
ReplyDeleteBest online tutorial site for your study.give test here