Loop through columns in Access SQL - sql

In my Access database, I have a query (call it qrySource) that adds a new column every month. When you run the query in March, the datasheet looks like this:
ID January February March
1 20% 50% 100%
2 10% 60% 95%%
3 25% 60% 80%
4 20% 30% 75%
5 15% 50% 100%
Next month it would have an April column, etc.
I have another query that pulls from this one:
SELECT qrySource.ID, qrySource.January AS Jan, qrySource.February AS Feb, qrySource.March AS Mar
FROM qrySource
As it stands, I have to update this query every month to pull in the new monthly column from qrySource.
(I can't just use SELECT *, because the real query is more complex than this. For one thing, it pulls fields from several "source" queries.)
Is there a way to make this query "loop" through all the columns in qrySource without knowing exactly how many there are?

Related

Postgresql- divide sum by total already in table

I have a table with several time intervals as rows with one "total" row. I have four columns; car, bus, truck, and total, that refer to the number of vehicles leaving a warehouse at each time interval by category and the total number of vehicles at each time interval. My table looks like this:
time car truck bus total
12-6am 10 15 10 35
7am-12pm 8 12 8 28
Total 18 27 18 63
I want to create a percent total row that takes the total value in each row (35 and 28) and divides it by the maximum value in the total row (63).
How do I do this?
If you look at the schema of your table, it doesn't make sense to have an extra row in it, but only an extra column.
However, even that is a bad idea. A database is not a spreadsheet, where you have largely free-form data. It's a collection of tables. Total rows should be calculated with SELECT statements, not make some attempt to have them in the table. Unlike a spreadsheet, Postgres won't auto-update that as rows are added and deleted. (Note: Yes, sometimes you need to materialize this summary stuff for efficiency, but that's the advanced course.)

Expression for - Row by row variance on grouped datasets

I am having troubles with creating an expression in SSRS.
I'd like to calculate the difference between two figures. The columns are in separate datasets and are grouped. They also show a total at the end of each group.
Eg
Dataset 1 Dataset 2
Month Workshops which Ran Month Workshops which Ran Variance
Apr 40 Apr 30 10
May 50 May 40 10
Jun 45 Jun 35 10
Q1 Total 135 Q1 Total 105 30
The quarters then carry on but, you get the picture.
Is there a way to make an expression to calculate the variance column even though the data is grouped and in different datasets?
Any help would really be appreciated :)
Will
If we assume:
There could be voids in either data set, we could use a full outer join and coalesce.
You want the absolute difference for variance (no negatives)
You want to display the month and workshops which ran in all cases.
Neither dataset would span more than 1 year's period. (if they did we would need the aggregate datasets to contain year along with month and include it on the join)
The Q1 total value (or others) exists in both data sets and is spelled the same.
.
SELECT DS1.Month as [DS1 Month]
, DS1.[Workshops which Ran] as [DS1 Workshops which Ran]
, DS2.Month as [DS2 Month]
, DS2.[Workshops which Ran] as [DS2 Workshops which Ran]
, abs(coalesce(DS1.[Workshops which Ran],0) - coalesce(DS2.[Workshops which Ran],0)) as [Variance]
FROM Dataset1 DS1
FULL OUTER JOIN Dataset2 DS2
on DS1.Month = DS2.Month
The best way is to create a dataset with all your data in one place. If you can't do this for whatever reason, and the data in the datasets is more details than the aggregated data you are showing in your example, then check this post.
http://salvoz.com/blog/2013/05/27/sum-result-of-ssrs-lookupset-function/

Rank in powerpivot

In Powerpivot, I have a problem in ranking in Table 1, based on Sales and Year. I want to have the result like that:
Year Store Sales **Rank**
2013 A 200 3
2013 B 250 2
2013 C 300 1
2014 A 350 2
2014 B 300 3
2014 C 400 1
Which rank function could I use to have this rank result?
Thanks in advance.
Tran,
Probably the smartest way to go is to use the 'X' functions. They can be a bit tricky and non intuitive, yet are extremely powerful.
First, create a simple measure to calculate the total sales:
TotalSales:=SUM(Stores[Sales])
Then, use this formula below to calculate the rank (per store per year):
Rank:=RANKX(ALL(Stores[Store]), [TotalSales])
That should do what you are looking for. Once those two measures are ready, create a new powerpivot table, dray Year and Store onto rows pane and add required values.
ALL function overwrites the applied rows filter and thus allows to calculate rank per year.
The result should look like this:
Hope this helps.

identifying trends and classifying using sql

i have a table xyz, with three columns rcvr_id,mth_id and tpv. rcvr_id is an id given to a customer, mth_id is a column which stores the month number( mth_id is calculated as (2012-1900) * 12 + 1,2,3.. ( depending on the month). So for example Dec 2011 will have month_id of 1344, Jan 2012 1345 etc. Tpv is a variable which shows the customers transaction amount.
Example table
rcvr_id mth_id tpv
1 1344 23
2 1344 27
3 1344 54
1 1345 98
3 1345 102
.
.
.
so on
P.S if a customer does not have a transaction in a given month, his row for that month wont exist.
Now, the question. Based on transactions for the months 1327 to 1350, i need to classify a customer as steady or sporadic.
Here is a description.
The above image is for 1 customer. i have millions of customers.
How do i go about it? I have no clue how to identify trends in sql .. or rather how to do it the best way possible.
ALSO i am working on teradata.
Ok i have found out how to get standard deviation. Now the important question is : How do i set a standard deviation limit on my own? i just cant randomly say "if standard dev is above 40% he is sporadic else steady". I thought of calculating average of standard deviation for all customers and if it is above that then he is sporadic else steady. But i feel there could be a better logic
I would suggest the STDDEV_POP function - a higher value indicates a greater variation in values.
select
rcvr_id, STDDEV_POP(tpv)
from yourtable
group by rcvr_id
STDDEV_POP is the function for Standard Deviation
If this doesn't differentiate enough, you may need to look at regression functions and variance.

SQL YTD for previous years and this year

Wondering if anyone can help with the code for this.
I want to query the data and get 2 entries, one for YTD previous year and one for this year YTD.
Only way I know how to do this is as 2 separate queries with where clauses.. I would prefer to not have to run the query twice.
One column called DatePeriod and populated with 2011 YTD and 2012YTD, would be even better if I could get it to do 2011YTD, 2012YTD, 2011Total, 2012Total... though guessing this is 4 queries.
Thanks
EDIT:
In response to help clear a few things up:
This is being coded in MS SQL.
The data looks like so: (very basic example)
Date | Call_Volume
1/1/2012 | 4
What I would like is to have the Call_Volume summed up, I have queries that group it by week, and others that do it by month. I could pull all the dailies in and do this in Excel but the table has millions of rows so always best to reduce the size of my output.
I currently group by Week/Month and Year and union all so its 1 output. But that means I have 3 queries accessing the same table, large pain, very slow not efficient and that is fine but now I also need a YTD so its either 1 more query or if I could find a way to add it to the yearly query that would ideal:
So
DatePeriod | Sum_Calls
2011 Total | 40
2011 YTD | 12
2012 Total | 45
2012 YTD | 15
Hope this makes any sense.
SQL is built to do operations on rows, not columns (you select columns, of course, but aggregate operations are all on rows).
The most standard approach to this is something like:
SELECT SUM(your_table.sales), YEAR(your_table.sale_date)
FROM your_table
GROUP BY YEAR(your_table.sale_date)
Now you'll get one row for each year on record, with no limit to how many years you can process. If you're already grouping by another field, that's fine; you'll then get one row for each year in each of those groups.
Your program can then iterate over the rows and organize/render them however you like.
If you absolutely, positively must have columns instead, you'll be stuck with something like this:
SELECT SUM(IF(YEAR(date) = 2011, sales, 0)) AS total_2011,
SUM(IF(YEAR(date) = 2012, total_2012, 0)) AS total_2012
FROM your_table
If you're building the query programmatically you can add as many of those column criteria as you need, but I wouldn't count on this running very efficiently.
(These examples are written with some MySQL-specific functions. Corresponding functions exist for other engines but the syntax would be a little different.)