I’m using SQL and I have some dummy car dealership data that for a given dealership tells me whether a particular make is currently being sold there, and if not, when the last time that make was available at that dealership. An example of a few rows of data for one dealership looks like this, focusing only on the fields of interest:
dealership_ID | make | Available? | brought_in_date | sold_date |
---|---|---|---|---|
612 | BMW | Yes | 2024-11-23 | NULL |
612 | BMW | No | 2024-09-13 | 2024-12-05 |
612 | Audi | No | 2024-10-15 | 2024-10-28 |
612 | Audi | No | 2024-09-06 | 2024-11-03 |
612 | Mercedes Benz | Yes | 2024-10-20 | NULL |
What I’m trying to do is return one row per dealership that tells me for given car makes, whether they are currently available and if not, how many days has it been since they were last available
using dealership ID 612 as an example, it would return something like this:
dealership_ID | BMW | Audi | Mercedes Benz | Ford |
---|---|---|---|---|
612 | 0 | 61 | 0 | NULL |
Where BMW and Mercedes are 0 since at least one of each is available right now, Audi is 61 as it has been that many days since an Audi was last available at the dealership (audi with most recent sold_date) and Ford is Null as a ford has never been sold at this particular dealership before.
2
Answers
Well you can always simplify things and just use the
with clause
as in:Break through:
For every dealership and make, find out whether the car is in stock now or the most recent
sold_date
for out-of-stock cars.Calculate date difference between today and the most recent
sold_date
for not currently in stock cars.Rows to columns on each car make, respective values
0
, the number of days orNULL
.Rather than messing with dynamic pivots, you should just return the result in long form.
You can use some fairly simple left-join and aggregation logic for this, but you need a table which lists all possible
Make
s, or put them in aVALUES
clause.db<>fiddle