Relational data with modifications/variants - postgresql

Let's say a supermarket has one table with statistical revenue data by business day. In a second table they want to try out different variations of these data, f.e. less bakery revenue at 1st January, one more public holiday or maybe opening on sundays.
I do not want to duplicate the whole revenue data of a year or more for just a few variations. Additionally it should be easily possible to show the diff between the base table and one alternative scenario.
To make it a little more clear, I created a demo case at SQLFiddle. SUPERMARKET_DAYS is the table with the base revenue data. ALTERNATIVES are the variations/alternatives or scenarios (however you want to name it) and ALTERNATIVE_DAYS is a table with a similar structure as the first one and holds the concrete changes.
Example at SQLFiddle
SUPERMARKET_DAYS
(
ID,
DAY,
NUM_CUSTOMERS,
NUM_VISITORS,
REVENUE_BAKERY,
REVENUE_BEVERAGE,
REVENUE_CANNED,
REVENUE_DAIRY,
REVENUE_DRY,
REVENUE_FROZEN,
REVENUE_MEAT,
REVENUE_CLEAN,
REVENUE_PERSONAL_CARE,
REVENUE_PAPER,
REVENUE_OTHER
)
ALTERNATIVES
(
ID,
TITLE
)
ALTERNATIVE_DAYS
(
ID,
DAY,
NUM_CUSTOMERS,
NUM_VISITORS,
REVENUE_BAKERY,
REVENUE_BEVERAGE,
REVENUE_CANNED,
REVENUE_DAIRY,
REVENUE_DRY,
REVENUE_FROZEN,
REVENUE_MEAT,
REVENUE_CLEAN,
REVENUE_PERSONAL_CARE,
REVENUE_PAPER,
REVENUE_OTHER
)
I do not even know, if this schema design makes any sense but it illustrates the problem.
How would you store this kind of data?
I thought about something like a versioning table but this does not really solve my problem.
I could merge both tables inside of my java code.
Is there something like the MERGE functionality for views?
I would really be grateful for every kind of idea or the correct keyword for my own investigations. This problem should already exist but I didn't find much to solve it, so I maybe did not search with the correct terms.
Thank you everybody!

Related

Tableau: relationship between tables with different fields

