How to combine multiple Tables, appending column information based on confirming same record - sql

I'd like to append specific columns from multiple tables to each record in a summary table, but only to the records that match Account# and Ticker Symbol in the Summary table
Summary Table
id
Account
Symbol
Value
1
A
JPM
$100
2
B
TSLA
$200
3
C
GE
$300
4
D
JPM
$400
Dividends Table
Account
Symbol
Dividends
D
JPM
3.2%
Purchase Date
Account
Symbol
Purchase Date
B
TSLA
10/2/22
Expected outcome
Multi Query Table
id
Account
Symbol
Value
Dividends
Purchase Date
1
A
JPM
$100
2
B
TSLA
$200
10/2/22
3
C
GE
$300
4
D
JPM
$400
3.2%
I tried Visual Basic (I'm using MS Access 2007) and tried combining just the summary and dividend tables first, but it didn't recognize the "."
Option Compare Database
Function AddCol_1(Summary.Account, Summary.Symbol, Dividend.Account, Dividend.Symbol)

Related

How to join a fact table to a dimension table which has a duplicate key value while avoiding duplications in fact table?

How do I join a fact table to a dimension table with a duplicate key value, while at the same time avoiding duplication in the fact table that would result from the join?
Dimension table: enter image description here
fact table: enter image description here
product look-up table (another dimension table): enter image description here
I thought of using the activation date as the next unique value, but they share a month in common.
I thought of creating a snowflake schema which connects dimension table in question (marketing campaigns) to product dimension which in turn connects to the fact table with no issues.
edit:
I am designing a datawarehouse which should answer how effective marketing campaigns based on purchase data.
Purchase data which will be the core of my fact table looks like this:
product_id timestamp sales_price user_id
1 5/9/2015 120 124
2 6/9/2015 150 129
the product lookup table looks like this:
id product_name model production_cost
6 ring 2019 300
5 headband 2018 200
the marketing campaigns look up table looks like this:
startdate enddate type amount_spent currency product_id
1/1/2019 7/1/2019 print 100,000 USD 6
6/1/2019 1/1/2020 socialmedia 10,000,000 USD 6
6/1/2019 1/1/2020 socialmedia 10,000,000 USD 3
The issue is that the marketing table has duplicate product id value of 6. So, when I use it as my natural key to create a surrogate primary key for that dimension table and pull that surrogate key to the fact table as a foreign key it's going to cause duplications for anything with product_id of 6 (as it's not unique). How do I connect marketing campaigns data to fact table, whilst keeping the data integrity intact -- that is no duplications?
I thought about combining start/end date with product_id to create a composite primary key, but they share/overlap a month (6/1/2019 to 7/1/2019)
I also thought about connecting the purchases (fact table) to product lookup and then product to marketing campaigns (a snowflake schema) to avoid the duplication.
I suggest you take the time to read the details of dimensional database design.
If you mean dimensional design, there is no such thing as a lookup table there; there is either a Slowly Changing Dimension (SCD), or just a Dimension. Your product lookup table could be a product lookup dimension. Your Dimension table looks imperfect, too: It does contain the element of time , but not correctly. You need - usually in this order:
a completely arbitrary integer as a surrogate, primary, key - often populated by a sequence or defined as IDENTITY
a business identifier - that could be the SKU for a product, first part of a business unique identifier
the valid-from-date, second part of a business unique identifier
the valid-to-date, '9999-12-31' for the current row, or equal to the valid-from-date of its successor
Type 1 attributes, those that don't change over time
Type 2 attributes, those that change over time and need a new row every time they change
There can be more columns: the Boolean current-indicator, and an inserted timestamp and an updated timestamp.
The fact table is populated from the source transactions, after the dimension table. For each transaction row, you join with the SCD table with the business identifier (SKU in our case), that must be equal and the transaction's timestamp, that must be greater or equal to the valid-from-date and less than the valid-to-date. You pick the surrogate key of the row found in the SCD to populate the fact table's foreign key.
This is an exemplary, minimal, customer SCD table, without the insert/change timestamps and without the current-indicator:
c_key
c_id
c_from_dt
c_to_dt
c_fname
c_lname
c_loy_lvl
c_org_id
66459
1
2022-01-25
9999-12-31
Arthur
Dent
1
1
34168
2
2022-01-25
9999-12-31
Ford
Prefect
2
2
2284
3
2021-12-25
9999-12-31
Zaphod
Beeblebrox
3
3
84768
4
2021-12-25
9999-12-31
Tricia
McMillan
4
4
80080
5
2022-01-25
9999-12-31
Gag
Halfrunt
5
5
57458
6
2022-01-25
9999-12-31
Prostetnic Vogon
Jeltz
6
6
1076
7
2022-01-25
9999-12-31
Lionel
Prosser
1
0
9782
8
2021-12-25
9999-12-31
Benji
Mouse
2
1
42655
9
2021-12-25
9999-12-31
Frankie
Mouse
3
2
57348
10
2021-09-25
2021-10-25
Wonko
The Sane
1
3
22279
10
2021-10-25
2021-11-25
Wonko
The Sane
2
3
3675
10
2021-11-25
2021-12-25
Wonko
The Sane
3
3
95534
10
2021-12-25
2022-01-25
Wonko
The Sane
4
3
69529
10
2022-01-25
9999-12-31
Wonko
The Sane
5
3
34845
11
2022-01-25
9999-12-31
Eccentrica
Gallumbitis
6
4

