I have the following data:
Input:
----------------------------
| Id | Value|
----------------------------
| 1 |A |
| 1 |B |
| 2 |C |
| 2 |D |
| 2 |E |
| 3 |F |
----------------------------
I need to convert the results to the following:
Output (Count is based on Id)
----------------------------
| Id | Value| Count|
----------------------------
| 1 |A | 2 |
| 1 |B | 2 |
| 2 |C | 3 |
| 2 |D | 3 |
| 2 |E | 3 |
| 3 |F | 1 |
----------------------------
I am using SQL server 2008. Is it possible to write a query to do this?
If yes could anyone help me provide a SQL to obtain the above output from the input data I gave.
You are looking for COUNT OVER:
select id, value, count(*) over (partition by id)
from mytable
order by id, value;
Related
Table Contents are:
|Emp_ID | Name |
|-------|-------|
|1 | xyz |
|23 | pqq |
|22 | wdd |
|12 | fdv |
Here I want the row index where Emp_ID is greater than 15.
Result should return 2 and 3
Hi i need some advice on how to do a select statement on selecting all rows in which the phone number acts as a measure of "distinction".
Example of what i have.
|ID |Name |Phone Number| Address |
| | | | |
|1 |John | 1234567 | A.Road 1 |
|1 |John | 1234567 | B.Road 2 |
|2 |Jane | 7654321 | C.Road 3 |
|3 |Jim | 7654321 | C.road 3 |
Example of what i want:
|ID |Name |Phone Number| Address |
| | | | |
|1 |John | 1234567 | A.Road 1 |
|2 |Jane | 7654321 | C.Road 3 |
Regarding on which of the rows SQL chooses to pic on the result doesn't matter only that the whole row is available and that it makes a selection of distinct phone numbers. Hope you understand what i'm trying to do here.
ANSI SQL supports the row_number() function, which is a typical solution:
select t.*
from (select t.*,
row_number() over (partition by phone_number order by id) as seqnum
from t
) t
where seqnum = 1;
My I/P table is T1:
+----+------+----+------+
| Id | v1 |v2 |v3 |
+----+------+----+------+
| 1 | a |b |c |
| 2 | null |b |null |
| 3 | d |null|null |
| 4 | null |e |null |
| 5 | e |f |null |
+----+------+----+------+
My Requirement : I have to compare one row with the others on the basis of id's.If they have all the values same or null/empty then I have to club the values of id separated by commas.
Required output:
+----+---------------------+
| Id |v1 |v2 |v3 |
+----+---------------------+
| 1,2| a |b |c |
| 3,4| d |e |null |
| 5 | e |f |null |
+----+---------------------+
Please assist.I am trying to use while loop but it is taking me very long.
I want optimize solution as I have run the statement on large record set.
Actually I am stuck in one issue. I have a table:
tbl_color
+------------+
|id | name |
|---|--------|
|1 | Red |
|---|--------|
|2 | Blue |
|---|--------|
|3 | Black |
+------------+
tbl_clothes
+----------------+
|id | name |
| 1 | Pant |
| 2 | Shirt |
| 3 | T-shirt |
+----------------+
tb_sales
+---------------------------------------+
|id | id_cloth | id_color | sales_date |
|---|----------|-----------|------------|
|1 | 1 | 1 | 2016/1/1 |
|---|----------|-----------|------------|
|2 | 1 | 3 | 2016/1/1 |
|---|----------|-----------|------------|
|3 | 1 | 1 | 2016/2/2 |
+---------------------------------------+
So when I change one row of tbl_color to
tbl_color
+---------------------------+
|id | name | modified_on |
|----|--------|-------------|
|1 | Orange | 2016/3/2 |
|----|--------|-------------|
|2 | Blue | 2016/1/2 |
|----|--------|-------------|
|3 | Black | 2016/1/2 |
+---------------------------+
So when I want to get report of sales on 2016/1/1
SELECT * from table tb_sales
JOIN tbl_clothes ON tbl_clothes.id = tbl_sales.id_cloth
JOIN tbl_sales ON tbl_color.id = tbl_sales.id_color
where sales_date = '2016/1/1'
I get the report that have been modified no the original sales
How can I handle this issue?
I have a table named class
|Record_No [PK] | Student_No | Class_No | Seat_No |
=====================================================
|1 | 200910 | 2 | 20 |
|2 | 201234 | 2 | 13 |
|3 | 200965 | 2 | 1 |
|4 | 200920 | 2 | 8 |
|5 | 200911 | 2 | 9 |
|6 | 200955 | 1 | 10 |
|7 | 200924 | 1 | 9 |
|8 | 200922 | 1 | 1 |
|9 | 200901 | 2 | 11 |
|10 | 200902 | 2 | 18 |
is it possible to update the numbers in Seat_No in order from 1 up to the last count of Seat No where its class no is equal to 2?
the table should look like this:
|Record_No [PK] | Student_No | Class_No | Seat_No |
=====================================================
|1 | 200910 | 2 | 1 |
|2 | 201234 | 2 | 2 |
|3 | 200965 | 2 | 3 |
|4 | 200920 | 2 | 4 |
|5 | 200911 | 2 | 5 |
|6 | 200955 | 1 | 10 |
|7 | 200924 | 1 | 9 |
|8 | 200922 | 1 | 1 |
|9 | 200901 | 2 | 6 |
|10 | 200902 | 2 | 7 |
as of now, i can only achieve this by using
UPDATE class SET Seat_No = 1 WHERE Class_No = 2 AND Student_No = 200910;
UPDATE class SET Seat_No = 2 WHERE Class_No = 2 AND Student_No = 201234;
...
and so on..
How can i solve this without putting every student_no in the query? pls help.
update class
set seat_no = s.rn
from (
select
row_number() over(order by record_no) rn,
record_no
from class
where class_no = 2
) s
where class.record_no = s.record_no
If you want to update all classes:
update class
set seat_no = s.rn
from (
select
row_number() over(partition by class_no order by record_no) rn,
record_no
from class
) s
where class.record_no = s.record_no
Assuming your Record_No is 1-5 in your example, you can use it:
UPDATE class SET Seat_No = Record_No WHERE Class_No = 2
If not, it depends on your RDBMS.
Here is the postgresql approach:
UPDATE Class C
SET Seat_No = t.rn
FROM (
SELECT Record_No, ROW_NUMBER() OVER (ORDER BY Record_No) rn
FROM Class
WHERE Class_no = 2
) t
WHERE C.Record_No = t.Record_No
SQL Fiddle Demo