Multiple row Sums into a Total Column - sql

I have a temp table populated in a sproc that is similar to this:
Company Col1 Col2 Col3 Total
Company1 4 3 2 9
Company2 1 0 3 4
Total ? ? ? ?
Is there a slick way to get the bottom total column populated with SUM of each row in one shot without having to do each column individually using sub-queries?

select sum(col1), sum(col2), sum(col3), sum(col1+col2+col3)
FROM CompanyTable

If the DBMS you use is MS SQL than be aware of how to reference the temporary table:
SELECT SUM(Col1) as TotalOfCol1,...
FROM #Company

If you are using MySQL, you can get the whole table in one query using the WITH ROLLUP feature:
SELECT Company, SUM(Col1), SUM(Col2), SUM(Col3), SUM(Col1 + Col2 + Col3)
FROM Table1
GROUP BY Company
WITH ROLLUP
Results:
'Company1' 4 3 2 9
'Company2' 1 0 3 4
'' 5 3 5 13 <--- This extra row is the column totals

Related

Counting relations between two columns in SQL server

Good morning, I have two columns in sql studio and I need to count the relations between the elements one column and the other one. The problem is it seems only is counting in 'one direction' and I want to know both. Maybe it is easier if I show you in a simple example. I am using SQL server.
This is the original table:
Col1 Col2
3 1
3 2
3 2
4 4
4 5
4 6
3 2
2 3
2 3
And if I do the following (based on count(concat)), it gives the following results.
select Col1, Col2, count(concat(Col1, Col2)) as weight
FROM test1
group by Col1, Col2
Col1 Col2 weight
3 1 1
3 2 3
2 3 2
4 4 1
4 5 1
4 6 1
But what I would like is also counts the relations between the two elements, independently if this is in one column or other. So, the number of relations between 3-2 (or 2-3) would be 5. It is possible to do that?
3 2 5
Any suggestion would be very welcome! Thanks in advance!
Most databases support least() and greatest(). This allows you to do:
select least(col1, col2) as col1, greatest(col1, col2) as col2,
count(*) as cnt
from t
group by least(col1, col2), greatest(col1, col2);
If your database does not support these handy functions, then you can implement similar logic using case expressions.

To find total number of rows

I have a table like this
Table1
=======
A B
8 5
2 9
null 4
2 5
How to find total number of rows from table1,note if any column value is null in a row then that row should be considered as 2 rows?
I have tried with count(*)*2 and nvl function it doesn't work
Try this
SELECT SUM(CASE WHEN A IS NULL OR B IS NULL THEN 2 ELSE 1 END) AS CountVal
FROM TABLE1
Fiddle Demo
O/P:
COUNTVAL
--------
5
COUNT() is rowbased.. you can tweak it using SUM() instead..
select sum(NVL2(a,NVL2(b,1,2),2)) FROM TABLE1
CASE as suggested by #Vignesh is the simplest and more readable !!
COUNT() can also done like this.. But NOT a optimal solution at all!
SELECT COUNT(1) FROM
(
SELECT NVL(a,NVL(b,1)) FROM TABLEA
UNION ALL
SELECT NVL(a,NVL(b,1)) FROM TABLEA
WHERE A OR B iS NULL
)

SQL query to print mirror labels

I want to print labels in words as returned by a SQL query such as follow.
1 2 3
4 5 6
When I want to print the reverse of those labels, I have to print them as follow
3 2 1
6 5 4
In my real case, I have 5 colums by 2 rows, how can I formulate my query so that my records are ordered like the second one.
The normal ordering is handled by word, so my query is like
SELECT * FROM Products ORDER BY Products.id
I'm using MS Access =(
EDIT :
Just to make it clear
I'd like my records to be ordered such as
3 2 1 6 5 4 9 8 7 12 11 10
EDIT2 :
my table looks like this
ID ProductName
1 Product1
2 Product2
3 Product3
n Product[n]
I want the ids to be returned as I mentioned above
SELECT * FROM Products ORDER BY Products.id desc
Alternately if your query at the moment is really giving you this:
select col1, col2, col3 from products order by products.id;
why not use
select col3, col2, col1 from products order by products.id;

TSQL query to retrieve flagged/total rows

I'm struggling to find a solution to the following problem.
Assume, one has a table like this
COL1 FLAG
aaa 1
aaa 0
aaa 1
bbb 0
I need to write a query to get the following output:
COL1_VALUE FLAGGED TOTAL
aaa 2 3
bbb 0 1
where FLAGGED column contains the total count of the 'aaa' row values for which FLAG=1, and TOTAL column is the total number of rows containing 'aaa', in other words find how many rows containing 'aaa' are flagged in relation to total number of rows containing 'aaa'. Is it possible with a single query? (i.e. without using temp tables etc.)
(MSSQL2008)
SELECT COL1 AS COL1_VALUE,
COUNT(CASE WHEN FLAG = 1 THEN 1 END) AS FLAGGED,
COUNT(*) AS TOTAL
FROM YourTable
GROUP BY COL1
SELECT COL1, SUM(FLAG) AS FLAGGED, Count(*) AS TOTAL from tbl GROUP BY COL1
SELECT Tab.COL1 AS COL1_VALUE,
SUM(CASE WHEN Tab.FLAG = 1 THEN 1 ELSE 0 END) AS FLAGGED,
COUNT(*) AS TOTAL
FROM Tab
GROUP BY Tab.COL1

Using ROLLUP in query... Small question

It is a "table_one" in oracle database:
COL1 COL2
-----------------
2 4
2 1
13 14
13 15
I have this query:
SELECT col1, sum(col2) FROM table_one GROUP BY ROLLUP(col1, col2);
After query execute I have:
------------
2 1
2 4
2 5
13 14
13 15
13 29
34
but i need in another way, like this:
------------
2 1
2 4
2 5
13 14
13 15
13 29
without summary of all columns
How I can change my query..... ?
There are two ways to solve this problem. The first is to use grouping sets to define exactly what summary groups you want created.
In this case, you can define to group on (col1) and (col1, col2) usig the following query:
select col1, sum(col2)
from table_one
group by grouping sets ((col1), (col1, col2))
Otherwise, you can group by col1 and rollup on col2 using the following query:
select col1, sum(col2)
from table_one
group by col1, rollup(col2)
Both of these queries should produce the output you require.
Use your current query as a sub query. In the outer query, use a WHERE clause that eliminates the NULL value in Col1
SELECT *
FROM
(
-- Your Rollup Query goes here.
SELECT * FROM MyTable
)
WHERE COL1 IS NOT NULL
A better solution was presented in the comments to eliminate NULL values in the source column(s)
Use the GROUPING function to determine if it is a a sub-total.
http://www.remote-dba.net/pl_sql/t_sql_grouping.htm