How to get minimum date by group id per client id - sql

I am trying to Select Group ID, and minimum dates per clients ID.
This is sample data set.
ClientID GroupID DataDate
1 9 2016-05-01
2 8 2015-04-01
3 7 2016-07-05
1 6 2015-01-05
1 5 2014-11-12
2 4 2016-11-02
1 3 2013-02-14
2 2 2011-04-01
I wrote
SELECT
clientID, MIN(DataDate)
FROM sampleTable
GROUP BY clientID
But in this query, I do not have GroupID selected. I need to include GroupID to join another table.
If I do:
Select
ClientID, GroupID, MIN(DataDate)
FROM sampleTable
GROUP BY ClientID, GroupID
It won't really get minimum dates per client.
Could you help me. How I should do this?

You can use ROW_NUMBER instead:
SELECT
ClientID, GroupID, DataDate
FROM (
SELECT *,
rn = ROW_NUMBER() OVER(PARTITION BY ClientID ORDER BY DataDate)
FROM SampleData
) t
WHERE rn = 1
If you want to include ties, use RANK instead of ROW_NUMBER.

I hope i understood your question correctly .
You want to display min dates for each client id's
If my table has data like this:
CID GID D1
1 9 03-06-2016
1 6 01-06-2017
1 5 01-06-2015
1 3 01-06-2014
2 4 01-06-2017
2 8 01-06-2014
3 5 03-06-2016
2 4 01-06-2011
Output :
CID GID D1
1 3 01-06-2014
2 4 01-06-2011
3 5 03-06-2016
This is what i think you can go with .
select cx.cid,cx.gid, cx.d1 from cli cx where cx.d1=(select min(c1.d1) from cli c1 where c1.cid=cx.cid)
group by cx.cid,cx.gid,cx.d1
order by cx.gid
Hope it helps.

Related

Select unique field values with condition

