Hello all,
I have data like :-
year amt
2004 10
2005 20
2006 30
Now i want output as:-
2004 2005 2006
10 30 60
but i have to use here group by on year.So, i need a single
query within that i can find.
Answers were Sorted based on User's Feedback
Answer / sagun sawant
select ((select sum((case when year = '2004' then (amt)
else 0 end)) from account )) as [2004]
,((select sum((case when year = '2005' then (amt)
else 0 end)) from account )) as [2005]
,((select sum((case when year = '2006' then (amt)
else 0 end)) from account )) as [2006]
| Is This Answer Correct ? | 2 Yes | 2 No |
Answer / pradip jain
Pivot concept can be use
please correct this as it it near to correct.
SELECT
[2004] '2004',
[2005] '2005',
[2006] '2006'
FROM
(select year,amt from dbo.Pivot1) s
PIVOT
(
sum(amt )
FOR year IN ([2004],[2005],[2006])
) p
output is
2004 2005 2006
10 20 30
| Is This Answer Correct ? | 2 Yes | 2 No |
Answer / murali krishna reddy
SELECT A.YEAR, (SELECT SUM(AMT) FROM ACCOUNT WHERE YEAR <=
A.YEAR) AS AMT
FROM ACCOUNT AS A GROUP BY A.YEAR
How about this?
| Is This Answer Correct ? | 0 Yes | 2 No |
How to restart SQL Server in single user mode? How to start SQL Server in minimal configuration mode?
What is the difference between OLEDB and ODBC
Explain what is log shipping?
In what version of sql server were synonyms released?
Which tcp/ip port does sql server run on? How can it be changed?
How can we determine what objects a user-defined function depends upon?
What is tablesample?
How to create user messages with print statements in ms sql server?
What is a cursor in SQL Server 2000 and their types?
What is difference between clustered index and non clustered index?
When we are using this query to shrink the log file,what exactly it will execute internally? Do we lose any data when we run this script? which data it will truncate in the log file and where it is saved. Please let me know... USE DatabaseName GO DBCC SHRINKFILE(<TransactionLogName>, 1) BACKUP LOG <DatabaseName> WITH TRUNCATE_ONLY DBCC SHRINKFILE(<TransactionLogName>, 1) GO
What is a livelock?
Oracle (3259)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)