SQL Pivot Table with Dates - sql

I have a table with inventory quantities
Date
StockCode
QtyonHand
I need to create a pivot table which I can pull into either Excel or Crystal which includes data for the previous 25 working days.
The column headings need to be a formula so when I bring them into Crystal or Excel, I can have conditional formatting on them.
For example
Stock Code
QtyonHand -25 days
QtyonHand-24days
QtyonHand-23days
QtyonHand-22days
Does anyone have any ideas?

Use a cross-tab and save yourself some effort.
Place:
stock code in rows summary section
date in column summary section (group by day; DESC)
qtyonhand in summarized field section
Change record-selection formula to be:
{table.date} >= (DataDate - 25)

Related

How do I create a new SQL table with a percentage column which is conditional on whether information shows up in two other tables?

I have 3 tables, the table called agg((date,sname,open,high,low,close,volume)) contains daily information for every stock for x number of past years. Another table, split(date,sname,post,pre), has info for every time any stock split. Another table, div(date,sname,dividend), has info for every time a stock had a dividend. I want to create a new table, with a column that gives the percent change from closing of the previous day, to the day after, for every stock and every day listed in agg.
Here is the line I have for just the daily change, not including div and split:
create table daily
as
with prevclose as (
select date,sname,close,
lag(close) over (partition by symbol order by date) pclose
from agg
)
select a.*,
100.0*(close - pclose)/(case when pclose=0 then null else pclose end) as prcnt
from prevclose a
where pclose != 0;
I want to change this code to incorporate the change in split and dividends which is not incorporated in the agg table. I don't even need the full calculation for this, but I need help figuring out how to incorporate the condition into the new table. I only need to add in split and div info if there is split and div info for that particular date and time. I think if I could just see the query for a similar problem it would help.

Spotfire Running Balance (Cumulative Sum)

I am trying to create a running balance column in Spotfire that's supposed to look like the picture attached below. In essence,I want to calculate the cumulative total of the "amount" column row by row, and that I want it to start from 0 as the date changes.
I have tried several OVER functions:
Sum([AMOUNT]) OVER AllPrevious([Date])
Sum([AMOUNT]) OVER Intersect([CURRENCY],AllPrevious([SETTLEDATE]))
Sum([AMOUNT]) OVER Intersect([Calculation Date],AllPrevious([SETTLEDATE]))
Any help is greatly appreciated.
You were very close with your first over statement. The problem is, when you use over (AllPrevious([Date])) and you don't have 1 row for each date, then you will skip rows. So, the last row of your data would only sum it over the rows where 6/1/2017 is in the Date column. Instead, we need to apply a RowID to your data set and then sum over it. This will ensure we sum over all previous rows.
Assuming your data set is in the order you want it to be in when you bring it into SpotFire, do the following:
Insert a calculated column RowID() and name it RowID
Use this calculation: Sum([amount]) over (Intersect([Date],AllPrevious([RowID])))
This will give you the running sum you are looking for.
#scsimon- I modified your custom expression slightly to include date as requested in the question.
Modified expression:
Sum([Amt]) over (intersect(Allprevious([rowID]),[Date]))
Final output table:
#LeoL - Hope this answers your question.

PowerPivot: Aggregating 'SAMEPERIODLASTYEAR' Sales by Year, Qtr etc

I've created a new measure which uses [TotalSales] and 'SAMEPERIODLASTYEAR' to calculate the previous year's sales, see below:
=CALCULATE([TotalSales], SAMEPERIODLASTYEAR(Dates[Date]))
This all works fine if I create a pivot that displays individual dates (e.g. 01/01/2015) and then the new measure 'previous year sales' value next to it. My problem occurs when I want to change the pivot and display previous year sales by year, quarter or month - with any of these options I get no sales value.
I'm using a 'Dates' table which is linked to the Sales table.
Am I right in thinking I can re-aggregate sales in this way? I have seen an error message which says something about not been able to aggregate a non-contiguous value or date.
I've had a good look to see if anyone else has experienced the same problem, but can't see anything. Any guidance would be helpful.
Regards,
Martyn
Yes you can re-aggregate in this way. Your formula is correct would handles the changes to the aggregation level.
I would check that your 'Dates' table is marked as a date table. Ensure that the year, quarter & months are in this date table and not in your Sales table. Make sure that your date table has one record for each day between the beginning of your sales data set and the end. Check behavior in Power View if you are using Excel 2013.

Crystal Report formula to reset a count?

I want to get the total qty of items sold by year by each part number. So far I have the following formula:
if {INVOICE_DATE} >= DateTime(2012, 01, 01) and
{INVOICE_DATE} <= DateTime(2012, 12, 31)
THEN Sum ({INVOICE_QTY)
Now, that formula just sums all the parts sold in that year. What I need is for it to reset the sum as it count through each invoice and by each part. How can I do this?
Your formula is not doing what you think it is. For each row in your report (I'll assume that this is by an Invoice_ID or something along those lines) this will simply display the grand total sum of {INVOICE.QTY} for all invoices in the report and will not change from row to row.
To get something that is like your image, you need to create a Cross-Tab object and configure it such that its rows are based on the Product_ID, the columns are based on Invoice_Date and set to be printed "for each year", and set the "Summarized Fields" to be the sum of {INVOICE.QTY}.
I guess you are not grouoing the report hence you are getting the grand total using that formula. What you can do is:
First create a group by Year and then by Part.
In details place your fields that need to be summed and supress the detail part.
Take sum in group footer 2 (For parts summary) and Group footer 1 (For year summary).

Crystal Report in VB.net

Im creating a report using crystal report in vb.net.
The report contained a crosstab which I have 3 data:
1. Dealer - row field
2. Month - column
3. Quantity Sales - summarize field
How can I arrange this by ascending order based on the
Quantity Sales - summarize field?
thanks
Depending on how you're working with it, you can adjust the input to order the data ascending.
SELECT customer, sum(amountdue) AS total FROM invoices
GROUP BY customer
ORDER BY total ASC
If you're doing in a way that you can't change that information, could you provide a little more insight?
Note that other Business Objects products order on the total of a summary field when sorting a cross tab by it's values. I forget how CR does it exactly.