SQLite selecting multiple sets of rows - sql

I'm using SQLite, trying to select multiple sets of rows. Example:
RowID USER Event
------------------
1 Sam eventX
2 Sam eventY
3 Sam eventA
4 John E1
5 John E5
6 Lisa ev3
7 Lisa ev4
8 Lisa ev3
I want to select one entry per USER, the one with the highest rowid.
The result set should look like:
RowID USER Event
------------------
3 Sam eventA
5 John E5
8 Lisa ev3
Please help.

Mean and lean:
SELECT * FROM Table1 WHERE RowID IN (
SELECT MAX(RowID) FROM Table1 GROUP BY User
);

select max(RowID),User,Event from Info group by User order by max(RowID)
Info Table
Result

Please learn Aggregate function for SQL Lite
select max(RowID) as RowID,User,Event
from Table1
group by User
order by max(RowID)
SQL FIDDLE

SELECT rowid, user, event FROM table WHERE rowid IN (SELECT MAX(rowid) FROM table GROUP BY user);

Related

joining 2 tables in sql which has no dependency on each other

I have 2 tables in the following way
Table 1:
e_id e_name e_salary e_age e_gender e_dept
---------------------------------------------------
1 sam 95000 45 male operations
2 bob 80000 21 male support
3 ann 125000 25 female analyst
Table 2:
d_salary d_age d_gender e_dept
----------------------------------
34000 25 male Admin
56000 41 female Tech
77000 35 female HR
I want the output something like this:
e_id e_name e_salary e_age e_gender e_dept d_salary d_age d_gender e_dept
1 sam 95000 45 male operations 34000 25 male Admin
2 bob 80000 21 male support 56000 41 female Tech
3 ann 125000 25 female analysts 77000 35 female HR
There is no dependency between the tables. No common columns. No primary or foreign key.
I tried using cross join that results in duplicate rows because it works on M X N
I am new to this SQL thing. Can someone help me, please? Thanks in advance
Though I didn't get the reason behind your desired output but you can get that with below query:
select a.e_id ,a.e_name ,a.e_salary ,a.e_age ,a.e_gender ,a.e_dept,b.d_salary ,b.d_age ,b.d_gender ,b.e_dept
from
(select e_id ,e_name ,e_salary ,e_age ,e_gender ,e_dept, row_number()over(order by e_id)rn
from table1)a
inner join
(select d_salary d_age d_gender e_dept,row_number()over(order by d_salary) rn
from table 2) b
on a.rn=b.rn
Generally you can create a row count using the row_number() window function on both tables and use this as join criterion. But this requires a certain order for both tables, which means that you have explicitly tell the query why is the Admin record ordered first and must be joined on the first record of table 1:
SELECT
*
FROM (
SELECT
*,
row_number() OVER (ORDER BY e_id) as row_count -- assuming e_id is your order criterion
FROM table1
) t1
JOIN (
SELECT
*,
row_number() OVER (ORDER BY /*whatever you expect to be ordered*/) as row_count
FROM table2
) t2
ON t1.row_count = t2.row_count

Limit column value repeats to top 2

So I have this query:
SELECT
Search.USER_ID,
Search.SEARCH_TERM,
COUNT(*) AS Search.count
FROM Search
GROUP BY 1,2
ORDER BY 3 DESC
Which returns a response that looks like this:
USER_ID SEARCH_TERM count
bob dog 50
bob cat 45
sally cat 38
john mouse 30
sally turtle 10
sally lion 5
john zebra 3
john leopard 1
And my question is: How would I change the query, so that it only returns the top 2 most-searched-for-terms for any given user? So in the example above, the last row for Sally would be dropped, and the last row for John would also be dropped, leaving a total of 6 rows; 2 for each user, like so:
USER_ID SEARCH_TERM count
bob dog 50
bob cat 45
sally cat 38
john mouse 30
sally turtle 10
john zebra 3
In SQL Server, you can put the original query into a CTE, add the ROW_NUMBER() function. Then in the new main query, just add a WHERE clause to limit by the row number. Your query would look something like this:
;WITH OriginalQuery AS
(
SELECT
s.[User_id]
,s.Search_Term
,COUNT(*) AS 'count'
,ROW_NUMBER() OVER (PARTITION BY s.[USER_ID] ORDER BY COUNT(*) DESC) AS rn
FROM Search s
GROUP BY s.[User_id], s.Search_Term
)
SELECT oq.User_id
,oq.Search_Term
,oq.count
FROM OriginalQuery oq
WHERE rn <= 2
ORDER BY oq.count DESC
EDIT: I specified SQL Server as the dbms I used here, but the above should be ANSI-compliant and work in Snowflake.

Get number of different values in a column in Access

I've tried more or less all combinations of count and distinct (except the correct one :) ) in order to get the example below.
Input: table t1
NAME | FOOD
Mary | Apple
Mary | Banana
Mary | Apple
Mary | Strawberry
John | Cherries
Expected output:
NAME | FOOD
Mary | 3
John | 1
N.B. Mary has Apple in two rows but she has 3 as we have 3 different values in the column.
I only managed to get 4 in FOOD Column for her, but I need 3 :(
select a.name as NAME, a.count(name) as Food
from
(SELECT distinct NAME,Food from table)a
Start with a query which gives you unique combinations of NAME and FOOD:
SELECT DISTINCT t1.NAME, t1.FOOD
FROM t1
Then you can use that as a subquery in another where you can GROUP BY and Count:
SELECT sub.NAME, Count(*) AS [FOOD]
FROM
(
SELECT DISTINCT t1.NAME, t1.FOOD
FROM t1
) AS sub
GROUP BY sub.NAME;
select a.name, sum(a.FoodCount) from(
select distinct name,COUNT(food) as FoodCount from #t1 group by name, food ) as a group by a.name order by 2 desc

Count entries that have different values in other column

Here is an (simplified) example of DB I have (sorry for the ulgy format, I don't know how to write tables):
Name | Num
John | 1
John | 3
John | 4
Dany | 2
Andy | 5
Andy | 5
I want to count how many people have more at least two different Numbers.
For instance, here, only john, because he has 1, 3 and 4.
Not Andy because he has twice 2 and no other one.
And obviously not Dany because he has only one entry.
Thank you very much.
Try this.
select count(name) from table group by name having count(distinct num)>1
Try this:
SELECT A.Name, COUNT(DISTINCT A.Num) cnt
FROM tableA
GROUP BY A.Name
HAVING cnt >= 2;
select count(*)
from (
select Name from Temp group by Name having count(distinct num) > 1
) as a
Try this:
select name from `table` group by name,num having count(num)>1

Removing duplicate rows but retaining unique copy

I got a SQL table of the form:-
Name Age
Kim 5
Tom 8
Jim 12
Kim 5
David 21
Jim 12
In the above scenario, Kim and Jim are duplicated and they exist twice.
If I have a large table of the above form, with about 60000 entries, how do I write a sql query to delete only the duplicates but still retain the unique ones?
With the help of Common Table Expression and a Window Function, you can easily delete duplicate records.
WITH dups
AS
(
SELECT Name, Age,
ROW_NUMBER() OVER (PARTITION BY Name, Age ORDER BY Age DESC) rn
FROM TableName
)
DELETE FROM dups
WHERE rn > 1
SQLFiddle Demo
Ranking Functions (Transact-SQL)