I need to create a bottom row in sql that will have regular averages for columns as well as take in the sum of two columns and calculate a weighted average for some specific columns. Is this possible?
You can compute an average one or more columns with a query like:
SELECT AVG(mycol1), AVG(mycol2) FROM mytable
You can compute a sum of multiple columns:
SELECT SUM(mycol1)+ SUM(mycol2) AS mytotal FROM mytable
You can weight the column sums:
SELECT 2*SUM(mycol1)+ 3*SUM(mycol2) AS mytotal FROM mytable
Or perform more complex operations on those SUM() or AVG() results, but think of these results as separate from the original column data, not like a "bottom row" in a spreadsheet calculation.
Related
The Yield values need to be created in the Measure column which is based on the same column values.
That is the Value of (Saleable MetIN+Saleable Thermal)/FeedIN.
I tried using CTE but the query is getting too big.
Columns Names:
YEAR,
ANO_MONTH,
ANO_WEEK,
MONTH_CODE,
WEEK_CODE,
EQMTID,
MEASURE,
VALUE
I have some counts that get periodically recorded into SQL and I'm trying to find out the difference between the start count and the final count.
Raw Data Below
This table has around 30 columns but they are the more of the same just different counts.
I want to take the min and max row for a time period based on user input from an SQL report I can filter out the data with the code below. (I can also filter it into two different tables, one with min, and one with max if that is easier.)
SELECT *
FROM #tempTable
WHERE TableIndex = (SELECT min(TableIndex) FROM #tempTable)
or TableIndex = (SELECT max(TableIndex) FROM #tempTable)
Filtered Data Below
The end goal is the difference between these two rows, I would then give that data to an SQL report to display a bar graph.
I've seen solutions but they seemed overly complex for what I'm trying to do and I would need to define each column I'm subtracting vs using *. Some of the tables have a couple hundred columns.
How about joining these together?
SELECT tt.*, ttm.*
FROM #tempTable tt
(SELECT min(TableIndex) as minti, max(TableIndx) as maxti
FROM #tempTable
) ttm
ON ttmTableIndex IN (ttm.minti, ttm.maxti);
You can then do whatever arithmetic operations you like with the values in the same row.
Personally, I would find it easier to just put the two rows into a spreadsheet and subtract the values using a formula.
PIVOT can only pivot one value into its one column.
Also PIVOT is not helpful if for examle we want to pivot and (group by) intersecting ranges of values.
So we're planning to use MODEL clause to overcome this limits.
We have to pivot into columns:
sum(), count() of data over separate quarters for last 2 years and
present each quarter's result into its own set of column;
sum(), count() of data over separate years for last 2 years and present each years's result into its own set of column;
sum(), count() of data over separate months for last 3 months and present each month's result into its own set of column.
The list of quarters / years / months in the bucketing is fixed and can be hard-coded in the MODEL clause (i.e. it'll be always to look back 2 years).
I have a working prototype doing above using three separate pivots, but it is very inefficient because each pivot has to pass our huge dataset again.
We expect MODEL might require just one pass over the dataset.
Questions:
Is it possible for MODEL clause to create new columns (ie. we group
by month and product category in a subquery, but MODEL should add
columns for the above quarter/year/month buckets)?
Can't think of a best way to define DIMENSION BY and MEASURES so we could
create new columns.. any ideas are highly appreciated.
Edit: posted the same question at https://community.oracle.com/message/13951429
You can PIVOT with multiple pivot columns and multiple result columns
with atc as
(select owner, table_name, column_name, data_type, nullable
from all_tab_columns
where table_name = 'ALL_TAB_COLUMNS')
select *
from atc
pivot
(max(column_name) as max_col_name, min(column_name) as min_col_name
for (data_type, nullable) in
(
('NUMBER','N') as dt_number_n, ('VARCHAR2','N') as dt_vc_n,
('NUMBER','Y') as dt_number_y, ('VARCHAR2','Y') as dt_vc_y
)
)
This question already has an answer here:
Access query producing results like ROW_NUMBER() in T-SQL
(1 answer)
Closed 7 years ago.
I have the following code
SELECT C_Record.BunchOfColumns, Count(*) AS Degrees
FROM C_Record
WHERE (((C_Record.[C#])=[Enter Value])) //Parameter Input from User
GROUP BY C_Record.BunchofColumns;
My Degrees column never increments, it shows 1 always no matter how many rows are returned from the query. I am suspecting that I have not implemented my GROUP BY method properly. If I understand it correctly, all columns that are selected and are not part of the aggregate function (COUNT in my case) should be put together in GROUP BY. Any help is much appreciated. Thanks in advance
Edit: What I am trying to achieve is to check how many rows have a particular value for a column, then select all other relevant columns and create a Index columns. For example if there are three rows that meet my requirement
Col1 Col2 Degrees
A X 1
B Y 2
C Z 3
and if only 2 rows meet my requirement then
Col1 Col2 Degrees
P X 1
Q Y 2
P.S - my C_Record.BunchofColumns consists of about 10 columns that I did not include for the sake of brevity.
P.P.S - If I try to skip out on any column it gives me the error You Tried to execute a query that does not include the specified expression <<column_name>> as part of an aggregate function
When you use Count() with a GROUP BY the count returned is the number of rows in each group. So to get a count greater than one you would have to have more than one row in your table that had exactly the same values. If you are selecting 10 different columns it seems likely that you have no two columns in the database that have exactly those 10 same values.
If you start with a selecting and grouping by a single column you will see count's of more than one.
That is not how GROUP BY works.
GROUP BY completely changes the meaning of your query. Each row of the result is an "aggregate grouping" of the original rows. Each aggregate grouping consists of all the rows with a particular combination of values for their GROUP BY columns. So if you GROUP BY ten columns, each grouping will consist of rows which are identical on all ten columns.
Once these groupings have been formed, you SELECT various aggregate values like count() or sum(), which provide you with information about the group as a whole. count(*) gives you the number of rows in the group, while count(column) gives you the number of rows in which column is non-NULL. You can also select any of the columns which appear in the GROUP BY clause, because those columns are identical across the whole group.
You are getting a count(*) of one because each of your groups only contains a single row. This is probably because you are grouping by ten columns, and there are no two rows which are identical for all ten columns.
If you just want a count of how many rows satisfy some query, and you don't want this aggregation at all, you write it like this:
SELECT count(*)
FROM something
WHERE something
-- no GROUP BY
;
That will form a single aggregate group of your whole query, and count the rows.
If you want something else, you will need to further explain what you're trying to do.
What happens when each column value in a table is divided with the total table row count. What function is basically performed by sql server? Can any one help?
More specifically: what is the difference between sum(column value ) / row count and column value/ row count. for e.g,
select cast(officetotal as float) /count(officeid) as value,
sum(officetotal)/ count(officeid) as average from check1
where officeid ='50009' group by officeid,officetotal
What is the operation performed on both select?
In your example both will be allways the same value because count(officeid) is allways equal to 1 because officeid is contained in the WHERE clause and officetotal is also contained in GROUP BY clause. So the example will not work because no grouping will be applied.
When you remove officetotal from the GROUP BY, you will get following message:
Column 'officetotal' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
It means that you cannot use officetotal and SUM(officetotal) in one select - because SUM is meant to work for set of values and it is pointless to SUM only one value.
It is just not possible to write it this way in SQL using GROUP BY. If you look for something like first or last value from a group, you will have to use MIN(officetotal) or MAX(officetotal) or some other approach.