Counting Duplicates from table - asp.net

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;

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)

How to exclude the records Using Qualify statement in Teradata

I have to create population for the people who has only one product association (ABC) using qualify statement.
For example I have the data
Id Code Prod Date
101 202 ABC 2017-05-31
101 203 DEF 2017-04-30
102 302 ABC 2018-06-30
From the above data I need the data for Id=102 because this id has only one prod relation where as id 101 has both ABC and DEF which should be excluded.
I tried the following
Select id,prod from table1
Qualify row_number() over (partition by id order by Date)=1
Where prod=‘ABC’
With this, I get the two records in my data which I don’t want. Appreciate your help.
Select *
from table1
Qualify min(Prod) over (partition by id)='ABC'
and max(Prod) over (partition by id)='ABC'
Both MIN and MAX return the same value ABC, thus there's no other value
If you want to return the id's that have one prod value (ABC) in the table, you can do something like this:
SELECT id, prod
FROM (
SELECT id, prod
FROM table1
GROUP BY id, prod -- Get unique (id, prod) combinations
QUALIFY COUNT(prod) OVER(PARTITION BY id) = 1 -- Get id's with only one prod
) src
WHERE prod = 'ABC' -- Only get rows with "ABC" prod
The key here is the order in which Teradata processes the query:
Aggregate - GROUP BY
OLAP - COUNT(prod) OVER()
QUALIFY
You may be able to move the WHERE prod = 'ABC' into the QUALIFY clause and get rid of the outer SELECT, not 100% sure.
Just use having, instead of qualify. I don't see any need for window fuctions. Something like:
Select id,prod ,
count(prod)
from
table1
group by
id,
prod
having count(prod) = 1

Teradata - OLAP Functions - filter rows

I'm wondering if I can use an OLAP Function to filter irrelevant rows like this:
If I have one matching value (the fourth fields) all the rows with the same key ( the first 3 fields) must not be displayed
In this example, the matching value would be 'C':
Entities product ID Solde
997 0050 123 D
997 0050 123 D
997 0050 123 C
899 0124 125 D
899 0124 125 D
So here My key is composed by entities/product/ID, regarding the value of "Solde" I need to display or not.
Here the the undesired value is Solde = C.
In this example only the last row should be diplayed, because the key 899/0124/125 has only rows with solde = 'D'
The key 997/0050/123 has one row with solde = 'C' so I don't want to display it
Thanks in advance for your helping
Christophe
Updated answer
The more traditional way to solve this is to first select the Entities/Product/ID records that you DON'T want.
SELECT Entities, Product, ID FROM table WHERE Solde<>'D';
Use that result in a subquery in your WHERE clause to exclude those:
SELECT DISTINCT Entities, Product, ID, Solde
FROM table
WHERE (Entities, Product, ID) NOT IN ( SELECT Entities, Product, ID FROM table WHERE Solde<>'D');
Alternatively using a HAVING clause and aggregating
SELECT Entities, Product, ID
FROM table
COUNT(*) = SUM(CASE WHEN Solde = 'D' THEN 1 ELSE 0 END)
GROUP BY 1,2,3
I guess you are looking for answer as the below:
SELECT Solde
FROM yourtable
QUALIFY COUNT(*) OVER (PARTITION BY Entities, Product, ID, Solde) = 1;

populate from diff tables based on condition

I'm trying to join 3 tables in a view; here is the situation:
I have 3 tables....
first one is [ADM_Panels] that contain information of panels with
columns:panel_id, panel_number, YearID
second one is [ADM_PanelsDtl] with
columns: [panel_id], [pnlmember_id]
I have another table called "GEN_Years" with 2 columns
table 3: [GEN_Years]
columns:[YearID] ,[Year]
I tried some thing like this but my select statement should return 2 different tables
SELECT YearID,count(pnlmember_id) Reviewer FROM [ADM_PanelsDtl] join [ADM_Panels]
on [ADM_PanelsDtl].[panel_id]=[ADM_Panels].[panel_id]group by YearID
select YearID,count([panel_id])Panel from dbo.ADM_Panels group by YearID
But i want to show my result in a table with fields
[YearID], [Year], count(pnlmember_id), count([panel_id])Panel
by joining the above 3 tables. Any solution on how to do it?
I don't want to use temporary table, I just want want to join these tables
May be lyk this
SELECT A.[YearID], c.[Year], a.Reviewer, B.Panel
FROM (SELECT YearID,
Count(pnlmember_id) Reviewer
FROM [ADM_PanelsDtl]
JOIN [ADM_Panels]
ON [ADM_PanelsDtl].[panel_id] = [ADM_Panels].[panel_id]
GROUP BY YearID) A
JOIN (SELECT YearID,
Count([panel_id])Panel
FROM dbo.ADM_Panels
GROUP BY YearID) B
ON a.YearID = b.YearID join GEN_Years c on a.YearID = c.YearID

PL/SQL: How can I write this program?

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

Resources