i have one Issue in My Query not get Group Record - asp.net

hi i have one issue in get query like 3 column
ID Grp Name
1 10 aa
2 11 bb
3 11 cc
4 11 dd
5 12 ee
6 12 ff
i want ans is
ID Grp Name
1 10 aa
2 11 bb
5 12 ee
so what can id Do ?

In sql server
Try this !
select * from
(
select *,rn=row_number()over(partition by Grp order by ID) from table
)x
where x.rn=1

For SQL Server:
WITH CTE AS (SELECT *,RN=ROW_NUMBER() OVER(PARTITION BY Grp ORDER BY ID)
FROM TableName)
SELECT ID,Grp,Name
FROM CTE
WHERE RN=1
Example in SQL Fiddle.
For MySQL:
SELECT ID,Grp,Name
FROM
(SELECT A.ID, A.Grp, A.Name, count(*) as row_number FROM TableName A
JOIN TableName B ON A.Grp = B.Grp AND A.ID >= B.ID
GROUP BY A.Grp, A.ID,A.Name) T
WHERE row_number=1
Example in SQL Fiddle.

Related

Mariadb Increment counter with ORDER BY

I Have table with description
Table Name : mt_user
column : id , name
Records
id
name
1
Tom
10
Carren
30
Jessy
Then I Query:
select r.id,
#rownum := #rownum + 1 as rownum
from mt_user r
cross join
(SELECT
#rownum := 0
) params
order by r.id desc;
the result is :
id
rownum
30
3
10
2
1
1
But if the query :
select r.id,
#rownum := #rownum + 1 as rownum
from mt_user r
cross join
(SELECT
#rownum := 0
) params
order by r.id asc;
the result is:
id
rownum
1
1
10
2
30
3
the question is, how can I achieved the result like this
id
rownum
1
1
10
2
30
3
but with order by r.id desc
because I trying the query in mariadb version 10.5.9, it have no problem.
it act strange on mariadb version 10.6.10 or 10.9.2

how do i update mulitple rows with dynamic data?

I am using SQLite to update mulipte rows, but it does not work.
item table to keep the item data
item_ID Qty
- -
1 10
2 10
3 10
user_basket table to keep users' basket data
user_ID item_ID Bask_Qty
- - -
1 1 5
1 2 1
2 1 1
I used command like:
UPDATE item
SET Qty =
(SELECT Qty-Bask_Qty FROM user_basket
INNER JOIN item ON item.item_ID = user_basket.item_ID)
WHERE item_ID IN (SELECT item_ID FROM user_basket WHERE user_ID = 1);
After the command, I should expect item table be like that:
item_ID Qty
- -
1 5
2 9
3 10
but instead, I got:
item_ID Qty
- -
1 5
2 5
3 10
apparently, it used the same value to update all the rows.
Use a correlated subquery:
UPDATE item AS i
SET Qty = i.Qty - COALESCE(
(SELECT SUM(b.Bask_Qty) FROM user_basket b WHERE b.user_ID = 1 AND b.item_ID = i.item_ID),
0
)
See the demo.
Or, with a WHERE clause to avoid unnecessary updates:
UPDATE item AS i
SET Qty = i.Qty - (SELECT SUM(b.Bask_Qty) FROM user_basket b WHERE b.user_ID = 1 AND b.item_ID = i.item_ID)
WHERE EXISTS (SELECT 1 FROM user_basket b WHERE b.user_ID = 1 AND b.item_ID = i.item_ID)
See the demo.
If your version of SQLite is 3.33.0+ you can use UPDATE...FROM syntax:
UPDATE item AS i
SET Qty = i.Qty - b.Bask_Qty
FROM (SELECT item_ID, SUM(Bask_Qty) Bask_Qty FROM user_basket WHERE user_id = 1 GROUP BY item_ID) AS b
WHERE b.item_id = i.item_id
In all of the above queries I used SUM(Bask_Qty) to return the quantity of each item from the table user_basket, just in case there are more than 1 rows for each item.
If you are sure that there may be only 1 row for each item then replace it with just Bask_Qty.

error when trying to do a running sum in r with sqldf

I am trying to calculate a running sum in R using sqldf.
I have tried several ways, and I keep getting an this error,
error in statement: near "(": syntax error
I have a pretty simple example dataframe
DF <- data.frame(col1 = 1:4, id = 1:12)
And this is what I am trying to do
install.packages('sqldf')
require(sqldf)
sqldf("SELECT col1, SUM(col1) OVER (ORDER BY id) AS runningsum FROM DF")
I want to get something like this
1) sqlite with the default sqlite backend to sqldf that syntax is not supported but this works:
library(sqldf)
sqldf("select a.*, sum(b.col1) as runningSum
from DF as a
left join DF b on a.id >= b.id
group by a.id")
giving:
col1 id runningSum
1 1 1 1
2 2 2 3
3 3 3 6
4 4 4 10
5 1 5 11
6 2 6 13
7 3 7 16
8 4 8 20
9 1 9 21
10 2 10 23
11 3 11 26
12 4 12 30
2) H2 With the H2 backend we can do this:
library(RH2)
library(sqldf)
sqldf("select *, set(#i, ifnull(#i, 0) + col1) as runningSum from DF")
3) PostgreSQL With the PostgreSQL back end it could be done like this:
library(RPostgreSQL)
library(sqldf)
sqldf('select
*,
sum(col1) over (order by id asc rows between unbounded preceding and current row)
from "DF"')

Updating a column based on conditions

I have a table like:
ID ID2 Name
1 1
2 1
3 2
4 2
5 2
6 3
7 3
8 3
I want to Update the Name column with Values like Name1, name 2 and so on.
This will be based on condition if there are two similar values in ID2 column, for example, the first two rows, then the Name column to be updated with values Name1 and Name2 respectively.Next rows would be Name1, Name2, Name3 respectively and so on. Can someone help me with the logic for this?
ROW_NUMBER with a partition on the ID2 column, ordered by the ID column, should generate the sequences you want. Try this update query:
UPDATE yourTable t1
SET Name = (SELECT 'Name' || t.rn FROM
(
SELECT t2.ID, t2.ID2,
ROW_NUMBER() OVER (PARTITION BY t2.ID2 ORDER BY t2.ID) rn
FROM yourTable t2
WHERE t1.ID = t2.ID AND t1.ID2 = t2.ID2
) t)

SQLite UPDATE Statement

SQLite UPDATE Statement
I have two tables:
Table1
ID Num
1
2
3
4
5
Table2
ID
1
1
2
2
2
3
3
4
4
4
5
I need to UPDATE the Num field in Table1 with occurences of the ID field in Table2 i.e. based on previous:
Table1
ID Num
1 2
2 3
3 2
4 3
5 1
If i run this SQLite statement:
SELECT COUNT(t2.ID) FROM Table1 t1,Table2 t2 WHERE t1.ID=t2.ID GROUP BY t2.ID;
i have the correct table but when i try to UPDATE with that statement:
UPDATE Table1
SET Num=(SELECT COUNT(t2.ID) FROM Table1 t1,Table2 t2 WHERE t1.ID=t2.ID GROUP BY t2.ID);
i have nonsense output.Any ideas?
You must use a correlated subquery to correlate the value returned by the subquery with the current row in the outer query.
This means that you must not use Table1 again in the subquery, but instead refer to the outer table (with the actual name; the UPDATEd table does not support an alias):
UPDATE Table1
SET Num = (SELECT COUNT(t2.ID)
FROM Table2 t2
WHERE Table1.ID=t2.ID
GROUP BY t2.ID);

Resources