Trying to run this SQL Query and although it says it was executed successfully, it does not return 1 result. Instead, it returns all rows from the database table as it normally does:
SELECT * FROM [Philadelphia].[dbo].[mmgusers_tbl] [WHERE UserName = 'testing' LIMIT 1];
How do I change this to give me only 1 row when executed?
LIMIT 1 is used in mySQL to limit records. In SQL Server, try using the following query:
SELECT TOP 1 * FROM [Philadelphia].[dbo].[mmgusers_tbl] WHERE UserName = 'testing';
Here is what your looking for
SELECT TOP 1 * FROM [Philadelphia].[dbo].[mmgusers_tbl] WHERE UserName = 'testing';
SELECT * FROM [Philadelphia].[dbo].[mmgusers_tbl] WHERE UserName = 'testing'
and only 1 rows matched a UserName of 'testing'
if you just want a single row
SELECT TOP 1 * FROM [Philadelphia].[dbo].[mmgusers_tbl] WHERE UserName = 'testing'
Try this
SELECT Top 1 * FROM [Philadelphia].[dbo].[mmgusers_tbl]
WHERE UserName = 'testing'
(Or)
Select * FROM
(
SELECT *,Row_Number() Over (Order By (Select Null)) AS RN FROM [Philadelphia].[dbo].[mmgusers_tbl] WHERE UserName = 'testing'
) AS T
Where RN = 1
Related
I create an SQL statement to check duplicated records in a table. If the table does not have duplicated records, the count should return zero. The actual result is the count return empty
SELECT COUNT(1) MY_ID_dup_count
FROM mytable
GROUP BY MY_ID
HAVING COUNT(1) >1
Expect Result:
MY_ID_dup_count
0
Actual Result:
MY_ID_dup_count
MS SQL version: Microsoft SQL Server 2017 - 14.0.3381.3 (X64)
The return is 1 record for every MY_ID group of 2 or more. You now want to count these if you want the count of MY_ID rather than the duplicate record count from all the groups. This counts both.
SELECT COUNT(*) as [GROUPS_COUNT], SUM(k.[MY_ID_COUNT]) as [RECORDS_COUNT]
FROM (
SELECT MY_ID, COUNT(*) as [MY_ID_COUNT]
FROM mytable
GROUP BY MY_ID
HAVING COUNT(*) > 1
) k
PS Wrap the SUM with ISNULL if you want 0 when there are no records to sum. (Can't remember if this is needed.)
Something like the following occurs to me:
Count the values without 'having' and then count them with the condition you need
SELECT COUNT(v.MY_ID_dup_count)
FROM(
SELECT COUNT(1) MY_ID_dup_count
FROM mytable
GROUP BY MY_ID
--HAVING COUNT(1) >1
)V
where v.MY_ID_dup_count > 1
You are talking about post-processing the result of your duplicate-check. You could do it this way:
DROP TABLE IF EXISTS #Duplicates;
SELECT MY_ID_dup_count = COUNT(1)
INTO #Duplicates
FROM mytable
GROUP BY MY_ID
HAVING COUNT(1) > 1;
IF ##ROWCOUNT = 0
SELECT MY_ID_dup_count = 0;
ELSE
SELECT * FROM #Duplicates;
I have two sql statements,
select * from UserTable where role='HOD'
select * from UserTable where role='Supervisor'
I want the results to be in a way such that if the first statement returns nothing, I want the second statement to run and if first statement returns something, second statement do not need to run. Is there a way to do it, be it in a stored procedure or a SQLQuery?
Executing two queries is more expensive than executing a single one that returns both result sets. It would be cheaper to filter the results on the client.
Even if you return all results in a single query, you can differentiate the two cases. For example, HOD always comes before Supervisor. You could use a ranking function like ROW_NUMBER() to assign a value to each row, depending on whether it matches HOD or Supervisor:
with a as (
select * ,row_number() over (partition by Role order by Role) as rn
from UserTable
where Role in ('HOD','Supervisor')
)
select *
from a
where rn=1
Another option is to combine a query that returns HOD with a query that returns Supervisor if HOD doesn't exist:
select *
from UserTable
where Role ='HOD'
UNION ALL
select *
from UserTable
where Role ='Supervisor'
AND NOT EXISTS (SELECT 1
from UserTable
where Role ='Supervisor')
The performance of both queries can be improved if Role is part of an index. The first query becomes equivalent to a simple index seek if the table has an index that covers all the returned fields. If the query returns only eg, ID, UserName, Role :
with a as (
select * ,row_number() over (partition by Role order by Role) as rn
from UserTable
where Role in ('HOD','Supervisor')
)
select ID,UserName, Role
from a
where rn=1
and the table has a covering index:
CREATE INDEX IX_UserTable_Roles ON UserTable (Role,ID,UserName)
The resulting execution plan is a single INDEX SEEK on the index
Try This
IF EXISTS (
SELECT 1 FROM UserTable WHERE ROLE = 'HOD'
)
BEGIN
SELECT * FROM UserTable WHERE ROLE = 'HOD'
END
ELSE BEGIN
SELECT * FROM UserTable WHERE ROLE = 'Supervisor'
END
This should do :
IF EXISTS(SELECT 1 FRom UserTable where role='HOD')
Begin
select * from UserTable where role='HOD'
END
ELSE BEGIN
select * from UserTable where role='Supervisor'
END
If you are using Transact SQL then a good page to read about this is https://learn.microsoft.com/en-us/sql/t-sql/language-elements/if-else-transact-sql
In the Transact SQL instance, the general version of what you are looking for is;
IF Boolean_expression
{ sql_statement | statement_block }
[ ELSE
{ sql_statement | statement_block } ]
SELECT * FROM users WHERE uid IN (SELECT doctors FROM MainPage WHERE Valid=1)
users table uid INT
Mainpage table doctors text with value as 1,2,3,4,5
When I am running the above query, it is only resulting 1 row which is for uid = 1, however I wanted all the 5 rows. So in MySQL I used this query:
SELECT *
FROM users
JOIN MainPage ON FIND_IN_SET(uid, doctors)
WHERE Valid = 1;
It worked. I am trying to find an equivalent in SQL Server for FIND_IN_SET to achieve the same result ?
This might work
SELECT *
FROM users u
WHERE EXISTS (
SELECT *
FROM MainPage
WHERE CONCAT(',',doctors,',') LIKE CONCAT('%,',u.uid,',%')
AND Valid = 1
)
Since SQL Server 2016 (13.x) there is STRING_SPLIT()
SELECT *
FROM users
WHERE uid IN
(SELECT value FROM STRING_SPLIT(
(SELECT doctors FROM MainPage WHERE Valid = 1),
','))
I have a table with 12 columns.
I need a query for computing COUNT(*) and selecting all the columns.
I mean I want to have these two queries just in one query:
select *
from mytable
where OneOfTheColumns = something;
select COUNT(*)
from mytable
where OneOfTheColumns = something;
Conditions and tables are the same.
Can I do this?
Thanks a million.
You can use a window function for that
select *,
count(*) over () as total_count
from mytable
where OneOfTheFields = something;
From a table do I want to select the first 4 rows after the first one. I had this in MySQL working as the following:
SELECT * FROM `yp_playlist` LIMIT 1, 4;
I have done some research to see the SQL Server version of this query and came out on the following but this keeps resulting me into an error which keeps me clueless for now.
SELECT id, entry
FROM (
SELECT id, entry, ROW_NUMBER() OVER (ORDER BY id) AS RowNum
FROM playlist
) AS MyDerivedTable
WHERE MyDerivedTable.RowNum BETWEEN 0 AND 10
This is the error:
There was an error parsing the query. [ Token line number = 3, Token line offset = 36, Token in error = OVER ]
With SQL Server Compact 4.0 you can use;
SELECT * FROM [Orders] ORDER BY [Order Date] OFFSET 1 ROWS
FETCH NEXT 4 ROWS ONLY;
SELECT TOP 10 *
FROM ( SELECT id, entry
FROM playlist
ORDER BY id )
one way is to set rowcount
e.g
set rowcount 4
then order your data so you get the ones you want at the top