How can I rearrange this table using SAS or MS-Access? - ms-access

I have a dataset on Microsoft Access and SAS of about a million option prices arranged with the following fields/columns:
DATE, COMPANY, PUT/CALL, PRICE
The PUT/CALL variable is an indicator variable that comes out as either PUT or CALL for each unique DATE-COMPANY combination.
Example with numbers:
DATE COMPANY PUT/CALL PRICE
2001/01/01 XOM PUT 10
2001/01/01 XOM CALL 12
2001/01/01 ABB PUT 11
2001/01/01 ABB CALL 13
What I need is for my table to be arranged with:
DATE, COMPANY, PUT PRICE, CALL PRICE
The above example with numbers, the output should be:
Example with numbers:
DATE COMPANY PUT PRICE CALL PRICE
2001/01/01 XOM 10 12
2001/01/01 ABB 11 13
Would someone know how I could use SAS, Microsoft Access or any other software to complete this?

The following works in Access:
SELECT
[DATE],
[COMPANY],
MAX(IIf([PUT/CALL]="PUT", [PRICE], NULL)) AS [PUT PRICE],
MAX(IIf([PUT/CALL]="CALL", [PRICE], NULL)) AS [CALL PRICE]
FROM [PRICES]
GROUP BY [DATE], [COMPANY];
Note that...
several of the column names have spaces or "funny characters" in them, and
DATE is a reserved word in Access,
...so the square brackets [] are important.

In SAS this is pretty easy.
Assuming your first table is a dataset named 'HAVE', and is sorted by date/company:
proc transpose data=have out=want suffix=price;
by date company;
id put_call;
var price;
run;
In Access (or SQL) you'd want to do a SQL query, something like this:
create table want as select date,company,
max(case when put_call='put' then price else null end) as put_price,
max(case when put_call='call' then price else null end) as call_price
from have group by date,company;
In SQL server you could probably do this with a pivot.

Related

Populate Dynamic table columns based on parameter drop down values selection in SSRS report

