How to get the earliest record and how to covert wide table to a long table in SQL

BusyBee123

New Member
Joined
Nov 1, 2022
Messages
1
Office Version
  1. 2021
Platform
  1. Windows
I have two tables. My first one looks like this:

idDistrictDateSubData
1​
Kent
10/14/2022​
6​
2​
Kent
10/16/2022​
9​
3​
Kent
10/17/2022​
10​
4​
Olympia10/18/20222
3​
5​
Olympia
10/16/2022​
5​
6​
Seattle
10/14/2022​
9​
7​
Seattle
10/15/2022​
7​


How do I make it look like this:

idDistrictDateSubData
1​
Kent
10/14/2022​
6​
5​
Olympia
10/16/2022​
5​
6​
Seattle
10/14/2022​
9​

The second one looks like this:
idGenderIsBillingualPeffLevel
1​
MaleBillL1
2​
FemaleNotBL2
3​
XBillL3
4​
MaleNotBL3
5​
FemaleBillL4
6​
FemaleNotBL1

How can I make it look like this:

idGroupPeffLevel
1​
MaleL1
1​
BillL1
2​
FemaleL2
2​
NotBL2
3​
XL3
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Welcome to the MrExcel Message Board!

One way of doing this is using a sub-select query as shown below.
FirstTable is the table name that you need to change with yours.

SQL:
SELECT FirstTable.*
FROM FirstTable
INNER JOIN
  (SELECT
     District, MIN(DateSub) AS minDateSub
   FROM
     FirstTable
   GROUP BY
    District) AS earliestRecord
ON FirstTable.District = earliestRecord.District
    AND FirstTable.DateSub = earliestRecord.minDateSub

Basically, we join the main table with itself as grouped by the minimum date value on the District and DateSub fields.

Please note that if you have the same date for the same District, and if it is the earliest date, then there will be two records listed for the same District.

Similar logic could be easily applied to the second table, however, I don't see a date field in your second data to get the "earliest" record. If you could be more specific about how you'd like to "filter" the second table, then we could adapt the same query accordingly.
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,580
Members
452,653
Latest member
craigje92

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top