PL/SQL: How can I write this program? - plsql

I have 3 Columns: 'GAME' 'PLAYER_ID' 'MINUTES_PLAYED'
looks like this:
GAME ID MINUTES_PLAYED
12550788 229569 23
12500788 393438 4
12500788 458730 25
12500782 229569 10
I would like find total minutes played per game i.e.
ID TOTAL_MINUTES
229569 33
.
.
.
Thank you in advance!

select id, sum(minutes_played) from tableName group by id

SELECT ID, SUM(MINUTES_PLAYED)
FROM MYTABLE
GROUP BY ID

Simply add them up:
SELECT ID, SUM(MINUTES_PLAYED) AS TOTAL_MINUTES
FROM table GROUP BY ID

Related

SQLite select order by alphabet first, then all other characters

I have a SQLite db with a table, containing rows with different names. For example:
id
name
1
antony
2
%
3
10
4
stackoverflow
5
john
I get the data from this table with
SELECT * FROM table WHERE 1 ORDER BY name Asc LIMIT ?, ?
And it returns
id
name
2
%
3
10
1
antony
5
john
4
stackoverflow
But i want it to return names in alphabetical order first, then all other names which starts with non letters in the right order too. So i want to get:
id
name
1
antony
5
john
4
stackoverflow
2
%
3
10
How can i achieve that?
Use the operator GLOB to check if the name starts with a letter in the ORDER BY clause:
SELECT *
FROM tablename
ORDER BY name GLOB '[A-Za-z]*' DESC, name
See the demo.
Thanks to #forpas, just wish to add
If you wish to make Case insensitive sorting, you may try as below
SELECT *
FROM tablename
ORDER BY name GLOB '[A-Za-z]*' DESC, Upper(name)

Iteration for a non-sequential column

can some one help me...
I have to create,for each "Costumer", a iterator for a non-sequential ID to update the "version" column.
I need a cursor or something else?
Can i get some help?
Example:
ID COSTUMER VERSION
12 ANNA 1
24 ANNA 4
25 ANNA 5
60 ANNA 11
I want to correct the version to be sequential
You could use code something like this:
begin
for r in ( select id, row_number() over (partition by name order by version) as rn
from costumer
)
loop
update costumer
set version = r.rn
where id = r.id;
end loop;
end;
/
The partition by is there because I have assumed you want to have the sequence start from 1 for 'ANNA', then start from 1 again for customer 'JANE' etc. If not you can remove that part.
Here's the way to do it via a single MERGE statement:
MERGE INTO costumer tgt
USING (SELECT ID,
costumer,
VERSION,
ROWID row_id,
row_number() OVER (PARTITION BY costumer ORDER BY VERSION) new_version
FROM costumer) src
ON (tgt.rowid = src.rowid)
WHEN MATCHED THEN
UPDATE SET tgt.version = src.new_version;

How to create a column for even and odd records dynamically?

I have a query in Teradata. I want to add an additional column that would be a VARCHAR.
It should say whether the selected record is even or odd
select id, name, CASE newColumn WHEN --- ???
from my table
Like this
id name newColumn
1 asdf odd
2 ts df even
32 htssdf odd
4 asdfsd even
23 gftht odd
How can I do this
Based on your example, I can't tell how you are sorting the results. You would need to define a sort order. Let's assume you would do it based on the id number.
SELECT id, name,
ROW_NUMBER() OVER(ORDER BY id) row_id,
CASE WHEN ROW_NUMBER() OVER(ORDER BY id) MOD 2 = 0 THEN 'Even' ELSE 'Odd' END newColumn
FROM my table
The row_id is incrementally assigned based on the id field being sorted ascending. You then use the MOD function to determine if there's a remainder after dividing the number by a value (in this case 2). Result would look like the following:
id name row_id newColumn
1 asdf 1 Odd
2 ts df 2 Even
4 asdfsd 3 Odd
23 gftht 4 Even
32 htssdf 5 Odd

Counting Duplicates from table

I have got a table which i have mentioned below, i need to get a total results avoiding duplicates , can any one provide any assistance or suggestions on how to get the results mentioned below, thankyu
ID name Total Used
24 John 5 2
24 John 10 6
27 Peter 20 0
27 Peter 20 5
Result should be something like this
ID name Total Used
24 John 15 8
27 Peter 40 5
Looks like you just need to use SUM() on the two columns. Also use a GROUP BY on the id and name
SELECT id, name, sum(total) All_total, sum(used) All_used
FROM yourtable
GROUP BY id, name
see SQL Fiddle with Demo
The GROUP BY field must include any other columns you are selecting which are not in an aggregate function, so for the example you would include both id and name in the GROUP BY.
Edit #1 your query would be:
SELECT [ID] , name, sum([Total]), sum([Used]), [GUID]
FROM [table].[dbo].[vw_data]
GROUP BY [ID], [name], [GUID]
select sum(Total), sum(Used), ID, name
from table
group by ID, name;
select ID, name, sum( Total,), sum( Used)
from table
group by Id,name;
Try this:
select id,name, sum(Total),sum(used)
from tab
group by id,name
SELECT id, name, sum(total) total, sum(used) used from table GROUP BY id, name
MySql syntax for this problem:
CREATE TABLE DUPLICATE (ID,NAME, TOTAL, USED);
INSERT INTO DUPLICATE VALUES(24,'John',5,2);
INSERT INTO DUPLICATE VALUES(24,'John',10,6);
INSERT INTO DUPLICATE VALUES(27,'Peter',20,0);
INSERT INTO DUPLICATE VALUES(27,'Peter',20,5);
SELECT ID, NAME, SUM(TOTAL), SUM(USED) FROM DUPLICATE GROUP BY ID, NAME;

MySQL Specific Query Requirement

I have datetime field names "salestime" in "sales" table
I need to get data as:
SalesMonthYear Total
2010-11 10
2010-10 15
2010-09 21
or
Nov-2010 10
Oct-2010 15
Sep-2010 21
Any one know how can I achieve this using query?
Thanks for the help
You need to use DATE_FORMAT and GROUP BY to achieve what you want. This query will do it:
SELECT DATE_FORMAT(salestime, '%Y-%m') as SalesMonthYear, count(*) as `Total` FROM `sales` GROUP BY SalesMonthYear ORDER BY `salestime`
And for the second version:
SELECT DATE_FORMAT(salestime, '%b-%Y') as SalesMonthYear, count(*) as `Total` FROM `sales` GROUP BY SalesMonthYear ORDER BY `salestime`
SELECT DATE_FORMAT(salesTime, '%Y-%m') AS salesMonthYear,
COUNT(*) AS salesCount
FROM sales
GROUP BY DATE_FORMAT(salesTime, '%Y-%m')

Resources