*Scenario:
We have one table with below columns: I need to use ONLY this table
ID
ACCOUNTID
STATUS
COMPARE
MODFIEDUSERNAME
FILENAME
FILEDESP
1
A2
IN
MATCH
Sam
abc
wew
2
A4
OUT
MATCH
Ken
xcr
wew
3
A2
IN
MISMATCH
Roy
abc
wew
4
A3
OUT
MISMATCH
Roy
xcr
wew
In the report we should have a drop down(SingleValue) for COMPARE column with values(MATCH/MISMATCH) where user can select either of one value.
If User select MATCH Option then Report should display a dropdown(MultiValue)(ReportFields Data Set) with these columns :
ID ACCOUNTID STATUS
If User select MISMATCH Option then Report should display a dropdown(MultiValue) (ReportFields Data Set) with these columns :
ID COMPARE MODFIEDUSERNAME FILENAME FILEDESP
Basically , populate column names dynamically based on MATCH and MISMATCH selection and when user clicks on View Report, Report should display respective column data.
I created the Data Set -ReportFields as below:
SELECT 1 ID, 'Id' AS ColumnName UNION
SELECT 2 ID, ACCOUNTID AS ColumnName UNION
SELECT 3 ID, 'Status' AS ColumnName UNION
SELECT 4 ID, COMPARE AS ColumnName
I created two parameters: #Compare and #ReportFields
Problem:
Need the logic to populate Dynamic columns based on user selection from first dropdown with (MATCH/MISMATCH) Values.
Your example is a little confusing but I'll show you a typical scenario which should give you enough info to solve your problem.
If we have a table of, say, fruit & veg sales and we want the user to select either fruit or veg from the drop down, then have a seconds drop down that they can chose individual items(s) from then we can do this.
The table looks like this. (I've included some sales numbers here for simplicity but these could easily be in another table).
Category
ItemName
Month
Amount
Fruit
Apple
Jan
10
Fruit
Apple
Feb
20
Fruit
Apple
Mar
30
Fruit
Orange
Jan
40
Fruit
Orange
Feb
50
Vegetable
Carrot
Jan
15
Vegetable
Peas
Jan
16
Vegetable
Cucumber
Jan
17
Vegetable
Carrot
Feb
18
The dataset for our first parameter would be
SELECT DISTINCT Category FROM myTable
This will give us 'Fruit' and 'Vegetable'
We assign this as the available values of our first parameter which we will call pCategory
Our second dataset would be
SELECT DISTINCT ItemName FROM myTable WHERE Category IN(#pCategory)
I've used IN here so that if pCategory is multi-value, it will correctly select from both categories.
We assign this seconds dataset as the available value for our seconds parameter which we will call pItems. This shoudl be a multi-value parameter. We could also assign this same dataset to the default values for this parameter so all items are selected by default.
Finally our last dataset will get some data to show in a table/matrix and will look something like this
SELECT ItemName, Month, SUM(Amount) AS SalesAmount
FROM myTable t
WHERE t.ItemName IN(#pItems)
NOTE: When you specify a parameter name in a dataset query, it must match the name of the parameter name in your report design exactly, it is case sensitive.

I want to use a group by for a nvarchar data type

My goal is to create a query that can group by 3 respective survey types and their respective values. the columns are The date is automatically updated every couple of days, the Survey type broken down into different survey types being survey a , survey b and survey c. These surveys can be either labeled sent (meaning the customer is in possession and has not completed it) or it can be labeled received(meaning the customer has completed it and the information is received.Each of the survey strings begin with the same 4 characters XXX- . This is where my use of wildcard % comes into play. the format of the survey type is XXX-YY-SENT or "XXX-YY-RECIEVED" .The YY are distinct to the specific survey type. The value column gives the total number of surveys completed by the date which is updated by the system every 2 weeks. My goal is to group the data by its survey type in order to be able to create a stacked column chart in ssrs showing the difference between sent and received surveys. (which I already have a good idea on how to execute. The issue is developing the query in order to pull the data and group it by Survey X (Group being survey x-SENT and survey x-recieved) and Survey Y (Group being survey y-SENT and survey y-recieved) and Survey Z (Group being survey z-SENT and survey z-recieved).here is example data from table SurveyInfo. This is the query i use to pull my data:
SELECT [Date] ,[SurveyType] ,[Value]
FROM SurveyInfo]
WHERE Date IN (SELECT max(Date) FROM SurveyInfo) and SurveyType like '%XXX%'
(this is where i would like to group the data by survey type)
this is the data result.
Date , SurveyType, Value
2017-06-02 08:00:02.270 XXX-AA-SENT-WITHIN-2YR 1000.0000
2017-06-02 08:00:02.270 XXX-AA-RECEIVED-WITHIN-2YR 900.0000
2017-06-02 08:00:02.270 XXX-BB-SENT-WITHIN-2YR 1200.0000
2017-06-02 08:00:02.270 XXX-BB-RECEIVED-2YR 800.0000
2017-06-02 08:00:02.270 XXX-CC-SENT-WITHIN-2YR 500.0000
2017-06-02 08:00:02.270 XXX-CC-RECEIVED-WITHIN-2YR 400.0000
That was hard to read through but I think I understand what you want. Does the below start to help you out?
if object_id('tempdb.dbo.#test') is not null drop table #test
create table #test
(
dateVal datetime,
SurveyType nvarchar(500),
SurveyValue decimal(36,4)
)
insert into #test
values('2017-06-02 08:00:02.270','XXX-AA-SENT-WITHIN-2YR',1000.0000),
('2017-06-02 08:00:02.270','XXX-AA-RECEIVED-WITHIN-2YR', 900.0000),
('2017-06-02 08:00:02.270','XXX-BB-SENT-WITHIN-2YR', 1200.0000),
('2017-06-02 08:00:02.270','XXX-BB-RECEIVED-2YR', 800.0000),
('2017-06-02 08:00:02.270','XXX-CC-SENT-WITHIN-2YR', 500.0000),
('2017-06-02 08:00:02.270','XXX-CC-RECEIVED-WITHIN-2YR', 400.0000)
SELECT
substring(SurveyType,5,2) as SurveyType,
case when SurveyType like '%SENT%' then 'SENT' else 'RECEIVED' END as SENT_RECEIVED,
sum(SurveyValue) as SumSurveyValue
FROM #test
GROUP BY
substring(SurveyType,5,2) ,
case when SurveyType like '%SENT%' then 'SENT' else 'RECEIVED' END
ORDER BY
SurveyType,
SENT_RECEIVED

MySQL Sum and Case Query

I create a ReportViewer with VB.NET connecting to a MySQL database. The data appears like below.
IdProduct Quantity TotalPrice OrderDate
0001 1 10 29/09/2014
0002 2 40 29/09/2014
0001 4 40 29/09/2014
0001 2 20 29/09/2014
0001 2 20 29/09/2014
Based on the records above, I'd like the result to appear like below
0001 0002
9 2
90 40
What is Query Sum Case the best use here? Thanks in advance.
NOTE: It's not possible for a query to "dynamically" alter the number or datatype of the columns returned, those must be specified at the time the SQL text is parsed.
To return the specified resultset with a query, you could do something like this:
SELECT SUM(IF(t.IdProduct='0001',t.Quantity,NULL)) AS `0001`
, SUM(IF(t.IdProduct='0002',t.Quantity,NULL)) AS `0002`
FROM mytable t
UNION ALL
SELECT SUM(IF(t.IdProduct='0001',t.TotalPrice,NULL)) AS `0001`
, SUM(IF(t.IdProduct='0002',t.TotalPrice,NULL)) AS `0002`
FROM mytable t
Note that the datatypes returned by the two queries will need to be compatible. This won't be a problem if Quantity and TotalPrice are both defined as integer.
Also, there's no specific guarantee that the "Quantity" row will be before the "TotalPrice" row; we observe that behavior, and it's unlikely that it will ever be different. But, to have a guarantee, we'd need an ORDER BY clause. So, including an additional discriminator column (a literal in the SELECT list of each query), that would give us something we could ORDER BY.
Note that it's not possible to have this single query dynamically create another column for IdProduct '0003'. We'd need to add that to the SELECT list of each query.
We could do this in two steps, using a query to get the list of distinct IdProduct, and then use that to dynamically create the query we need.
BUT... with all that said... we don't want to do that.
The normative pattern would be to return Quantity and TotalPrice as two separate columns, along with the IdProduct as another column. For example, the result returned by this statement:
SELECT t.IdProduct
, SUM(t.Quantity) AS `Quantity`
, SUM(t.TotalPrice) AS `TotalPrice`
FROM mytable t
GROUP BY t.IdProduct
And then the client application would be responsible for transforming that resultset into the desired display representation.
We don't want to push that job (of transforming the result into a display representation) into the SQL.
select idproduct, sum(quantity), sum(totalprice)
from your_table
group by idproduct

Graphing a single row in an SSRS Line Graph

I have a table setup with the following columns:
Product Name
SalesMonth1
SalesMonth2
SalesMonth3
SalesMonth4
SalesMonth5
An example of a row is as follows:
Bread
300
600
800
900
1000
I am trying to put this into a line graph in SSRS but am having trouble figuring out which fields go where. In my dataset, I have a field for each "SalesMonth" column. So in my dataset, I have 6 fields total including the product name. Will this work? On which axis should the product name go and the sales fields go?
I hope your dataset uses SQL.
I would "unpivot" the data by writing a 5-part UNION SELECT, e.g.
SELECT Product_Name , 1 AS Month , SalesMonth1 AS Sales
UNION ALL
SELECT Product_Name , 2 AS Month , SalesMonth2 AS Sales
UNION ALL
...
SELECT Product_Name , 5 AS Month , SalesMonth5 AS Sales
Then in the SSRS Chart definition:
Month = Category Group
Product Name = Series Group
Sales = Values
But the real real answer is that your data is probably not in a useful shape for reporting. Will there only ever be 5 months? I suspect not...

Group and Sum with dynamic column names

I need to GROUP a table by Year and SUM all the possible Types (unknown) with dynamic column names.
Sample Table:
Type|Year
a 2001
a 2001
c 2002
b 2002
c 2003
a 2003
z 2003
Sample Result:
Year: 2001, Type_a: 2
Year: 2002, Type_c: 1, Type_b: 1
Year: 2003, Type_c: 1, Type_a: 1, Type_z: 1
You could group and sum types using a query like this -
SELECT year, type, COUNT(type) FROM table_name GROUP BY year, type;
It gives another result set, but with data you want.
SELECT year,COUNT(type) from tableName GROUP BY(type)
try that one
SOL is not designed for that, the result could never have a various number of column for each line.
I think the best way to get that is to change the design of your resultset with concatenation of information by example. Or having fixed number of.column filled by null or empty values.
In the other hand you can do it programmaticaly if your language allows dynamic number of column for each row.