Make Webi report through the BICS Connection

I'm creating a little complex WebI report. I have the following mock up data
Type Type_C Amt
---- ---- --
1 # $500.00
3 # $1000.00
ABC A $10.00
ABC B $14.00
ABC C $15.00
AB A $10.00
AB B $14.00
DAB D $20.00
DAB A $10.00
DAB B $14.00
BC B $14.00
BC C $15.00
My requirement is to show report like this:
Type_Desc Amount
------- ------
A $10.00
D $20.00
E $30.00
ABC $39.00
AB $24.00
BC $29.00
DAB $44.00
I am not sure I fully understand your requirement, but try this.
I created two very simple queries based on the eFashion universe; Query 1 has City and Quantity Sold while Query 2 has State and Quantity Sold. I am not doing a union or any sort of merging here.
In my screenshot tables on the left are the results from each query. The table on the right is actually two tables. I just removed the table header (uncheck Format Table > General > Show table headers) from the second table and put it directly beneath the first table using relative position (Format Table > Layout > Relative Position). I set the column widths in each table to be the same so they will always line up. I also changed the dimension label to "New Dim".
Does that help?

Creating a summary table on BigQuery aggregating multiple values in one row

I need to create a table like below, aggregating by payment method, where I can see a summary of the combinations of services used in one day. In this case, combinations (A, B) in service_type would be the same as (B, A), and (A, A, C) would be the same as (A, C). I need to use BigQuery, and I don't know how to generate all the combinations for service_type without manually printing them out. no_of_service is the number of services grouped in service_type, and total_orders is the number of unique customer_ids which purchased that specific combination of orders in that date.
order_date no_of_service service_type total_orders order_payment
2018-04-03 1 A 5 CASH
2018-04-03 1 B 4 CASH
2018-04-03 1 C 1 CASH
2018-04-03 1 D 10 CASH
2018-04-03 1 E 5 CASH
2018-04-03 3 A,C,D 10 CREDIT
2018-04-03 3 B,D,E 5 CREDIT
2018-04-02 1 A 8 CREDIT
2018-04-02 1 C 7 CREDIT
The daily_order table which I'm querying from has the columns order_payment (CASH, CREDIT, etc), service_type (A, B, C, etc), customer_id (unique value for each customer), and order_time (e.g. 2018-04-06 15:06:26 UTC). It looks like this:
order payment service_type customer_id order_time
CASH A 58128 2018-04-06 15:06:26 UTC
CREDIT B 58256 2018-04-06 15:08:34 UTC

automating account reconciliation in excel

