I'd like to count the size of a group.
My table looks like that:
Name Number
Renee Scott 1
Bruno Cote 1
Andree Scott 2
Renee Scott 2
Pierre Dion 2
Pierre Dion 3
Louise Tremblay 3
Renee Scott 3
Andree Scott 3
Jean Barre 3
Bruno Cote 3
There are 2 Name associated with the Number 1, 3 Name with Number 2 and 6 Name with 3. I'd like to select this table where the Number is associated with 3 name or more.
Thank you.
SELECT * FROM TABLENAME WHERE NUMBER IN
(
SELECT NUMBER FROM TABLENAME GROUP BY NUMBER HAVING COUNT(*)>3
)
Related
I have the following kdb table
name value price
-------------------------
Paul 1 2 3 4
where value and price are lists. How can I convert them into
name value price
------------------------------
Paul 1 3
Paul 2 4
? Thanks!!
ungroup is what you're looking for here.
As an aside, "value" is a reserved word in q and you should get an 'assign error if you try to use it as a column name.
q)t:([]name:`Paul;value:enlist 1 2;price:enlist 3 4)
'assign
q)t:([]name:`Paul;val:enlist 1 2;price:enlist 3 4)
q)ungroup t
name val price
--------------
Paul 1 3
Paul 2 4
I have a table with answers regarding different questions, all of them numbered. There are basically these columns: IdAnswer (unique for each answer in the table), IdUser (which won't repeat even if the same user answer questions a second time), IdQuestion and Answer.
IdAnswer IdUser IdQuestion Answer
1 John 1 0
2 John 4 1
3 John 5 1
4 John 6 0
5 Bob 1 1
6 Bob 3 1
7 Bob 5 0
8 Mark 2 0
9 Mark 7 1
10 Mark 5 0
I'd like to select from this table all answers to a specific question (say, IdQuestion = 5), and also the last question each user answered just before question number 5.
In the end I need a table that should look like this:
IdAnswer IdUser IdQuestion Answer
2 John 4 1
3 John 5 1
6 Bob 3 1
7 Bob 5 0
9 Mark 7 1
10 Mark 5 0
I've managed to make this work using a cursor to iterate through each line from the first SELECT result (which filters by IdQuestion), but I'm not sure if this is the best (and fastest) way of doing it. Is there any more efficient way of achieving the same result?
And by the way, I'm using SQL Server Management Studio 2012.
Here is one way using LEAD function
select * from
(
select *,NextQ = Lead(IdQuestion)over(partition by IdUser order by IdAnswer)
from youtable
) a
Where 5 in (IdQuestion, NextQ )
for older versions
;WITH cte
AS (SELECT prev_id = Min(CASE WHEN IdQuestion = 5 THEN rn - 1 END) OVER( partition BY IdUser),*
FROM (SELECT rn = Row_number()OVER(partition BY IdUser ORDER BY IdAnswer),*
FROM Yourtable)a)
SELECT *
FROM cte
WHERE rn IN ( prev_id, prev_id + 1 )
I want my query to return the rows of the table where a column (code) contains a specific value 3 or 4
then move bottom with order by customer
If I have a table something like this example:
Name Code
-------------
Arun 1
Arun 2
Arun 3
Arun 4
Babu 1
Babu 3
Raj 1
Raj 2
Ashok 1
Ashok 2
And using that table I want to my query to return the rows which column (code) contain value 3 or 4 bottom, and then the order by name. Is this possible to do using only one query?
Expected output
Name Code
------------
Ashok 1
Ashok 2
Raj 1
Raj 2
Arun 1
Arun 2
Arun 3
Arun 4
Babu 1
Babu 3
You could add a sub query in your ORDER BY clause which will allow for sorting by names which have 3/4 to be after other values:
SELECT atable.name, atable.code
FROM atable
ORDER BY (
SELECT a1.code
FROM atable a1
WHERE a1.name = atable.name
AND a1.code IN ( 3, 4 )
LIMIT 1 ) DESC, atable.name, atable.code;
my table look like this..
id name count
-- ---- -----
1 Mike 0
2 Duke 2
3 Smith 1
4 Dave 6
5 Rich 3
6 Rozie 8
7 Romeo 0
8 Khan 1
----------------------
I want to select rows with max(count) limit 5 (TOP 5 Names with maximum count)
that would look sumthing like...
id name count
-- ---- -----
6 Rozie 8
4 Dave 6
5 Rich 3
2 Duke 2
3 Smith 1
please help,,
thanks
Here is how:
MySQL:
SELECT * FROM tableName ORDER BY count DESC LIMIT 5
MS SQL:
SELECT TOP 5 * FROM tableName ORDER BY count DESC
I want to get the movie, character_name, and Visits of the 4 rows(*) having the max "Visits" value group by movie.
My table (D) is like this:
movie character_name Visits
1 Owen Lars 1
1 Obi-Wan Kanobi 2
1 Luke Skywalker 3*
2 Princess Leia 2
2 Luke Skywalker 3*
2 R2-D2 3*
3 Jabba the Hutt 1
3 Han Solo 2
3 Luke Skywalker 4*
The best coding I can think of is
select * FROM D
group by D.movie
HAVING max(Visits)
But i just can't get the right rows >
Can anyone tell me how I should revise it?
thanks so much!!
SELECT
D.*
FROM
D
INNER JOIN
(
SELECT
MOVIE
, MAX(VISITS) AS VISITS
FROM D
GROUP BY MOVIE) F ON D.MOVIE = F.MOVIE AND D.VISITS = F.VISITS