selecting last record from table - c#

I am deveoping a c# app in which oledb connection is used.
My table is following
payment
Id RemFee
1 2000
2 2500
1 1500
3 8000
3 5000
2 500
3 0
I want to select and check only last record of each Id and compare Remfee to 0. If it is greater than 0 then print the record. i.e. My expected result is:
Id
1
2
In this check is made for Id 1 with RemFee 1500 (as it is last record with Id1). It is grater than 0 hence record is printed.

Guessing you are using MSQ SQL Server and no row to know wich is your last inserted row. Try this code:
SELECT Id, Min(RemFee) AS RemFee
FROM payment
WHERE RemFee > 0
GROUP BY ID
Here is an SQL Fiddle working code sample
EDIT
If you only want the ID field here is one option, together with the SQL Fiddle code
SELECT myTable.Id
FROM (SELECT Id as ID, Min(RemFee) AS Remfee
FROM payment
WHERE RemFee > 0
GROUP BY ID
) myTable

SELECT ID,MIN(REMFEE) FROM payment WHERE ID NOT IN (SELECT ID FROM payment WHERE REMFEE = 0) GROUP BY ID

Select
id, Min(RemFee)
from payment
where RemFee > 0
group by id

Related

SQL Server : check not existing number into a table

I have a Clients table already populated by thousands of records and now I need to search for a non-existing number in the card number column starting from the number x.
Example: I would like to search for the first available card number starting from number 2000.
Unfortunately I cannot select MAX() as there are records with 9999999 (which is the limit).
Is it possible to do this search through a single SELECT?
It's possible with a few nested SELECTs:
SELECT MIN(`card_number`) + 1 as next_available_number
FROM( SELECT (2000-1) as `card_number`
UNION
SELECT `card_number`
FROM clients
WHERE `card_number` >= 2000
) tmp
WHERE NOT EXISTS ( SELECT NULL
FROM clients
WHERE `card_number` = tmp.`card_number` + 1 )
It can be done with a self-join on your clients table, where you search for the lowest cardnumber for which the cardnumber + 1 does not exist.
In case x is 12, the query would be:
SELECT MIN(cardnumber) + 1
FROM clients
WHERE cardnumber + 1 NOT IN (SELECT cardnumber FROM clients)
AND cardnumber + 1 > 12
Eg. with a dataset of
INSERT INTO clients (cardnumber) VALUES
(1),(2),(3),(4),(5),(6),(7),(8),(9),(11),(12),(13),(14),(15),(17),(18)
this returns 16, but not 10.
Example on SQL Fiddle.
I think this is very similar to this question, but the minimum criteria is new.
If the credit card is represented as integer in your table and your starting number is 2000 you could do something like:
SELECT top 1 (card_id + 1)
FROM CreditCards t
WHERE card_id IN (
SELECT card_id
FROM CreditCards
WHERE card_id LIKE '%[2][0][0][0]%'
)
AND NOT EXISTS (SELECT 1 FROM CreditCards t2 WHERE t2.card_id = t.card_id + 1)
ORDER BY card_id
Example data (Table: CreditCards):
card_id
2000002
2000103
2000000
2000108
2000106
3000201
1000101
Result is: 2000001
Note that %[2][0][0][0]% is fixed here. You could also introduce a parameter.
It is not an optimal solution, but it does the trick.

How to find the column name of maximum value in a row of table in SQL

Hi i want to find the column name of max vaue of a row in a table
ID col1 col2 col3
1 15 12 10
2 6 10 3
3 25 50 100
4 150 80 90
Above is my table structure what i need is to find the max value of each row and then find the column name of that max value
eg In first row max value is 15 now i want to find that max value of first row is 15 and at the same time i want to find the column name of that max value 15 is col1(15 =>col1)
I need result like below one
ID Maxval colname
1 15 col1
2 10 col2
3 100 col3
4 150 col1
After finding the max value and column name i have to insert into another temporary table in the format which is mention above
SELECT ID,
(SELECT MAX(LastUpdateDate)
FROM (VALUES (col1,col2,col3)) AS UpdateDate(LastUpdateDate))
AS LastUpdateDate
FROM TestTable
This is the sql query i used to find out the max value of each row in a table but i donno how to find the column name of max value in each row after that only i can able to insert into temp table. Please any one understand my issue and help me to resolve this .Thanks
In SQL Server, you can use apply:
select t.*, v.*
from t cross apply
(select top (1) v.*
from (values ('col1', col1), ('col2', col2), ('col3', col3)) v(colname, val)
order by val desc
) v
You can use IIF fuinction. The IIF() function returns a value if a condition is TRUE, or another value if a condition is FALSE:
select ID,
IIF(col1>col2 and col1>col3,col1,IIF(col2>col1 and col2>col3,col2,col3))max_val,
IIF(col1>col2 and col1>col3,'col1',IIF(col2>col1 and col2>col3,'col2','col3'))col_nam
from TestTable

