SQL query for given input table to output table - sql

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

Related

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;

Distinct values from two columns

This is the query which I am using to get the distinct values:
SELECT DISTINCT
LOGIN_BY,
RECEIVED_BY
FROM SAMPLE
My requirement is I need the distinct values of both the columns as a single output. Is there any way to do that?
try this
(SELECT DISTINCT LOGIN_BY as ID from sample )
union all
(Select Distinct RECEIVED_BY as ID FROM SAMPLE)
Try it like this:
select distinct a from (
select
distinct LOGIN_BY as a
from
SAMPLE
union
select
distinct RECEIVED_BY as a
from
SAMPLE
);
This works for me on an Oracle database.

SELECT ALL with SUM(Col)

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

Simplest way to process multiple hardcoded values in SQL?

How would I do this in SQL Server? (I know it won't run as written but it illustrates the question better than I can explain)
SELECT SQRT(number) WHERE number IN (4,9,16,25)
It would return multiple rows of course
you can use table value constructor
select sqrt(number)
from (
values (4),(9),(16),(25)
) as T(number)
or use union all
select sqrt(number)
from (
select 4 union all
select 9 union all
select 16 union all
select 25
) as T(number)
sql fiddle demo
You could create a derived table:
SELECT SQRT(number)
FROM (
SELECT 4 AS number
UNION ALL SELECT 9
UNION ALL SELECT 16
UNION ALL SELECT 25
) A