I have a spread sheet with three columns. the first column contains names of people. the second column contains dates. the third column contains amounts received and invoices paid for the date.
eg:
Name date amount
abc 1-jan-2012 2000 usd
abc 2-jan-2012 (1500) usd
abc 3-jan-2012 2000 usd
abc 3-jan-2012 2000 usd
abc 3-jan-2012 (3500) usd
i am trying to offset the invoices (positive values) against payments (negative value) received. if i use a lifo application then the net_value for the first entry will be 500 USD. the net value for the second entry will be zero.
can anyone suggest a way of automating this exercise. i have written an if statement but the condition does not hold when the payments are more than the invoices (a case of advances being received by the client)
thanks in advance.
this is how the final table will look like
NAME DATE AMOUNT NET VALUE
abc 1-Jan-12 (4,910.00) (4,910.00)
abc 2-Jan-12 3,674.00 (26.00)
abc 16-Jan-12 1,777.00 -
abc 17-Jan-12 (5,477.00) -
abc 22-Mar-12 258.00 258.00
abc 31-Mar-12 5,502.00 1,465.00
abc 7-May-12 3,986.00 -
abc 20-May-12 5,238.00 -
abc 23-May-12 (6,861.00) -
abc 4-Jul-12 (6,400.00) -
abc 9-Aug-12 2,238.00 2,238.00
abc 21-Aug-12 4,855.00 2,456.00
abc 26-Aug-12 (2,399.00) -
abc 9-Sep-12 3,938.00 3,938.00
sorry guys for the confusion...
Do you want to balance for each invoice? If so you are going to want to have a seperate table with all of your invoices and the total/remaining balances can then be calculated for each invoice name/id.
I would probably just use a pivot table that I would refresh on workbook_change:
At least that is how I would do it for a small scale excel reconciliation project. Hope it helps. Good Luck.
Another way you could do it - To create a list of unique invoices you can use this array formula entered with ctrl+shift+enter:
=IFERROR(INDEX($A$2:$A$20, MATCH(0, COUNTIF($E$1:E1, $A$2:$A$20), 0)),"")
Then you can simply do a SUMIF on each invoice ID:
=SUMIF($A$2:$A$22,E2,$C$2:$C$22)
assuming you have the values listed as actual currency amounts, and not the type of text in your example, you can use SUMIF to keep a running total of the account.
in D2:
=SUMIF($A$2:A2,A2,$C$2:C2)
and copy that down. it will show a running total for the status of the account, also keeps note of the items relating to the name in column A, and not for all names.
for your example, this is the result:
Name date amount Running Total
abc 1-Jan-12 $2,000 2000
abc 2-Jan-12 ($1,500) 500
abc 3-Jan-12 $2,000 2500
abc 3-Jan-12 $2,000 4500
abc 3-Jan-12 ($3,500) 1000
Reversed format for a LIFO table - you put the first formula at the last cell (D6 in the example)
=SUMIF(A6:$A$6,A6,C6:$C$6)
and copying it upwards, giving a result that looks like this:
Name date amount Running Total
abc 3-Jan-12 ($3,500) 1000
abc 3-Jan-12 $2,000 4500
abc 3-Jan-12 $2,000 2500
abc 2-Jan-12 ($1,500) 500
abc 1-Jan-12 $2,000 2000
with the total consisting of the amounts on that line and below.

How can I create a tabular report in SQL when the column names are in the database, not the query?

http://www.geocities.com/colinpriley/sql/sqlitepg09.htm has a nice technique for creating a tabular report where the column names for the table can be coded in the query but in my case, the columns should be values from the database. Say I have daily sales figures like:
Transaction Date Rep Product Amount
1 July 1 Bob A12 $10
2 July 2 Bob B24 $12
3 July 2 Ted A12 $25
...
and I want a weekly summary report that shows how much of each product each rep sold:
A12 B24
Bob $10 $12
Ted $25 $0
My column names come from the Product column. Say, any product that has a row in the specified date range should have a column in the report. But other products -- which weren't sold in that time frame -- should not have a column of all 0s. How can I do that? Bonus points if it works in SQLite.
TIA.
http://weblogs.asp.net/wallen/archive/2005/02/18/376150.aspx has a good way to extract columns