update sql table with increment number [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a table of information in sql, I have added a counter field to it, we want to fill this field based on the item field in order. Of course, the item field is variable and must have its own counter based on each type.
|item|no|
|:---|-:|
|110 | |
|120 | |
|110 | |
|150 | |
After the update
|item|no |
|:---|--:|
|110 | 1 |
|120 | 1 |
|110 | 2 |
|150 | 1 |
ok.

You can use row_number(). Assuming that you have a column that specifies ordering:
select t.*,
row_number() over (partition by item order by <ordering col>) as number
from t;
Note: If you don't care about the ordering, you can use order by item. Some databases allow row_number() without the order by as well.

Related

Find exact match or first bigger number in Access database [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
I have next problem.
There is a table with four columns:
|ID | X | Y | VAL |
|:--|:--:|:--:|----:|
|1 | 1 | 1 | 1110|
|2 | 1 | 2 | 1720|
|3 | 1 | 3 | 2330|
|4 | 1 | 4 | 2940|
|5 | 1 | 5 | 3550|
...
When user enter some value in text field e.g. 2370 i need function to find is there exact match in VAL field and if not to find very first bigger than 2370 (2940) and return ID value.
In some other language I can do it trough dictionaries and so one but in VBA I simply don't have idea.
Any idea or help will be appreciated.
You can use a query to get this answer, using TOP 1 to just return 1 record:
SELECT TOP 1 tblData.ID, tblData.VAL
FROM tblData
WHERE (((tblData.VAL)>=2370))
ORDER BY tblData.VAL ASC;

Take first row after group in SQL Server [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Does anyone know how to take always the first row after group by in SQL Server? Look on the screenshot for better explanation:
Result after select:
+------+---------+------+
| NAME | CAR | AGE |
+------+---------+------+
| Alex | BMW | 5 |
+------+---------+------+
| Alex | Audi | 2 |
+------+---------+------+
| Tom | VW | 10 |
+------+---------+------+
| Tom | Renault | 4 |
+------+---------+------+
| Tom | Peugeot | 2 |
+------+---------+------+
Expected result after group by:
+------+-----+
| NAME | CAR |
+------+-----+
| Alex | BMW |
+------+-----+
| Tom | VW |
+------+-----+
You can try to use the ROW_NUMBER() window function with PARTITION_BY clause. This function assigns a sequential integer to each row within the partition of a result set. The row number starts with 1 for the first row in each partition.
After that, you can use the where clause to select rows that have row numbers as 1.
You can follow this article for a better understanding.
Below is just an example (As I don't know how your query works):
select *
from
(
SELECT
ROW_NUMBER() OVER (PARTITION BY name ORDER BY name) row_num,
*
from(
-- your main group by query
)
)
where row_num=1
you should be able to get the top 1 record for each person using the below query, let me know if this works for you.
SELECT * FROM car_owners GROUP BY person_name;
let me know if you want to order the records in alphabetical order ASC or DESC and then GROUP BY them
Thank You
enter image description here

Best SQL table design [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Which design is better for a table of products with probably more than one name per product?
Using one table or two tables:
One Table:
id | Price|EN name| CH name| JP name|....
---------------------------------------------------
1 | 100| ABC| 中文一| null|...
2 | 200| CDE| null| null|...
..... | ...| ...| ...| ...|...
Two Tables:
id | Price|EN name|
---------------------------
1 | 100| ABC|
2 | 200| CDE|
..... | ...| ...|
id | language| name|
-------------------------------
1 | CH| 中文一|
3 | JP| 東京|
..... | ... | ...|
In general, two tables is considered "better", because it makes it easier to add new languages. I will say that this might apply more to languages that share a common character set.
In many databases, expanding the width of a row (by adding more languages) has an effect on performance as well. Wider rows are more expensive. And adding columns to table with many rows is expensive.
One advantage of a single table is that you can define the character set for each column appropriate to that column, instead of having one generic column for all character sets.
In other words, there is not necessarily a simple answer to your question.
The second way allows you to add extra languages.
Actually you could even put English inside the second table in order to make it more compliant with the Database Normalization:
https://en.wikipedia.org/wiki/Database_normalization

SQL create view with sum query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have following registration table
| EMP_ID | START_DATE | END_DATE | PNUM |
| 1 | 2014-10-20 | 2014-10-25| 10 |
| 2 | 2014-10-20 | 2014-10-30| 30 |
And i want following result in view
| START_DATE | END_DATE | TOTALNUM |
| 2014-10-20 | 2014-10-25| 40 |
| 2014-10-20 | 2014-10-30| 40 |
And i have tried to create view with sum query but no success .
create view EMP
as
select START_DATE ,END_DATE,(select SUM(PNUM) from s) TOTALNUM
from s
group by [START_DATE],END_DATE
Assuming that there is no grouping but just selecting every row and show it's start & end date and the sum of PNUM of all the rows:
SELECT START_DATE, END_DATE, SUM(PNUM) FROM TableX

SQL query asked in interview [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
A table named fruit has the attributes "ID", "Fruit name" and "Basket No":
|ID | Fruit Name | Basket No|
|1 |Apple |1 |
|2 |Banana |2 |
|3 |Orange |1 |
|1 |Apple |2 |
|2 |Banana |3 |
|3 |Orange |2 |
|4 |Mango |2 |
|5 |Grapes |1 |
|1 |Apple |3 |
I was not able to answer these question.
Can someone help me with following questions:
question 1: Find the basket number which have more than 2 fruits.
question 2: Find the basket number which contain orange.
question 3: Find the fruits which are present in more than one basket.
I dont know why people are down voting and closing these question.
question 1:find the basket number which have more than 2 fruits.
SELECT basket_no
FROM baskets
GROUP BY basket_no
HAVING COUNT(*) > 2
question 2:find the basket number which contain orange.
SELECT DISTINCT basket_no
FROM baskets
WHERE fruit_name = 'Orange'
question 3:Find the fruits which are present in more than one basket.
SELECT fruit_name
FROM baskets
GROUP BY fruit_name
HAVING COUNT(*) > 1
While #Mureinik's answer is correct and should be the accepted answer I propose the following extension to Question 3 (which seems designed to catch the unaware out):
question 3: Find the fruits which are present in more than one basket.
For example if there are two Mangoes in basket 2, Mangoes do not meet this requirement and should not be given in the result. I have the following code to take this into account:
select Name
from
(
select BasketId,
Name,
count(id) [Fruit Of This Type Per Basket]
from Fruit
group by BasketId, Name
) as SubQuery
group by Name
having count(1) > 1
This is sql server sql, not sure how standards compliant the subquery is?