I`m new at SQL and my problem is:
I have a table like
card shop time date
1 1 0000 20171001
2 2 0125 20171002
2 1 0344 20171002
3 3 0342 20171103
4 5 1334 20171104
4 4 1225 20171105
5 4 1452 20171106
I need to select two fields (card(card must be unique) and shop) by the minimum value of the columns time and date (date is in priority).
The result should look like this:
card shop time date
1 1 0000 20171001
2 2 0125 20171002
3 3 0342 20171103
4 5 1334 20171104
5 4 1452 20171106
Thank you in advance!
For SQL Server you could use WITH TIES
select top 1 with ties *
from yourTable
order by row_number() over (partition by card order by date asc, time asc)
you can use sub-query and aggregate function
select * from yourtable t
where t.date in (select min(date) from yourtable t1
where t.card=t1.card )

SQL: SELECT value for all rows based on a value in one of the rows and a condition

I have a list of total store visits for a customer for a month. The customer has a home store but can visit other stores. Like the table below:
MemberId | HomeStoreId | VisitedStoreId | Month | Visits
1 5 5 1 5
1 5 3 1 2
1 5 2 1 1
1 5 4 1 7
I want my select statement to give the number of visits to the home store against each store for that member for that month. Like the below:
MemberId | HomeStoreId | VisitedStoreId | Month | Visits | HomeStoreVisits
1 5 5 1 5 5
1 5 3 1 2 5
1 5 2 1 1 5
1 5 4 1 7 5
I've looked at a SUM with CASE statements inside and OVER with PARTITION but I can't seem to work it out.
Thanks
I would use window functions:
select t.*,
sum(case when homestoreid = visitedstoreid then visits end) over
(partition by memberid, month) as homestorevisits
from t;
SELECT MemberID,HomestoreID,visitedstoreid,Month,visits, homestorevisits
FROM Table LEFT OUTER JOIN
(SELECT MemberID, Visits homestorevisits
FROM TABLE WHERE homestoreID =VisitedStoreId
)T ON T.MemberID = Table.MemberID
You can achieve this using a simple subquery.
SELECT MemberId, HomeStoreID, VisitedStoreID, Month, Visits,
(SELECT Visits FROM table t2
WHERE t2.MemberId = t1.MemberId
AND t2.HomeStoreId = t1.HomeStoreId
AND t2.Month = t1.Month
AND t2.VisitedStoreId = t2.HomeStoreId) AS HomeStoreVisits
FROM table t1

Select Query to Get Unique Cells in Two Columns

I have an SQL Server database, that logs weather device sensor data.
The table looks like this:
Id DeviceId SensorId Value
1 1 1 42
2 1 1 3
3 1 2 30
4 2 2 0
5 2 1 1
6 3 1 26
7 3 1 23
8 3 2 1
In return the query should return the following:
Id DeviceId SensorId Value
2 1 1 3
3 1 2 30
4 2 2 0
5 2 1 1
7 3 1 23
8 3 2 1
For each device the sensor should be unique. i.e. Values in Columns DeviceId and SensorId should be unique (row-wise).
Apologies if I'm not clear enough.
If you don't want to sum Value as your desired result suggest, so you just want to take an "arbitrary" row of each "DeviceId + SensorId"-group:
WITH CTE AS
(
SELECT Id, DeviceId, SensorId, Value,
RN = ROW_NUMBER() OVER (PARTITION BY DeviceId, SensorId ORDER BY ID DESC)
FROM dbo.TableName
)
SELECT Id, DeviceId, SensorId, Value
FROM CTE
WHERE RN = 1
ORDER BY ID
This returns the row with the highest ID per group. You need to change ORDER BY ID DESC if you want a different result. Demo: http://sqlfiddle.com/#!6/8e31b/2/0 (your result)

Getting record against nearest coming date for each distinct PID

I have a table active and data in table is as following.. Sample Data
id pid chq_date
-------------------------
1 6 2013-07-07
2 4 2013-10-06
3 4 2013-07-06
4 5 2013-01-06
5 13 2013-09-16
6 33 2013-09-08
7 4 2013-02-06
8 13 2013-01-06
9 7 2013-07-06
10 4 2013-08-02
I need to get records of nearest coming date for each distinct pid Desired output from sample data is
id pid chq_date
-------------------------
1 6 2013-07-07
3 4 2013-07-06
5 13 2013-09-16
6 33 2013-09-08
9 7 2013-07-06
I have tried different queries but did not get desired result. The query which seems me nearest is
select * from active where chq_date>=sysdate and chq_date in (select
min(chq_date) from active where pid in (select distinct pid from active))
Fiddle Link
Some changes in sashkello's answer as group by can not be used that way so I used it in inner query
select * from active where chq_date in (SELECT MIN(chq_date)
FROM active WHERE chq_date >= SYSDATE GROUP BY pid)
Fiddle Link
Try something like:
SELECT
id,
pid,
MIN(IF(chq_date > SYSDATE, chq_date, NULL))
FROM active
GROUP BY pid
Or if you want to remove pids which don't have upcoming dates:
SELECT
id,
pid,
MIN(chq_date)
FROM active
WHERE chq_date > SYSDATE
GROUP BY pid

sql assign a category id when using order by

I'm new to SQL.
When I order by SomeData on my table I get:
ID SomeData
6 ABC
3 ABC
12 FG
1 FH
2 GI
4 JU
8 K3
5 K3
11 P7
great. but what i really want on output is
ID Category
6 1
3 1
12 2
1 3
2 4
4 5
8 6
5 6
11 7
That is every time SomeData changes on the sort I want to increment Category by one
I can't see how to do this. Any help would be greatly appreciated. Thanks.
If you are on SQL-Server, you can use the DENSE_RANK() ranking function in combination with OVER:
SELECT ID
, DENSE_RANK() OVER (ORDER BY SomeData)
AS Category
FROM myTable
ORDER BY SomeData
See: SQL-Server: Ranking Functions
SELECT ROW_NUMBER() OVER(ORDER BY SomeData ASC) AS Category, otherfield1..