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

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

Related

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

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)

Is there a function in SQL that automatically generates more rows by month?

I've got a large database that's got all our transactions and shipping costs from them, here's a simplified version:
Source Table
Date
ROUTE
Cost
01/20/21
USA to UK
$40
01/01/21
USA to UK
$40
01/10/21
USA to UK
$40
12/20/20
USA to UK
$30
11/20/20
USA to UK
$20
11/20/20
USA to UK
$20
And I want to see the average cost by month before so it would look like:
Route
Nov 2020
Dec 2020
Jan 2020
USA to UK
$20
$30
$40
How do I write a code that I can repeat for when say April comes around and I have to refresh this table and I don't need to create new columns for Feb, March, etc.?
Here is a possible way of doing it by using PIVOT in Snowflake: https://docs.snowflake.com/en/sql-reference/constructs/pivot.html
Let's say "monthname" is a column you extracted out of your "Date"-column, probably this helps:
select * from yourTable
pivot(sum(cost) for monthname in ('January', 'February', 'March', 'April'))
order by route;
The values of your monthname-column should match the one in the brackets.
As this is a more static solution, you still have to adjust the code every month. Here probably writing a stored procedure is helping: https://docs.snowflake.com/en/sql-reference/stored-procedures.html

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?

Normalize monthly payments

First, sorry for my bad English. I'm trying to normalize a table in a pension system where subscribers are paid monthly. I need to know who has been paid and who has not and how much they've been paid. I believe I'm using SQL Server. Here's an example:
id_subscriber id_receipt year month pay_value payment type_pay
12 1 2016 January 100 80 1
13 1 2016 January 100 100 1
14 1 2016 January 100 100 1
12 2 2016 February 100 100 2
13 2 2016 February 100 80 1
But I'm not happy repeating the year and the month for every single subscriber. It doesn't seem right. Is there a better way to store this data?
EDIT:
The case is as follows: this company has many subscribers who must pay monthly and payment can be in various ways. They produce a single receipt for many customers, and each customer that receipt may be paying one or more installments.
These are my other tables:
tbl_subscriber
id_suscriber(PK) first_name last_name address tel_1 tel_2
12 Juan Perez xxx xxx xxx
13 Pedro Lainez xxx xxx xxx
14 Maria Lopez xxx xxx xxx
tbl_receipt
id_receipt(PK) value elaboration_date deposit_date
1 1,000.00 2015-09-16 2015-09-20
2 890.00 2015-12-01 2015-12-18
tbl_type_paym
id type description
1 bank xxxx
2 ventanilla xxx
This basically seems fine. You could split dates out into a separate table and reference that, but that strikes me as a kind of silly way to do it. I would recommend storing the month as an integer instead of a varchar column though. Besides not storing the same string over and over you can more reasonably do comparisons.
You could also use date values, although that might not be worth the trouble when you don't want greater granularity than the month.

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.