SELECT ALL with SUM(Col) - sql

Is there a way to Select all and also get the sum of a column all in the same query?
SELECT *, SUM(Colname) FROM `Table`...
Thank you.

This can easily be done using a window function:
select t.*,
sum(some_value) over () as total_sum
from some_table as t;
This is standard SQL and works on all modern DBMS.
SQLFiddle: http://sqlfiddle.com/#!15/c1a74/1

No, if you want to use aggregate functions then you have to GROUP BY and end up getting a single row for the columns you have specified in that GROUP BY col1, col2, col3.
If you want multiple records (SELECT *) and also want the SUM(Colname) of one of them then do it in 2 separate queries.

Yes you can do that with a subquery
SELECT
*
FROM
Table,
(SELECT
SUM(Colname) SumColumn
FROM
Table) t1
I tested in SQL Server with this small example
CREATE TABLE #Test (IntData INT, Data NVARCHAR(20))
INSERT INTO #Test(IntData, Data)
SELECT 1, 'Data1' UNION
SELECT 3, 'Data2' UNION
SELECT 5, 'Data3' UNION
SELECT 7, 'Data4' UNION
SELECT 9, 'Data5' UNION
SELECT 11, 'Data6'
SELECT
*
FROM
#Test,
(SELECT
SUM(IntData) SumIntData
FROM
#Test) t1
DROP TABLE #Test
Hope this helps

Related

Selecting distinct values within a a group

I want to select distinct values of one variable within a group defined by another variable. What is the easiest way?
My first thought was to combine group by and distinct but it does not work. I tried something like:
select distinct col2, col1 from myTable
group by col1
I have looked at this one here but can't seem to solve my problem
Using DISTINCT along with GROUP BY in SQL Server_
Table example
If your requirement is to pick distinct combinations if col1 and COL2 then no need to group by just use
SELECT DISTINCT COL1, COL2 FROM TABLE1;
But if you want to group by then automatically one record per group is displayed by then you have to use aggregate function of one of the columns i.e.
SELECT COL1, COUNT(COL2)
FROM TABLE1 GROUP BY COL1;
no need group by just use distinct
select distinct col2, col1 from myTable
create table t as
with inputs(val, id) as
(
select 'A', 1 from dual union all
select 'A', 1 from dual union all
select 'A', 2 from dual union all
select 'B', 1 from dual union all
select 'B', 2 from dual union all
select 'C', 3 from dual
)
select * from inputs;
The above creates your table and the below is the solution (12c and later):
select * from t
match_recognize
(
partition by val
order by id
all rows per match
pattern ( a {- b* -} )
define b as val = a.val and id = a.id
);
Output:
Regards,
Ranagal

Perform a function after union of two tables

I have two tables that I want to union together then perform some math functions on the combined table.
I know how to do the math for each separate table, but throwing in a union table to go off of is out of my league.
Here's the math for one table using column header "UnitsReceived" and "AsnPsUnits"
The other table would have headers: "cUnitsReceived" and "cAsnPsUnits"
select VendName,
1-abs(((cast(sum(UnitsReceived) as decimal(5,0))) - (cast(sum(AsnPsUnits) as decimal(5,0)))) /(cast(sum(AsnPsUnits) as decimal(5,0)))) as ASNpsAcc
from VenTest2
where ID<20
group by VendName
How would I perform this function after the union of two tables?
You'll need to get the unioned tables into some table object before performing your function. This could be done using:
A Common Table Expression
with cte as (
select ID, VALUE from A
union all
select ID, VALUE from B
)
select
*
,myfunction(VALUE) as MyFunctionResult
from
cte
A temp table
select ID, VALUE into #myTempTable from A
insert into #myTempTable select ID, VALUE from B
select
*
,myfunction(VALUE) as MyFunctionResult
from
#myTempTable
A table variable
declare #myTableVariable table (ID int, VALUE decimal)
insert into #myTableVariable
select ID, VALUE from A
union all
select ID, VALUE from B
select
*
,myfunction(VALUE) as MyFunctionResult
from
#myTableVariable
A sub query
select
*
,myfunction(VALUE) as MyFunctionResult
from
(
select ID, VALUE from A
union all
select ID, VALUE from B
) mySubQuery
This will help with the subq being the union
select VendName,
1-abs(((cast(sum(UnitsReceived) as decimal(5,0))) - (cast(sum(AsnPsUnits) as decimal(5,0)))) /(cast(sum(AsnPsUnits) as decimal(5,0)))) as ASNpsAcc
from
(
select ID, UnitsReceived, AsnPsUnits from VenTest2 where ID<20
union
select ID1, UnitsReceived1, AsnPsUnits1 from VenTest1
)a
group by VendName
This is not the way, brothers:
select VendName,
1-abs(((cast(sum(UnitsReceived) as decimal(10,2))) - (cast(sum(AsnPsUnits) as decimal(10,2)))) /(cast(sum(AsnPsUnits) as decimal(10,2)))) as ASNpsAcc
from VenTest2
where ID<10
group by VendName
union
select cVendName,
1-abs(((cast(sum(cUnitsReceived) as decimal(10,2))) - (cast(sum(casnpsunits) as decimal(10,2)))) /(cast(sum(cAsnPsUnits) as decimal(10,2)))) as ASNpsAcc
from CTest
where id <10
group by cvendname

I want to retrieve max value from two column - SQL table

Please find attached image for table structure
You could try using UNION ALL for build a unique column result and select the max from this
select max(col)
from (
select col1 col
from trans_punch
union all
select col2
from trans_punch) t
You can use a common table expression to union the columns, then select the max.
;with cteUnionPunch(Emp_id, both_punch) AS
(
SELECT Emp_id, In_Punch FROM trans_punch
UNION ALL
SELECT Emp_id, Out_Punch FROM trans_punch
)
SELECT Emp_id, max(both_punch) FROM cteUnionPunch GROUP BY Emp_id
You can use apply :
select tp.Emp_id, max(tpp.Punchs)
from trans_punch as tp cross apply
( values (In_Punch), (Out_Punch) ) tpp(Punchs)
group by tp.Emp_id;

SQL query for given input table to output table

I want to know query for the result
You can use PIVOT/UNPIVOT. Solution is here
You can achieve your expected result by below query,
SELECT *
FROM cardata
PIVOT(AVG(Price) FOR YEAR IN ([2010], [2011], [2012])) AS PivotTable;
So here you must have to use aggregate function to use Pivot.
You need to use PIVOT : SQL Fiddle
Data
create table cars(brand varchar(100),myear int,price bigint)
insert into cars
select 'audi',2010,5000000 union all
select 'audi',2011,5340000 union all
select 'audi',2012,5890000 union all
select 'bmw',2010,6000000 union all
select 'bmw',2011,6780000 union all
select 'bmw',2012,4450000 union all
select 'maruti',2010,4540000 union all
select 'maruti',2011,7800000 union all
select 'maruti',2012,9000000
Query
SELECT * FROM
cars
PIVOT
(
MAX(PRICE) FOR MYEAR IN ([2010],[2011],[2012])
)P

using SELECT INTO with multiple rows

This is re
I want to create a table using the results of a query by utilizing SELECT INTO.
The syntax
SELECT *
INTO Persons_Backup
FROM Persons
is very close to what I want to achieve, with the difference being that I want the FROM to use a query as source.
My situation is a bit more complicated than these simple examples.
I need to create a table and insert multiple rows at the same time. If I could (I can't) use a previously created table the statement would look like this:
INSERT INTO Person_Backup12 (Col1, Col2, Col3)
Select 1, 'a','2001-01-01 12:00'
UNION ALL
Select 83, 'z','2011-09-30 13:27'
UNION ALL
Select 777, 'k','1997-04-25 09:27'
Can I do that while creating a table at the same time?
You can put your query into a common table expression or derived table then SELECT ... INTO from that.
;WITH cte (Col1, Col2, Col3) AS
(
Select 1, 'a','2001-01-01 12:00'
UNION ALL
Select 83, 'z','2011-09-30 13:27'
UNION ALL
Select 777, 'k','1997-04-25 09:27'
)
SELECT *
INTO NewTable
FROM cte
In this case you would probably need some explicit casts to get the desired column datatype (datetime rather than char etc.)
A CTE shouldn't be necessary:
Select 1 as 'Col1', 'a' as 'Col2','2001-01-01 12:00' as 'Col3'
INTO Person_Backup12
UNION ALL
Select 83, 'z','2011-09-30 13:27'
UNION ALL
Select 777, 'k','1997-04-25 09:27'
Worked fine for me in 2008 r2.
It is possible, yes:
SELECT *
INTO Persons_Backup
FROM
(
Select 1 AS Col1, 'a' AS Col2,'2001-01-01 12:00' AS Col3
UNION ALL
Select 83 AS Col1, 'z' AS Col2,'2011-09-30 13:27' AS Col3
UNION ALL
Select 777 AS Col1, 'k' AS Col2,'1997-04-25 09:27' AS Col3
) AS SomeQuery