I am really new to Tableau.
I have an "accident" table (excel) that describes each traffic accident in the past few years, including its "district" (location). I have another "district" table that describes each district of the city, including its population.
Now I want to join those two tables and create a graph of accidents per person for each district.
The problem I face is: The two excel files are from different databases, which means that the same district may appear to have different names in two tables. How do I let tableau know the matching between districts?
Could you tell me how I can join those two tables so that I can create my chart?
Please let me know if there are any problems with my approach or understanding. Thank you in advance!
Assuming I understand question correctly.
Your accident transactions might have..
District#1, Accident Date, ,
District#2, Accident Date, ....
District#3, Accident Date, ....
Your district dimension table has 1 unique record per district, but names don't match.
District1, CityName
District2, CityName
District#3, CityName
You want to combine the results correctly...
Excel is the preferred datasource?
I assume the actual sources of data do NOT have a singular "code" value for the district name that actually does match. This is where MDM is important for system integrations. Right answer IMO is to actually have source systems/dbms understand that they are integrated and create XREF in 1 system that is required when a district is setup or exists.
Options. I think you need to "clean" the data someplace. The question is where.
MDM in your source system integrations.
Are the excel sources created in automated fashion? You could create a manual XREF excel sheet and lookup/decode one of the district values to conform a single definition of the district. In addition to your graph. create an audit that looks for any new data that doesn't "lookup" correctly to know to maintain the manual XREF.
Probably could create calculated XREF is tableau calc itself and then blend based on that calculated field. I think that would require full desktop versus web editor.
Tableau Prep might help as well. (although my company doesn't use this)

TSQL - Deleting with Inner Joins and multiple conditions

My question is a variation on one already asked and answered (TSQL Delete Using Inner Joins) but I have a different level of complexity and I couldn't see a solution to it.
My requirement is to delete Special Prices which haven't been accessed in 90 days. Special Prices are keyed on Customer ID and Product ID and the products have to matched to a Customer Order Detail table which also contains a Customer ID and a Product ID. I want to write one function that will look at the Special Price table for each Customer, compare each Product for that Customer with the Customer Order Detail table and if the Maximum Order Date is more than 90 days earlier than today, delete it from the Special Price table.
I know I can use a CURSOR (slow but effective) but would prefer to have a single query like the one in the TSQL Delete Using Inner Joins example. Any ideas and/or is more information required?
I cannot dig more on the situation of your system but i think and if it is ok for you, check MERGE STATEMENT, it might be a help instead of using cursors. check this Link MERGE STATEMENT

FullText Index - Searching values from another table

Is it possible, in SQL Server 2008, using the full text index syntax, to run a query such as this one?
SELECT *
FROM TABLE_TO_SEARCH S,
TABLE_WITH_STRINGS_TO_SEARCH SS
WHERE
CONTAINS(S.WHOLE_NAME,SS.FIRST_NAME)
OR CONTAINS(S.WHOLE_NAME,SS.LAST_NAME)
I need to search for the FIRST_NAME in table TABLE_TO_SEARCH, column WHOLE_NAME that has an full text index on it. It doesn't seem to be a valid query though... Is there any workaround to it by using the full text index search?
LATER EDIT:
Here is the business case: each night I am downloading from several websites information about "blacklisted" individuals and insert it into a table in this format: WholeName, LastName, FirstName, MiddleName. But the data is chaotic as WholeName does not necessarily contain either the last, first or middle name or the WholeName is null while the other 3 fields have values, or every of these 4 fields is null and so on. Also, the data may repeat itself as one blacklisted individual may come from 2+ of these websites. What I need to do is to compare this data, as chaotic as it is, against our customer data based on our customer's First and Last name and give it a matching score (rank) against the files we download from these websites.
First I tried with charindex or like operators but I couldn't create a scoring algorithm based on this and also it took 6 hours to compare just our customer's first and last name with only the WholeName column from the TABLE_TO_SEARCH table. I thought that perhaps implementing the full_text index it would get easier and faster but ... apparently I was wrong.
Has anyone dealt with a task like this? And if so, what was the best approach?
After skimming http://technet.microsoft.com/en-us/library/ms187787.aspx and http://technet.microsoft.com/en-us/library/ms142571.aspx I don't think it is possible to do your search in this way. Not only that, but it seems this type of index wouldn't work well with names anyway.
If you care about checking one name then all you have to do is set those values to variables. This method would allow you to use the full-text index.
Otherwise, I would suggest splitting the WHOLE_NAME column (if there is a space or unique character between the first and last name) and comparing each part to those other columns. If you are working with a huge data set, you may want to experiment with doing this at a temp table level and creating an index.
Good luck!

Inserting a record into Top of Table

Hopefully an easy question for someone with more experience than me. I have a stored procedure that Inserts records into a table. Like all databases that I have worked with, when you insert a record it inserts it into the bottom of the table. I would like to insert it to the top of the table and then move all the existing records down by one (I assume this would happen automatically with the insert).
I want to to do this because I'm using the 'Top #' keyword. I am pretty sure that I could just leave it the way it is, and instead of using the 'Top" keyword, I could use the 'Bottom" keyword. But I want to make it easier for people reading it that aren't familiar with it, so they can instantly see the newest entries. I'm going to keep researching this on my own, but If someone knew off the top of their head and could save me the time that would be appreciated.
is there any incremental id on that table.If yes then create clustered index on that id with descending order

Storing multiple test answers in Access

I'm fairly new to Access and have gotten stuck at a point despite hours of on-line research. In short, I'm trying to write a database that will store the answers that people give on several different tests. Some people take 1 test, some take 2, 3, etc. I need to store for each student what test(s) they took and what their answers were for each question. I feel like my current approach (make a separate field for each question on my MainRecord table along with a yes/no field for each test that can be taken) is cumbersome and leading to my ultimate problem: when I populate a continuous form with all of the test questions and an adjacent combo box to input their answer, I can't transcribe the combo box value back into my MainRecord. The data for the continuous form comes from a separate table (Test1) which has a field for question number and a lookup field that allows me to select the person's answer (i.e. A,B,C,D,E).
Is there a better way to construct my tables? If not, how can I get the combo box values on a continuous form into different fields on a table? Thanks, sorry if I sound like a moron.
You're going to need to look into a more generalized structure.
Here's a really basic structure that should work.
I can't help too much with all the continuous form stuff
Test
test_id
TestQuestion
test_id
question_id
question_order (used for sorting)
question_text
QuestionPossibleAnswers
question_id
possible_answer_value
possible_answer_prompt
Student
student_id
student_name
// etc...
StudentTest
test_id
student_id
date_taken
// whatever
(assuming a student can only take a test once)
StudentAnswers
student_id
question_id
student_answer (would be the possible_answer_value from "QuestionPossibleAnswers"
Anyway, when a student takes a test, your top form would be 'bound' to the 'StudentTest' table i guess. The continuous form would be based probably on StudentAnswer. The student_answer drop down would need to be bound to the current StudentAnswer.question_id's possible answers (through the query builder).
It's been years since i've done Access so i can't give step by step, I apologize, but the structure above is pretty sound (if not overly simple).