I am using SQL Server and I have one table generated I just need to create another table from the below generated table that will have the following details:
Price (total price of the repeated UserId)
Number (Unique number with respect to UserId)
UserId (unique)
Please ignore first column it is repeated. Consider it as a one column i.e. only one data of 67.
After a while I have read and got the solution.
It's simple
Select sum(Price) as Price, Number, u.UserId
from the table Group by UserId, Number
Related
We must create a process in Spring Framework that reads a DB2 table by blocks.
However, that table does not have a column with an unique identifier that we can use as a cursor, so in second block we don't from which point we must read.
The table has those columns:
BOOK_ID SOLD_AT QUANTITY
The first one is a foreign key to book model, the second one is a date when a book was sold, and the third one the quantity of books sold.
Is it possible to do SELECT ordering by db2's rowId? Unfortunately, this is legacy code so we cannot create an extra column to the db2.
Thanks in advance.
Try this:
select hex(rowid) rowid, t.name, t.creator
from (
select t.*, rid_bit(t) rowid
from sysibm.systables t
) t
order by rowid
fetch first 10 row only;
rid_bit(table-designator) value for the row may change upon physical row movement (reorg, for example, old row is deleted, new row is inserted into the same physical place, etc.)
I've searched for long time for getting last entered data in a table. But I got same answer.
SELECT TOP 1 CustomerName FROM Customers
ORDER BY CustomerID DESC;
My scenario is, how to get last data if that Customers table is having CustomerName column only? No other columns such as ID or createdDate I entered four names in following order.
James
Arun
Suresh
Bryen
Now I want to select last entered CustomerName, i.e., Bryen. How can I get it..?
If the table is not properly designed (IDENTITY, TIMESTAMP, identifier generated using SEQUENCE etc.), INSERT order is not kept by SQL Server. So, "last" record is meaningless without some criteria to use for ordering.
One possible workaround is if, by chance, records in this table are linked to some other table records (FKs, 1:1 or 1:n connection) and that table has a timestamp or something similar and you can deduct insertion order.
More details about "ordering without criteria" can be found here and here.
; with cte_new as (
select *,row_number() over(order by(select 1000)) as new from tablename
)
select * from cte_new where new=4
How can I show the number of rows in a table in a way that when a new record is added the number representing the row goes higher and when a record is deleted the number gets updated accordingly?
To be more clear,suppose I have a simple table like this :
ID int (primary key) Name varchar(5)
The ID is set to get incremented by itself (using identity specification) so it can't represent the number of row(record) since if I have for example 3 records as:
ID NAME
1 Alex
2 Scott
3 Sara
and I delete Alex and Scott and add a new record it will be:
3 Sara
4 Mina
So basically I'm looking for a sql-side solution for doing this so that I don't change anything else in the source code in multiple places.
I tried to write something to get the job done but it failes. Here it is :
SELECT COUNT(*) AS [row number],Name
FROM dbo.Test
GROUP BY ID, Name
HAVING (ID = ID)
This shows as:
row number Name
1 Alex
1 Scott
1 Sara
while I want it to get shown as:
row number Name
1 Alex
2 Scott
3 Sara
If you just want the number against the rows while selecting the data and not in the database then you can use this
select row_number() over(order by id) from dbo.Test
This will give the row number n for nth row.
Try
SELECT id, name, ROW_NUMBER() OVER (ORDER BY id) AS RowNumber
FROM MyTable
What you want is called an auto increment.
For SQL-Server this is achieved by adding the IDENTITY(1,1) attribute to the table definition.
Other RDBMS use a different syntax. Firebird for example has generators, which do the counting. In a BEFORE-INSERT trigger you would assign the ID-field to the current value of the generator (which will be increased automatically).
I had this exact problem a while ago, but I was using SQL Server 2000, so although row number() is the best solution, in SQL Server 2000, this isn't available. A workaround for this is to create a temporary table, insert all the values with auto increment, and replace the current table with the new table in T-SQL.
Using MS SQL I'm trying the following:
I have one table with game data. In this table a player name can occur many times.
I now want to create a summary table, where every player has a single row.
Using the following statement, I can populate the summary tables "playername" column.
INSERT INTO PLAYER_Summary (Playername)
SELECT DISTINCT [Playername]
FROM ppPLAYER
The summary table also has columns for sums of the players results.
How can i populate my summary table so that each name and the correlating sums are in it once.
I need something like:
INSERT INTO PLAYER_Summary
(Playername, WinWhat )
SELECT DISTINCT [Playername] FROM ppPLAYER , SUM(WinWhat) FROM ppPLAYER
How can I make this work and parse the unique playername with his sum?
Thanks
INSERT INTO PLAYER_Summary
(Playername, WinWhat )
SELECT Playername, SUM(WinWhat)
FROM ppPLAYER
group by Playername
I want to write a trigger to transfer some columns of all inserted rows in a table to another table while incrementing the maximum number in a sequence number field in the destination table. this field is not auto increment but is a primary key field.
What I used to do was find the max sequence no in destination table, increment and then insert the new value. This worked fine if data is inserted row at a time. But when many rows are inserted from a single query, how can I increment the sequence number? Sample problem follows:
insert into [mssql].mssql.dbo.destination_table (name,seq_no)
select name,?
from inserted
even few thousand rows can be inserted at once.
seq_no is part of a composite primary key. So for example if data is inserted under different name seq_no will be different. (This requirement should be considered when I can increment the seq_no without considering its part in the primary key)
Okay, I got your problem, try this
insert into [mssql].mssql.dbo.destination_table (name,seq_no)
select name, x.MaxSeq + row_number() over (order by name)
from inserted, (select Max(seq_no) As MaxSeq From source_table) x