Extended (?) SQL connections?
I am a bit lost on how to explain this, so I will try to give an example of some tables (+ data) and then the result after which I will be (all my table columns are NOT USED):
Table: Customers
Id int primary key
Name varchar(250)
Table: Stats (Date, CustomerId is the primary key)
Date (date)
CustomerId (int) - foreign key to Customers table
Earning (money)
Table: Bonus
Id int primary key
CustomerId int - foreign key to Customers table
Date date
Amount money
Table: Payments
Id int primary key
DateFrom date,
DateTo date,
CustomerId bigint - foreign key to Customers table
Table: CampaignPayment
Id int primary key
PaymentId int - foreign key to payments table
Quantity int
UnitPrice money
Table: BonusPayment
Id int primary key
PaymentId int - foreign key to payments table
Amount money
The idea here is that every time a customer does something that should make them money, it goes into the statistics table. Customers can also receive various types of bonuses, which are included in the bonus table. Every time so often, I need to create an invoice for customers (Payment table) that will list material from the statistics table + bonus table for a specified period of time and which will generate an invoice (i.e. the payment table determines who the account is, for what period and the table campaignpayment and bonuspayment indicate what is paid and why).
Now - I need to be able to concatenate all of these tables to get the output from the following:
CustomerId | CustomerName | PaymentId | Amount | BonusAmount | DateFrom | DateTo
The amount is the total amount (SUM (Quantity * UnitPrice)) from the CampaignPayment table, and BonusAmount is the total amount (SUM (Amount)) from the BonusPayment table. DateFrom and DateTo from the Payments table.
The trick is that for every client for a specific month, when every day of that month is not spreading, I need a row with the following data:
CustomerId | CustomerName | NULL | (Stats.Earning - Amount Earned from possible payments within the month) | (Bonus.Amount - Amount Earned possible bonuses that is in payments within the month) | First day of month | Last day of month
I may need more complex logic as to how to calculate the amount and number of bonuses in these "blank" lines, but for now I start with that.
How can i do this? I know how to get the "start" bit, but how can I add these "blank" lines? Hopefully I'll explain the problem in some detail and that you can see the idea here - if you don't tell me and I'll try to explain further.
The database is MS SQL Server 2008.
EDIT: Also alternatively, a "blank" line for each customer per month is also an acceptable solution.
a source to share
I would make a helper table with "every day of this month" to make it easier to determine if "if every day of the month is not covered" (somewhat ambiguous specification, but the aux table should help if you mean "no day is covered "or" some days are not covered "and whether a day is considered" closed "if it has either a bonus or stats, or if it should be considered" covered "- these ambiguities are due to why I am not even going to try to sketch the SQL using this aux table ;-). Then I would UNION
"empty lines" into the "start bit", which you already know how to do - seems like a perfect task for UNION!)
a source to share