The new forums will be named Coin Return (based on the most recent
vote)! You can check on the status and timeline of the transition to the new forums
here.
Alright, so Im trying to build a composite key to join to another table that is storing comments on the rows. The report feeding my table is run daily. The same row can show up multiple days and we looking to create a key based on the first consecutive instance the item appeared.
Basically Im looking to concatenate the existing composite_key and the first consecutive
report_date
H/A please help and advise
0
Posts
Let's play Mario Kart or something...
key report_date composite_key
ABC123 7/1/2014 ABC1237/1/2014
ABC123 7/10/2014 ABC1237/10/2014
DEF456 7/11/2014 DEF4567/11/2014
ABC123 7/11/2014 ABC1237/10/2014
Shouldn't all three ABC123 rows have the same composite key made up of the row and the first date it appeared?
Choose Your Own Chat 1 Choose Your Own Chat 2 Choose Your Own Chat 3
Unfortunately, it isn't that easy. That will return the minimum report date. I need the minimum consecutive report date.
Like, if ABC123 showed up on one report in 2012 and started showing up again in June 2014, it's going to be because of a completely seperate issue. We want to fly all the consecutive appearances under the same banner.
unit
issue
comments
many comments to issue
many issues to unit
You want output of unit with all issues, and comments per issue? But comments for the same issue but a different time to be rolled up into the first issue?
edit: like 2 different events for 'item broke' should be rolled into the first 'item broke' event?
Okay, so you have this data:
What is the exact row you would like to reference with the last row?
Let's play Mario Kart or something...
The composite key includes the report_date is one to one on the report table, but we need a foreign key to join to the comment table that is composite key minus the report date + the first consecutive instance of the report date.
no, we need it minimum consecutive date. That is why the 2nd and 4th row use 7/10, but the 1st uses 7/1.
Then you'll need to do an encapsulated sub-clause, or whatever they're called (I use GUI's for my SQL, just troubleshoot it). Something like Select Report, (Select Date where Report = Report and Date = Date+1) Group by Report. Honestly though, to do it in pure SQL will be a royal pain, while producing it in BI Tools or Excel will be easy, as you're really talking about 2 groups of report+dates to build your key from, 1-1 report/date and 1-many report/dates, so ideally you'll select as separate groups and union them.
Choose Your Own Chat 1 Choose Your Own Chat 2 Choose Your Own Chat 3
ooooh.... shit, I think I get it.
you want to know, day by day
so like
7/1 -> 7/1
7/2 -> 7/1
7/3 -> 7/1
7/8 -> 7/8
7/9 -> 7/8
rite?
Let's play Mario Kart or something...
If the database is huge, this may take a while, but if you are only generating it once a month who gives a shit?
Let's play Mario Kart or something...
Just thinking out loud here, but after the initial backfill (which I dont give a warm fart about tbh), shouldn't I be able to look up if there was a report sharing the key on the most previous run of the report and if so, update the current days report to that value else start new with composite + current report date?
FirstConsecutiveDate will always be the earliest consecutive date for that key, and you only need to check for one with yesterday's date and the same key.
Dunno if adding a new column is possible, though.
Choose Your Own Chat 1 Choose Your Own Chat 2 Choose Your Own Chat 3
I can create a new column, but the meat of the matter is I am struggling to identify that first consecutive date.
SELECT
MAX(report_date) as previous_report_date
from
stupidtable
WHERE
CONVERT(Char(10),GETDATE(),101) > report_date
then if exists key + previous_report_date
use that the composite key value of that report date....
(thinking out loud)
After that, though, its just filling the column with (select isnull(b.firstConsecutiveDate, todaysDate) from tblA b where b.key = newRowKey and b.reportDate = todaysDate - 1).
Choose Your Own Chat 1 Choose Your Own Chat 2 Choose Your Own Chat 3
select a.mykey, a.report_date
from stupidtable as a join stupidtable as b on a.mykey=b.mykey
where DATEDIFF(dy, a.report_date, b.report_date) = 1
edit: you could do min to just get the earliest of these but then if you had two separate consecutive occurrences you would ignore the later one
edit2: you also need to make sure the year is the same if you have multiple years, so where datediff(dy,a.report_date,b.report_date)=1 and datediff(year,a.report_date,b.report_date)=0
twitch.tv/tehsloth
I owe u beer.
I don't give a single solitary fuck about backfill. So this is perfect.
This works perfectly. And will so long as the report is run every day.