I have one table like below format:
ID Date Frequency
1 01/06/2009 1
2 01/06/2009 1
3 01/06/2009 2
4 01/06/2009 1 *
5 01/06/2009 1
6 01/06/2009 2 *
I want result set like below:
Current : 6 01/06/2009 2
Previous : 4 01/06/2009 1
I am using SQL Server 2000 databse.Please provide solution for above resultset.
This is a pretty basic SQL statement
SELECT count(ID), date, Frequency
FROM yourTable
Group By Date, Frequency
ORDER BY Frequency DESC
But I'm not positve due to * and the current: Previous: statements
Related
There is a table with values as below,
Id Value
1 1
2 1
3 2
4 2
5 3
6 4
7 4
now need to write a query to retrieve value from the table and output should look as
ID Value
1 1
3 2
5 3
6 4
any suggestion ?
The query you want is nothing to do with being distinct, it's a simple aggregation of value with the minimum ID for each:
select Min(id) Id, value
from table
group by value
I want to group records by row numbers.
Like from row 1-3 in group 1 , 4-6 in group 2 , 7-9 in group 3 and so on.
Suppose below is the table structure:
Row NumberDataValue
1 A 10
2 A 5
3 A 1
4 A 33
5 A 2
6 A 127
1 B 1
2 B 0
3 B 7
4 B 7
5 B 5
6 B 8
7 B 1
8 B 0
I want a output like this:
GroupValue
1 10
1 5
1 1
2 33
2 2
2 127
1 1
1 0
1 7
2 7
2 5
2 8
3 1
3 0
I am using Oracle 11G.
I can achieve this using PL/SQL. But I have to use SQL only. As I have to use this query in a reporting tool.
If this is a duplicate question please provide the link of the answered question.
Subtract 1 from the column "RowNumber" and divide by 3.
Then use TRUNC() to get the integer part:
SELECT TRUNC(("RowNumber" - 1) / 3) + 1 "Group",
"Value"
FROM tablename
See the demo.
I would assume the name of the first column is ordering.
You can do:
select
1 + trunc(row_number() over(partition by data order by ordering) - 1) / 3,
value
from t
What you show looks like the output from something like this:
select ceil(rn/3) as grp, value
from your_table
order by rn;
Note that "row number" and "group" are reserved words/phrases which should not be used as column names. I used rn and grp instead.
I think the ceiling function is the simplest way to arrive at what you want. If you want to base it on the RowNumber column:
select ceil( RowNumber / 3.0) as grouping
If you want to calculate it yourself using row_number():
select ceil( row_number() over (order by RowNumber) / 3.0 ) as grouping
The first six balls mean first over, next six balls mean second over & so on than how to get average runs for each over.
input as
Ball no Runs
1 4
2 6
3 3
4 2
5 6
6 1
1 2
2 4
3 6
4 3
5 1
6 1
1 2
output should be:
Over no avg runs
1 3.66
2 2.83
As Gordon Linoff suggested, SQL table represents unordered sets, So you have to use an ordered column in your table. If you can use such a column you may use below query -
SELECT Over_no AVG(Runs) avg_runs
FROM (SELECT Ball_no, Runs, CEIL(ROW_NUMBER() OVER(ORDER BY ORDER_COLUMN, Ball_no) RN / 6) Over_no
FROM YOUR_TABLE)
GROUP BY Over_no;
I have managed to solve my problem with the following query:
SELECT ROWNUM OVER_NO, AVG_RUNS
FROM(
SELECT ROWNUM RN,
ROUND(AVG(RUNS)OVER(ORDER BY ROWNUM RANGE BETWEEN CURRENT ROW AND 5 FOLLOWING),2) AVG_RUNS
FROM TABLE_NAME
)
WHERE RN=1 OR RN=7;
I have table where number of errors made by an employee is stored.
i_empid error_category error_count date
13 1 1 1-feb-2017
13 2 1 1-feb-2017
13 2 2 3-feb-2017
341 1 1 3-feb-2017
I want result set to group by error category for particular date
error_category error_count 1-feb-2017 2-feb-2017 3-feb-2017
1 2 1 0 1
2 0 1 0 2
How can I achieve this?
I am not sure what database you use, is it MySQL?
I think youre trying to do something like this:
select t.error_category, t.error_count, sum(IF(t.date='1-feb-2017',1,0)) as `1-feb-2017`,
sum(IF(t.date='3-feb-2017',1,0)) as `3-feb-2017`
from <yourTable> t
group by t.error_category
There are different ways to archieve this actually
I have a table that has transactions by employeeID by TransactionTime. Each employee may have multiple transactions that occur at the same time. For example: EmployeeID 1 has 2 transactions at 12. I need to sum the transactions by EmployeeID at each time interval. So for employeeID 1, the new column (TotalTransactionsByTime) result would be 2. Next, if the CODE for a given TransactionTime has a CODE of BAD, I need to exclude all transactions at that time increment. So for EmployeeID 2, I would need to exclude all three transactions from the result set because they have a CODE of 'BAD' which nullifies all transactions at that increment.
MY TABLE
|EmployeeID|TransactionTime|CODE|
1 12 GOOD
1 12 GOOD
1 5 GOOD
2 1 BAD --need to omit all 3 transactions for employeeID 2
2 1 GOOD
2 1 GOOD
3 3 GOOD
3 3 GOOD
A correct result would look like:
|EmployeeID | TransactionTime | CODE | NUMBERTRNS
1 12 GOOD | 2
1 5 GOOD | 1
3 3 GOOD | 2
select mt1.EmployeeID, mt1.TransactionTime, mt1.CODE, count(*) as NUMBERTRNS
from MyTable mt1
where mt1.EmployeeID not in (select EmployeeID from MyTable where CODE = 'BAD')
group by mt1.EmployeeID, mt1.TransactionTime, mt1.CODE