after a lot of join and group by i have come to the totals that i wanted...
to keep things simples i will reduce the complexity of the table..
lets say that i have this table that give me the totals of models per year/mont
YearMonth| Totals|model
------------------------
2015-05 | 70 |AA
2015-05 | 50 |BB
2015-06 | 30 |AA
2015-06 | 10 |BB
------------------
201x-yy | 33 |AA
201x-yy | 90 |BB
i have to create a specific (non convencional)graphic in excel with this data
but the only way is to transform the table to something
where the columns are dynamic and the rows fix... something like this
Model|2015-05|2015-06|----|201X-yy
------------------------------------
AA | 70 | 30 |--- |33
BB | 50 | 10 |----|90
is it possible to create with a query? or do i have to do it use some complex store procedure to first create a temp table and than insert data into it.
Me recommendation in this case is to return that data into Excel, and use Excel pivot table to get your output. You can tell Excel to return a linked quiet directly to a pivot table.
Related
I have Quaterly basis Data and Data keeps Growing dynamically as Quater Grows-
qtr dimvalue percentage
FY2019-Q1 XYZ 15
FY2019-Q1 ABC 80
FY2019-Q1 PPP 5
FY2019-Q2 XYZ 10
FY2019-Q2 ABC 70
FY2019-Q2 PPP 20
When the Number of Quarters are less i am manually editing the query every time and trying the query as below to transpose it-
SELECT dim_value,SUM(Quater_1) as Quater_1,SUM(Quater_2) as Quater_2 from
(
SELECT dim_value,
CASE WHEN qtr='FY2019-Q1' THEN percentage END AS Quater_1,
CASE WHEN qtr='FY2019-Q2' THEN percentage END AS Quater_2 FROM
( select * from schema.table where qtr in ('FY2019-Q1','FY2019-Q2'))t2 order by dim_value
)t1 group by dim_value;
dimvalue Quater_1 Quater_2
XYZ 15 10
ABC 80 70
PPP 5 20
But my Query is how can i active this in a dynamic way and more robust way to transpose rows into columns and keeping in mind the growing quaters and also have proper Quaterwise column names as the Quater grows.
Altogether i am looking for how can perform this using a more dynamic Query be it using Hive or Spark-SQL or if any suggestions to perform it?
Thanks for the Help
You could easily do such pivot using Dataset API if that's doable for you.
spark.table("schema.table").groupBy("dimvalue").pivot("qtr").sum("percentage").show
+--------+---------+---------+
|dimvalue|FY2019-Q1|FY2019-Q2|
+--------+---------+---------+
| PPP| 5| 20|
| XYZ| 15| 10|
| ABC| 80| 70|
+--------+---------+---------+
With SQL the only way is to build it dynamically.
I'm wracking my brain trying to figure this out. I have a dataset / table that looks like this:
ID | Person1 | Person2 | Person3 | EffortPerPerson
01 | Bob | Ann | Frank | 2
02 | Frank | Bob | Joe | 3
03 | Ann | Joe | Beth | 1
I'm trying add up "Effort" for each person. For example, Bob is 2+3, Joe is 3+1, etc. My goal is to produce a PowerBI scatter plot showing total Effort for each person.
In a perfect world, the query shouldn't care how many "Person" fields there are. It should just count up the Effort value for every row that the individual's name appears.
I thought GROUP BY would work, but obviously that's only for one column, and I can't wrap my head around how to make nested queries work here.
Any one have any ideas? Thanks in advance!
As Nick suggested, you should go with the Unpivot transformation. Go to Edit Queries and select Transform tab:
Select columns you want to transform in rows, open dropdown menu under Unpivot Columns and select "Unpivot Only Selected Columns":
And that's it! Power BI will aggregate values for you:
I get stuck generating a SQL query. I have a Table in a Firebird DB like the following one:
ID | PROCESS | STEP | TIME
654 | 1 | 1 | 09:08:40
655 | 1 | 2 | 09:09:32
656 | 1 | 3 | 09:10:04
...
670 | 2 | 15 | 09:30:05
671 | 2 | 16 | 09:31:00
and so on.
I need the subtotals for each process group (It's about 7 of these). The table has the "time"-type for the TIME column.I have been trying it with DATEDIFF, but it doesn't work.
You need to use SUM
This question has been answered here.
How to sum up time field in SQL Server
and here.
SUM total time in SQL Server
For more specific Firebird documentation. Read up on the sum function here.
Sum() - Firebird Official Documentation
I think you should use "GROUP BY" to get max time and min time, and to use them in the datediff function. Something like that:
select process, datediff(second, max(time), min(time)) as nb_seconds
from your_table
group by process;
I have a table with the times athletes of a sport club take to run a lap around the field . Each athlete has several entries in that table for each time they run and and for statistics purposed I need to gather some statistics regarding the time they take.
I already have the basic statistics like average time, median time, etc.... However I have no idea how to exactly do the bottom and top quartiles.
I see some examples for quartiles of a table if you just want the statistics of the whole table (in this case the whole club) but I have no idea how to make them for sub groups like distinct athletes of a table, could anyone give me point me on the right direction/give me an example?
The relevant data is in a very simple structure like this (there are more columns but in this case they don't matter)
LAP_ID | ATHLETE| TIME |
1 | Ath_X | 120 |
2 | Ath_Y | 160 |
3 | Ath_X | 90 |
4 | Ath_X | 80 |
5 | Ath_Z | 113 |
6 | Ath_X | 115 |
EDIT:There seems to be some misunderstanding, by Quartile I mean the 1st and 3rd Quartile, that is the place where it splits off the lowest 25% of data from the highest 75% and the place where it splits off the highest 25% of data from the lowest 75%.
My table called lets say "table1" looks as follows:
Area | Owner | Numberid | Average
1200 | Fed_G | 998 | 1400
1220 | Priv | 1001 | 1600
1220 | Local_G | 1001 | 1430
1220 | Prov_G | 1001 | 1560
1220 | Priv | 1674 | 1845
1450 | Prov_G | 1874 | 1982
Ideally what I would like to do is sum a few rows in the average column if:
1. they have the same numberid (lets say three rows all had a numberid=1000 then they would have their average added together)
2.Area=1220
Then take that and append it to the existing table, while setting the Owner field equal to "ALL".
I just started working with Access so I'm not really sure how to do this, this is my horrible attempt:
SELECT ind.Area, ind.Owner, ind.numberid,
(SELECT SUM([Average]) FROM [table1]
WHERE [numberid]=ind.numberid) AS Average
FROM [table1] AS ind
WHERE (((ind.numberid)>="1000" And (ind.numberid)<"10000") AND ((ind.Area)="1220"))
Can anyone guide me through what I should do? I'm not used to sql syntax.
I tried to use "ind" as a variable to compare to.
So far it gives me column names but no output.
I'm still unsure whether I understand what you want. So I'll offer you this query and let you tell us if the result is what you want or how it differs from what you want.
SELECT
t.Area,
'ALL' AS Owner,
t.Numberid,
Sum(t.Average) AS SumOfAverage
FROM table1 AS t
GROUP BY t.Area, 'ALL', t.Numberid;
Using your sample data, that query gives me this result set.
Area Owner Numberid SumOfAverage
1200 ALL 998 1400
1220 ALL 1001 4590
1220 ALL 1674 1845
1450 ALL 1874 1982
Probably I would be able to (maybe) give you a better answer if you improve the wording of your question.
However, to <> you need to select average column and group by numberid and Area columns. Since the Owner field is <> I guess it doesn't matter in this query that I'm writing:
SELECT numberid, area, SUM(average)
FROM table1
WHERE owner = 'your-desired-owner-equal-to-all'
GROUP BY numberid, area
ORDER BY numberid, area