Oracle sql, select First Row while selecting other things - sql

What do I need to add to the query below to get just the first row that shows up after this query?
Query:
SELECT column1,
column2,
MIN (column3),
MIN (colmun4)
FROM table
WHERE column0 = 'value'
GROUP BY column1, column2
Current Results:
column1 column2 column3 column4
30187 C 201330 1/3/2013 2:49:35 PM //I want this row only
33459 C 201330 1/3/2013 4:32:42 PM
90855 C 201390 3/28/2013 9:20:44 AM
96077 RA 201390 7/1/2013 11:31:46 AM

select * from (
SELECT column1,
column2,
MIN (column3),
MIN (colmun4)
FROM table
WHERE column0 = 'value'
GROUP BY column1, column2)
where rownum = 1
or if you're using oracle 12c you can use the below,
SELECT column1,
column2,
MIN (column3),
MIN (colmun4)
FROM table
WHERE column0 = 'value'
GROUP BY column1, column2
OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;

Related

SQL Script using sum(column1$)/count(distinct(column2person)

trying to use sum and count distint function & not getting results
Column1 column2 column3 column4 (3dividedby2)
personid count distinct sum$ sum$/count(distinct)
Above is the output i'm trying to get and what i see is this
Column1 column2 column3 column4 (3dividedby2)
1234 20 20,000 20,000
instead i would want to see this
Column1 column2 column3 column4 (3dividedby2)
1234 20 20,000 1,000
What am i doing wrong..
here is the query
select column1, count(distinct(column2)) as X, Sum(column3) as "COST"
, cost/ x as "Avg of column1 "
from table.table1
group by column1;
thanks!
You cannot re-use aliases in the select. Just repeat the expressions:
select column1, count(distinct column2) as X, Sum(column3) as cost,
sum(column3) / count(distinct column2) as avg_column1
from table.table1
group by column1;

Can I use TOP and Count in a single SELECT of SQL?

I am using SQL Server2008R2. I have below SQL select statement:
select column1, max(column2), min(column3)
from myTable
group by column1
order by column1
Let's say the above select statement returns 1001 records.
Let it return TOP 5 is not hard:
select top 5 column1, max(column2), min(column3)
from myTable
group by column1
order by column1
How can I modify the above statement so 1001 will also return and hence I know how many records in total?
I want some result like this:
1001 column1 max(column2) min(column3) -- top#1 row data
1001 column2 max(column2) min(column3) -- top#2 row data
1001 column1 max(column2) min(column3) -- top#3 row data
1001 column2 max(column2) min(column3) -- top#4 row data
1001 column2 max(column2) min(column3) -- top#5 row data
1001 is the total number of available records, and I only select top 5 of them. I want to know the total number and the details for top 5.
One method is with a subquery:
select top 5 *
from (select count(*) over () as cnt, column1, max(column2), min(column3)
from myTable
group by column1
) t
order by column1;
Although I prefer the subquery to prevent ambiguity, it also works without the subquery:
select top 5 count(*) over () as cnt, column1, max(column2), min(column3)
from myTable
group by column1
order by column1;
You can use the count() over() window function..
select top 5
count(*) over (),
column1,
max(column2),
min(column3)
from myTable
group by column1
order by column1

Return column with running sequence number Oracle

My simple query returns data like this:
SELECT column1, column2 FROM table1
COLUMN1 COLUMN2
------- -------
CA A
CA B
CB C
CB D
I want to return column3 with these values (for same COLUMN1 value, I want to return same sequence number):
COLUMN3
-------
1
1
2
2
You can use analytic function DENSE_RANK.
SELECT column1,
column2,
DENSE_RANK() OVER(ORDER BY column1) as "column3"
FROM table1
See the following for some examples - oracle-base.com/articles/misc/rank-dense-rank-first-last-analytic-functions.php#dense_rank
Try this query,
Select column1, column2,
dense_rank() over (order by column1) as column3
from table1;

SQL Count/Sum displaying column as rows

I have a table with 4 bit columns
I need to create a report that will show the total of all "true" values for each column but I need the column names to return as a row.
For examples, the table will contain:
Column1 Column2 Column3
1 1 0
0 1 0
1 1 0
The result should be:
Category Value
Column1 2
Column2 3
Column3 0
The table has other columns, I just need specific ones
Thanks
I don't know if there are other approaches, but the following should work:
select 'Column1' as "Category", sum(column1) as "Value" from my_table union
select 'Column2', sum(column2) from my_table union
select 'Column3', sum(column3) from my_table
Here's a SQLFiddle for it.
You can try UNPIVOT on the table (this is for SQL Server)
create table Test (Column1 bit, Column2 bit, Column3 bit)
insert into Test values (1,1,0)
insert into Test values (0,1,0)
insert into Test values (1,1,0)
SELECT Value, sum(Vals)
FROM
(CONVERT(INT, Column1) Column1, CONVERT(INT, Column2) Column2, CONVERT(INT, Column3) Column3
FROM Test) p
UNPIVOT
(Vals FOR Value IN
(Column1, Column2, Column3)
)AS unpvt
GROUP BY Value
PIVOT/UNPIVOT documentation
http://sqlfiddle.com/#!6/957c6/1/0
Try this:
select
category = "column1", value = sum (convert(int,col1)) from MyTable1
union
select
category = "column2", value = sum (convert(int,col2)) from MyTable1
union
select
category = "column3", value = sum (convert(int,col3)) from MyTable1

How to access the value of a function generated column in SQL

I have the following SQL
select count(*) col, column1, column2, column3 from TempTable
group by column1, column2, column3
order by 1 desc
so the column generated by the count will return a number and there are 17 rows that do not have the number 1 (duplicate rows as columns 1, 2 and 3 are primary keys) and i want to delete any that have the count greater than 1?
You can use the having-clause:
select count(*) col, column1, column2, column3
from TempTable group by column1, column2, column3
having count(*) > 1
order by 1 desc
To delete:
delete tt
from TempTable tt
inner join (select count(*) col, column1, column2, column3
from TempTable group by column1, column2, column3
having count(*) > 1) tmp
on tmp.column1 = tt.column1
and tmp.column2 = tt.column2
and tmp.column3 = tt.column3
First you insert the data in temporary table:
select count(*) col, column1, column2, column3
into #temp
from TempTable group by column1, column2, column3 order by 1 desc
Then, you delete the data, and insert it from the #temp table:
delete from TempTable
go
insert into TempTable select column1, column2, column3 from #temp
go