Two types of invoice numbering (2 tables with 0..1 relationship) - sql

I have table invoices - PK is year+document_type_id+number (this is current numbering, and i can't change it). So, the data is like this:
year document_type_id number
2013 351 1
2013 351 2
2013 352 1
Now, i need to develop second type of numbering - table invoices_2 - PK is year+market_id+cash_register_id+number (this is numbering for some of invoices which is proscribed by the law), and FK is invoices_year+invoices_document_type_id+invoices_number.
invoice - invoice_2 must be 1 -> 0..1 relationship.
The problem is that in invoices_2 table i may have this (which i would like to eliminate - using some PK+FK combination?):
year market_id cash_register_id number invoices_year inovices_document_type invoices_number
2013 1 1 1 2013 351 1
2013 1 1 2 2013 351 1
As you can see, the invoice 2013-351-1 using can be added more that 1 times in invoices_2 table, which must be forbidden.
See http://www.sqlfiddle.com/#!3/6b42c/1

It would be better to put the second numbering system in its own table.
Invoice
-------
year
document_type_id
number
invoice_2_fk
...
Invoice_2
---------
invoice_2 _id
year
market_id
cash_register_id
number
The invoice 2 foreign key in the Invoice table is nullable. When it's null, there is no invoice 2. When it's a valid id, then there is an invoice 2. This is a one to zero / one relationship.

Related

Getting a sum from nested many to many relations based on another relation

I have the following table structure:
calculations
- id
persons
- id
calculations_persons
- id
- calculation_id
- person_id
assets
- id
- person_id
- type (enum: normal/stock)
- stock_id (nullable)
- value
stocks
- id
- value
the relations are like so:
calculations <- Many to Many -> persons
persons | Has Many -> assets
stocks | can Belong To -> assets
Basically, I want to get the calculations table ordered by the value of all assets, but there is a catch, the calculation of assets is based on the fact that if a asset is of type stock then it's value is based on the value of the asset * the stocks value
so if I have for example a calculation with id: 1 with personA with id 1 with assetA of value 50 and assetB of value 100 (type stock) with it's stock value being 20 and person with id 2 with assetA of value 100 the result would look like this
calculation.id, (personA.id, personB.id, ...etc persons), total value
1, (1 , 2), 2150
How would the SQL look like? I'm using postgres

The IDs change in the column

I am quite a novice in programming and I kind need your help regarding SQL and an issue I notice.
I have a table:
date, ID, secondary ID, expenses
jul2020 258 0004 1000
jul2020 xxx xxxx xxx
...... .... .... .....
aug2020 258 0008 2000
aug2020 xxx xxxx xxx
aug2020 500 0004 1000
Id and secondary should be unique and always matching. But I notice that they are not. It's either correct the ID or the secondary ID. I want to sum all the D column per unique ID.
Thanks for reading and if you have any ideas would be very helpful
UPDATE: everything is numeric even ID. It's like this. As you can see we have different dates (but for the same date multiple customers). I notice that customer 258 for secondary ID 0004 during the years the ID or the secondary ID changes. And I wan to assign the same ID as the first date and the same secondary ID as the first date ( or any day just to be consistent). I want to to do this cause I want to know how many expenses each customer has during the years. There are like 50m obs.

Database Design: Order Items with quantities spanning multiple statuses

I have a requirement to create a database that holds orders with items for each order.
This would be the traditional table setup
Order Table
Id (pk)
CustomerId (fk)
1
1
Item Table
Id (pk)
OrderId (fk)
StatusId (fk)
Quantity
1
1
1
1000
ItemStatus Table
(Don't worry about how the data knows which status is first, second, third, etc)
Id (pk)
Name
Description
IsStart
IsTerminal
1
New
For newly created items
1
0
2
Materials Ordered
For indicating that raw materials are ordered
0
0
3
Pre-Fabrication
For receiving materials and gathering other resources
0
0
4
In Work
For indicating that the assembly process is underway
0
0
5
Staging
For preparing the items for shipping
0
0
6
Shipped
For indicating that items are complete and no longer in the facility
0
1
However
I have the requirement to take the above quantity of 1000 and break it down by status as it pertains to the business workflow.
My initial implementation looked something like this, but I wanted to reach out and see if there is a better design.
Modified Item Table
Id (pk)
OrderId (fk)
1
1
QuantityBreakdown Table
Id (pk)
OrderItemId (fk)
StatusId (fk)
Quantity
1
1
2
200
2
1
3
200
3
1
4
400
4
1
5
200
Edit
Here are some examples in layman terms to help clarify expected solution. All scenarios will be simplified by only having a single item. Also, the handling of ordering materials is out of scope; I just need to know that the item is waiting.
In these examples, we will be handling the creation process of a burger (item #1). In more advanced scenarios, we could add another ITEM such as fries (that would be item #2)
Example 1
A restaurant order is created with 1 burger. All materials needed for the assembly of the burger are on-hand; therefore, the burger will progress through the statuses with all quantities (New => Prep => Cooking => Packaged => Delivered).
Example 2
A restaurant order is created with 2 burgers. Only enough materials for one burger are on-hand; therefore, the item quantity needs to be split. Since we don't want the customer waiting, the first burger will progress through the statuses with a quantity of 1. While the second burger will have to wait in a new status called Pending. Then once the materials are available, the second burger may continue the workflow.
Well I cannot just comment to ask for clarifications.
But I would have each OrderItem be its on distinct item in an order with its own Status (status in OrderItem). So if in fact you had 4 sets of the same item, each would have its own status.
You could always group-by if you want the total # of each OrderItemId

Grouping attributes in SSAS

I have an account dimension where the same account can have multiple account numbers. So I setup a durable key to group these accounts. So for example
Durablekey AccountNum
1 1
1 2
1 3
2 4
3 5
The durablekey has no business meaning. I would like to have an attribute in cube that displays the accountnum but would essentially have the durablekey as the key. So if I selected accountnum 2 it would pull all the information for everything with a durablekey of 1. If I simply set the accountnum as the name and durablekey as the key I get a duplicate error during processing as expected.
Is this possible to do?
Hi there is sample data http://msftdbprodsamples.codeplex.com/releases/view/55330
You need to make Parent->Child Hierarchie, that Account key was normal integer key and other Accounts will have Parent Account key, which show to Parent Account. Parrent Account can have multiple other Accounts:
AccountKey | ParentAccount | Code
1 - 102
2 1 10255
3 1 10266
4 3 13444
5 3 13555
There is a lot of examples in internet just type - SSAS Parent - Child :
http://my.safaribooksonline.com/book/databases/microsoft-sql-server/9780735626201/creating-dimensions/creating_a_parent-child_dimension
Also you can use sample database and SSAS projects, there is everything done on working Cube...

SSAS Relationship Slowly Changing Dimension

I have this structure in my Data Warehouse
DimCustomer
-id (pk)
-Name
-Country
-datestart
-dateend
FactBalance
-idCustomer (pk) (fk [DimCustomer.id])
-idTime (pk) (fk [DimDate.DateSK])
-Balance
The Customer Dimension has multiple rows for when the country changes, and the FactBalance has multiple rows for every day over a period of time.
Here is some example data:
DimCustomer
1 JoeBloggs England 2012-01-01 2012-01-04
2 JoeBloggs France 2012-01-05 NULL
FactBalance
1 20120101 12334.0
1 20120102 12334.0
1 20120103 12334.0
1 20120104 12334.0
2 20120105 12334.0
2 20120106 12334.0
The problem I'm having is that when I view my cube in the browser and use DimCustomer.Country and FactBalance.Balance (which has aggregation set to LastNonNull) it shows two results
England 12334.0
France 12334.0
It is obviously getting the last non null for each of those customer entries in the SCD but how do I set up the dimension so that it only shows the last for the whole customer?
I would add the Business Key (I think it's Name in your example) to the Fact (I would add it as a column in the Fact table, or you could go via a SQL View).
Then I would build an alternative version of the Customer dimension that only shows the current records and uses the Business Key as it's key.
Finally I would add the new dimension to the cube and relate it to the Fact using the Business Key.