I'm currently working on an Access database, and I would like to make a query that can fuse multiple rows into one, but keeping all the entries separated in differents columns.
The table i'm making my query on is called "Monthly", is used for monthly reports of activity and is made this way:
project
date
turnover
margin
0001
01/01/2021
10000
20%
0001
01/02/2021
10500
15%
0002
01/01/2021
8500
25%
I would like to know if there is a way to transform it into this:
project
date
turnover
margin
date
turnover
margin
0001
01/01/2021
10000
20%
01/02/2021
10500
15%
0002
01/01/2021
8500
25%
01/02/2021
I first thought of using GROUP BY, but it's clearly not adapted.
The objective would be to format it on a SQL request, not to modify the structure/ how the data is stored.
Thanks in advance for anyone who answers.
If it doesn't already exist, add an autonumber field to table.
Consider:
Query1:
SELECT ID, project, [date] AS Data, "D" AS Cat FROM ProjectData
UNION SELECT ID, project, turnover, "T" FROM ProjectData
UNION SELECT ID, project, margin, "M" FROM ProjectData;
Query2:
TRANSFORM First(Query1.Data) AS FirstOfData
SELECT Query1.project
FROM Query1
GROUP BY Query1.project
PIVOT DCount("*","ProjectData","Project='" & [Project] & "' AND ID<" & [ID])+1 & [Cat];
Presume this will be a multi-year db so probably need some filter criteria to limit selection to one year.
Similar example Pivot Query in MS Access
For more about CROSSTAB queries, review http://allenbrowne.com/ser-67.html
Or emulate CROSSTAB (example for 2 months):
SELECT ProjectData.project,
Max(IIf(Month([date])=1,[Date],Null)) AS Jan, Max(IIf(Month([date])=1,[Margin],Null)) AS JanMargin, Max(IIf(Month([date])=1,[turnover],Null)) AS JanTurnover,
Max(IIf(Month([date])=2,[Date],Null)) AS Feb, Max(IIf(Month([date])=2,[Margin],Null)) AS FebMargin, Max(IIf(Month([date])=2,[turnover],Null)) AS FebTurnover
FROM ProjectData
GROUP BY ProjectData.project;
Should not use reserved words as names. Date is a reserved word.
Related
I am using Oracle SQL Developer.
I got the following result from several joined input-tables:
Working station number
Produced tools
Date
1
150
01.01.2020
2
100
01.01.2020
1
50
01.02.2020
3
70
15.01.2020
1
120
08.02.2020
4
130
08.02.2020
The date in the last column is at TO_Date format YYYY/MM/DD
My goal is to visualize the amount of produced tools per working station for each month and year.
Expected output-table:
Year/Month
Working Station Number
Sum of all WS
2022/01
1
150
2022/02
2
80
2022/03
3
100
2022/04
1
120
I want this output format for all WS per Month and year. Moreover I would like to add the sum of the WS per month and year
The data does also include the amount of tools per WS for 2021. The table should therefore aso show the amount of produced tools for more years.
To achieve this format I need to 1.: sum up the tools per ws and 2. sum up the tools per month per working station number and 3. convert the lines to columns.
I would begin to Sum up the Produced tools per month and afterwards calculate it based on the ws.
Afterwards I would use the pivot-Function in order to turn the lines (working stations) into columns.
My approach would be the following:
(SELECT Working station number, Amount of produced tools, Date from SourceTable
from
(SELECT Working station number, Amount of produced tools, Date from SourceTable from SourceTable) as SourceTable
Pivot
(Max(Amount of produced tools)
For Working Station in ([1] [2] [3] [4])
) as PIVOT_Table
Unfortunately I don't know how to get the 3 steps together.
I am happy about any comments!
Hi I am new to tables can anyone help me how to calculate the difference between two columns in table.
The table looks like this
Area 2016 sales 2017 sales difference
-------- ------------- ------------- ----------
India 5000 4500 -500
I am not able to calculate the difference like this in tableau while in excel is too easy
Can anyone help me
Put dimension for years on the columns, put measure on Text (marks tab)
Right click green meausure pile -> Add table calculation
Select Table Calculation "Difference from", "Table across"
Confirm
Now in 2017 column you'll see the difference between 2017 and 2016 and null in column 2016
To return sales redesign your worksheet like this (you can save sales with triangle calculation by dragging it on measures tab)
worksheet design
Problem:I am trying to make a Inventory Management Database on Microsoft Access 2010, and since i needed records date wise my Table looks like follows
SKU 2016-03-16 2016-03-17 2016-03-18 ... Total
AAA 10 -5 15 ... 20
BBB 05 05 25 ... 35
CCC 06 -5 24 ... 25
This way i wanted to add records for each day, but i am unable to make a "Total" column which will total all the columns (Sum(Columns*))
I am aware of sum and groupby but that works across multiple rows i am looking for something which can do the same for column
Expected Solution: A way either by Sql Query or VBA to total all the columns for SKU AAA,BBB,CCC, if not possible to total in the same table then i am open to total the columns in a new table.
Such a table is normally result of pivoting summary data. Real data would look like:
SKU, Date, Quantity
...
Then probably you would want to create a PIVOT table using SKU for rows, Date for Columns and Quantity for data (default operation is SUM). It would have the row totals by default.
It may have been named crosstab, cross tab .. or so in Access 2010.
(you may want to do the pivoting in Excel)
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.
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.)