How to write the sql select query for this? - sql

i have mssql tabel like this >
ID Code Rating
1 10 4
2 10 5
3 10 4
4 11 2
5 11 3
The sql query logic i want ...
I want when i search the record using code 10 then the output would be 4 because the 4 rating would be given most of the time for code 10 ....
and another logic if i search for code 11 then the out put will be 3 because 3 will be most recent rate for code 11...
how to write the sql query for im using ASP.NET ( VB)

The first thing you want to do is filter:
SELECT * FROM mytable WHERE Code = 10
You're interested in the 'rating' field:
SELECT Rating FROM mytable WHERE Code = 10
Now you want to count entries for Rating, which can be achieved using a combination of GROUP BY and the COUNT() function:
SELECT COUNT(*), Rating FROM mytable WHERE Code = 10 GROUP BY Rating
Now all that's left is sort by count, descending, and select only the first row:
SELECT TOP 1 Rating FROM mytable WHERE Code = 10 GROUP BY Rating ORDER BY COUNT(*) DESC

Related

How do i select 4 rows from a table holding 10 rows of persons based on the youngest age?

Lets say i have a table containing these rows
id age
1 5
2 7
3 8
4 9
5 3
6 1
How do i write a select statement that selects exactly 4 of the youngest persons?
You would use top and order by:
select top (4) t.*
from t
order by age asc;
SELECT id, age
FROM [MyTable]
ORDER BY age
OFFSET 0 ROWS FETCH NEXT 4 ROWS ONLY

Get ID of row from max value

I'm new in database
I have the following table:
id group factor
------------------------
1 11 1
2 11 5
4 11 2
5 12 3
6 12 2
7 13 4
8 13 1
9 13 8
10 14 6
I need to get the id of the one that has the largest factor based on its group, for example, in the case of group 11, the row with the largest factor is 5, so I need to obtain the id of that row, in this case 2.
please if someone can show me the right way.
You could use:
SELECT DISTINCT ON(group) group factor, id
FROM tab
ORDER BY group, factor DESC;
db<>fiddle demo
You can use simple CTE (Common Table Expression) for it, as in:
with
x as (
select group_id, max(factor) as max_factor from my_table group by group_id
)
select t.*
from my_table t
join x on x.group_id = t.group_id and x.max_factor = t.factor
This solution has the [desirable?] feature that in case there are multiple rows tied in first place in the same group, it will show them all, not just one [randomly] of them.
If you know the group in advance, e.g. 11, then you can simply do:
SELECT id
FROM tab
WHERE group = 11
ORDER BY factor DESC
LIMIT 1;
Otherwise if you want the result for each group that exists in the table then Lukasz Szozda's answer it the way to go.

Derby DB last x row average

I have the following table structure.
ITEM TOTAL
----------- -----------------
ID | TITLE ID |ITEMID|VALUE
1 A 1 2 6
2 B 2 1 4
3 C 3 3 3
4 D 4 3 8
5 E 5 1 2
6 F 6 5 4
7 4 5
8 2 8
9 2 7
10 1 3
11 2 2
12 3 6
I am using Apache Derby DB. I need to perform the average calculation in SQL. I need to show the list of item IDs and their average total of the last 3 records.
That is, for ITEM.ID 1, I will go to TOTAL table and select the last 3 records of the rows which are associated with the ITEMID 1. And take average of them. In Derby database, I am able to do this for a given item ID but I cannot make it without giving a specific ID. Let me show you what I've done it.
SELECT ITEM.ID, AVG(VALUE) FROM ITEM, TOTAL WHERE TOTAL.ITEMID = ITEM.ID GROUP BY ITEM.ID
This SQL gives the average of all items in a list. But this calculates for all values of the total tables. I need last 3 records only. So I changed the SQL to this:
SELECT AVG(VALUE) FROM (SELECT ROW_NUMBER() OVER() AS ROWNUM, TOTAL.* FROM TOTAL WHERE ITEMID = 1) AS TR WHERE ROWNUM > (SELECT COUNT(ID) FROM TOTAL WHERE ITEMID = 1) - 3
This works if I supply the item ID 1 or 2 etc. But I cannot do this for all items without giving an item ID.
I tried to do the same thing in ORACLE using partition and it worked. But derby does not support partitioning. There is WINDOW but I could not make use of it.
Oracle one
SELECT ITEMID, AVG(VALUE) FROM(SELECT ITEMID, VALUE, COUNT(*) OVER (PARTITION BY ITEMID) QTY, ROW_NUMBER() OVER (PARTITION BY ITEMID ORDER BY ID) IDX FROM TOTAL ORDER BY ITEMID, ID) WHERE IDX > QTY -3 GROUP BY ITEMID ORDER BY ITEMID
I need to use derby DB for its portability.
The desired output is this
RESULT
-----------------
ITEMID | AVERAGE
1 (9/3)
2 (17/3)
3 (17/3)
4 (5/1)
5 (4/1)
6 NULL
As you have noticed, Derby's support for the SQL 2003 "OLAP Operations" support is incomplete.
There was some initial work (see https://wiki.apache.org/db-derby/OLAPOperations), but that work was only partially completed.
I don't believe anyone is currently working on adding more functionality to Derby in this area.
So yes, Derby has a row_number function, but no, Derby does not (currently) have partition by.

Get offset of a row after performing sort operation in sql

I am using SQLite database.
Suppose I have rows with IDs 1 to 50. Then I perform select and order by operation.
Say, the result is IDs : 6,3,5,2,9,12,1,34,45,15.
Now, I want to know the offset of a particular row with given ID in the above result.e.g. offset of ID 1 is 6.
Can I do this in a single query?
put the query of ordered result into a subquery and use count(*) and check the id sequence:
Example:
SCHEMA:
CREATE TABLE tbl ("id" INTEGER,"val" INTEGER);
INSERT INTO tbl ("id","val")
VALUES
(12,6),(1,7),(34,8),(6,1),(9,5),
(45,9),(15,10),(3,2),(5,3),(2,4);
QUERY:
select id,(
select count(*)
from (
select id,val
from tbl order by val
) b
where a.val >= b.val)-1 as offset
from tbl a
order by offset
RESULT:
id offset
6 0
3 1
5 2
2 3
9 4
12 5
1 6
34 7
45 8
15 9
SQLFIDDLE DEMO

Select Distinct and Get Count

I need to get unique records from table and get their count of recurrence. I have query:
SELECT AwardReference.ownerID
FROM AwardReference
WHERE AwardReference.ownerType = 'song';
This returns something like:
ownerID
1
2
4
4
5
5
5
I need do get something like:
ownerID count
1 1
2 1
4 2
5 3
Does anyone knows how to do it?
Also I have to do this task with MS ACCESS...
SELECT AwardReference.ownerID ,
COUNT(*) AS count
FROM AwardReference
WHERE AwardReference.ownerType = 'song'
GROUP BY AwardReference.ownerID