i need to get multiple query values to a one data set

i want to create a report from an audit table this is the audit table, i need to get the count of address, tele, and AssName(AssociationName) of each and every person specificly
select count(tele),count(AssName),count(Address) from Audit where name='ben'
select count(tele),count(AssName),count(Address) from Audit where name='nik'
select count(tele),count(AssName),count(Address) from Audit where name='josh'
this is the query i used but i need these individual tables into one table and the count should be calculated if only "1" is in these cells. but my table has "0"s but it consider them as values and counts "0" cells toothis is the tabel now
SELECT
name,
sum(case when tele is not null then 1 else 0 end) tele,
sum(case when Assname is not null then 1 else 0 end) Assname,
sum(case when Address is not null then 1 else 0 end) Address
FROM Audit
GROUP BY name
try a group by instead
SELECT
name,
count(tele),
count(AssName),
count(Address)
FROM Audit
GROUP BY name
Select name , count (Assname), count (Address), count(tele)
from Audit
GROUP by Name
This would return your desired result.
Edit: I misunderstood your question earlier. You need to add group by the name if you want to count the number of each name a person is in the table.
just use GROUP BY
select name, count(tele),count(AssName),count(Address) from Audit group by name

How to join 3 tables in mysql query and save the result

I have 3 table how to get the value of 3rd table by using join in MySql
table1
RowID UserID RoleID
1 1 2
2 171 3
table2
RowID RoleID PermissionID
1 2 2
2 2 3
3 3 14
4 3 15
table3:
PermissionID PermissionName
2 Edit organisation
3 Delete organisation
14 Create group
15 Edit group
16 Delete group
Here I will know only the UserID, if suppose the UserID is 171, then I should get the roleid(3) from table1 and get PermissionID(14,15) from table 2 and then get the PermissionName(Create group, Edit group) from table 3 and I have to store it in a list. How can I do it. I am using c# and mysql. Thanks
SQL Query to select from DB:
SELECT p.PermissionID, p.PermissionName
FROM users u
INNER JOIN roles r ON r.RoleID = u.RoleID
INNER JOIN permissions p ON p.PermissionID = r.PermissionID
Next step: (But use ExecuteReader() instead of ExecuteNonQuery())
MSDN: ExecuteReader() example
Executing an SQL statement in C#?

SQL query to get the sum and the latest enries

I have a table called machineStatus:
ID Successfiles totaldata Backupsessiontime
> 1 3 988 1256637314
> 2 21 323 1256551419
> 3 8 23 1256642968
> 4 94 424 1256642968
> 1 42 324 1256810937
> 1 0 433 1256642968
Now here i want to group by ID where the successfiles and total data gets summed up, but only display the latest Backupsessiontime.
I can do this seperately but not together.
Any suggestions????
For doing this seperately:
to get the sum:
select ID, sum(NumOfSuccessFiles), sum(TotalData)
from MachineStat
group by ID;
to get latest:
With idT as (
select ID
from MachineStat
group by ID
)
select applyT.*
from idT p
CROSS APPLY (
select top 1 ID,BackupSessionTime from MachineStat where eID=p.ID
order by MachineID desc
) as applyT
It looks like you want to do
select ID, sum(NumOfSuccessFiles), sum(TotalData), max(Backupsessiontime)
from MachineStat
group by ID;
You can use an aggregate function that would suit your needs:
select
ID,
sum(NumOfSuccessFiles) TotalNumOfSuccessFiles,
sum(TotalData) TotalData,
max(Backupsessiontime) LastBackupsessiontime
from
MachineStat ms
group by
ID
Or you can use a sub-query in your primary query, if you need more complicated sorting logic.
select
ID,
sum(NumOfSuccessFiles) TotalNumOfSuccessFiles,
sum(TotalData) TotalData,
(select top 1 Backupsessiontime from MachineStat where ID = ms.ID order by ...) LastBackupsessiontime
from
MachineStat ms
group by
ID
Max(BackupSessionTime) ?

Categories