This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Dynamic SELECT TOP #var In SQL Server
why am I getting a syntax error at select top #recNo here?
create procedure getTopAccounts
(
#recNo int
)
as
begin
select top 1 accDesc, accNum
from
(select top #recNo accDesc,accNum
from
ACCOUNTS_TABLE
order by
accNum desc)
as a order by accNum
end
...select top (#recNo) accDesc...
Parametrised TOP needs to be in parenthesis
Related
This question already has answers here:
Rounding to 2 decimal places in SQL
(5 answers)
Closed last month.
SELECT
CONCAT((COUNT_PEOPLE_INSIDE_3MILES/ COUNT_TOTAL_PEOPLE*100),'%') Percentage_People_Within_3miles
FROM
(
SELECT count(*) COUNT_PEOPLE_INSIDE_3MILES
FROM CLIENT_DATA
WHERE (ABS(C_ADD_X) >=1 AND ABS(C_ADD_X) <=3) AND (ABS(C_ADD_Y) >=1 AND ABS(C_ADD_Y) <=3)
),
(
SELECT count(CLIENT_ID) COUNT_TOTAL_PEOPLE
FROM CLIENT_DATA
);
You can use the ROUND() function in MySQL To round to 2 decimal.
SELECT ROUND(your_value, 2);
SELECT ROUND(your_value, 2);
This can be done
This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 6 months ago.
This is the query in one of the Reports that I am trying to fix. What is being done here?
select
*
from
(
SELECT
[dbo].[RegistrationHistory].[AccountNumber],
[dbo].[RegistrationHistory].[LinkedBP],
[dbo].[RegistrationHistory].[SerialNumber],
[dbo].[RegistrationHistory].[StockCode],
[dbo].[RegistrationHistory].[RegistrationDate],
[dbo].[RegistrationHistory].[CoverExpiry],
[dbo].[RegistrationHistory].[LoggedDate] as 'CoverExpiryNew',
ROW_NUMBER() OVER(PARTITION BY [dbo].[RegistrationHistory].[SerialNumber]
ORDER BY
LoggedDate asc) AS seq,
[dbo].[Registration].[StockCode] as 'CurrentStockCode'
FROM
[SID_Repl].[dbo].[RegistrationHistory]
LEFT JOIN
[SID_Repl].[dbo].[Registration]
on [dbo].[RegistrationHistory].[SerialNumber] = [dbo].[Registration].[SerialNumber]
where
[dbo].[RegistrationHistory].[StockCode] in
(
'E4272HL1',
'E4272HL2',
'E4272HL3',
'E4272H3',
'OP45200HA',
'OP45200HM',
'EOP45200HA',
'EOP45200HM',
'4272HL1',
'4272HL2',
'4272HL3',
'4272H3'
)
)
as t
where
t.seq = 1
and CurrentStockCode in
(
'E4272HL1',
'E4272HL2',
'E4272HL3',
'E4272H3',
'OP45200HA',
'OP45200HM',
'EOP45200HA',
'EOP45200HM',
'4272HL1',
'4272HL2',
'4272HL3',
'4272H3'
)
I am looking for a simplified way of splitting this query into step by step, so that I can see where it is going wrong.
ROW_NUMBER in a subquery combined with a filter on it in the outer query is an idiom to filter out all but the first row in a group. So here
ROW_NUMBER() OVER(PARTITION BY [dbo].[RegistrationHistory].[SerialNumber])
Assigns the row with the lowest SerialNumber 1, the next lowest, 2, etc. Then later
where
t.seq = 1
removes all but the row with the lowest serial number from the result.
This question already has answers here:
In Oracle "AS" alias not working
(3 answers)
SQL syntax or database constrictions?
(1 answer)
Closed 4 years ago.
I am not able to select columns as I am getting SQL command not properly ended error
(
select mobile_no,SIM_NO,START_DATE,END_DATE
from (
select mobile_no,SIM_NO,START_DATE,END_DATE
from employee_1
) myemployee
) join (
( select mobile_1.mobile_no,mobile_1.status
from mobile_1
) as mymobile ) on myemployee.mobile_no = mymobile.mobile_no
Thanks in advance. If this is work i will apply it in my project.
This one might be better:
SELECT mobile_no,
sim_no,
start_date,
end_date
FROM (SELECT mobile_no,
sim_no,
start_date,
end_date
FROM employee_1) myemployee
JOIN
(SELECT mobile_1.mobile_no, mobile_1.status FROM mobile_1) mymobile
ON myemployee.mobile_no = mymobile.mobile_no;
[EDIT, based on a_horse's comment]
SELECT e.mobile_no,
e.sim_no,
e.start_date,
e.end_date
FROM employee_1 e JOIN mobile_1 m ON m.mobile_no = e.mobile_no
This question already has an answer here:
Why does "select count(*)" from nothing return 1
(1 answer)
Closed 7 years ago.
for a quick check I used a query
select COUNT(*) LargeTable
and was surprized to see
LargeTable
-----------
1
seconds later I realized my mistake, made it
select COUNT(*) from LargeTable
and got expected result
(No column name)
-----------
1.000.000+
but now I don't understand why COUNT(*) returned 1
it happens if I do select COUNT(*) or declare #x int = COUNT(*); select #x
another case
declare #EmptyTable table ( Value int )
select COUNT(*) from #EmptyTable
returns
(No column name)
-----------
0
I did't find explanation in SQL standard (http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt, online source is given here https://stackoverflow.com/a/8949764/1506454)
why COUNT(*) returns 1?
In SQL Server a SELECT without a FROM clause works as though it operates against a single row table.
This is not standard SQL. Other RDBMSs provide a utility DUAL table with a single row.
So this would be treated effectively the same as
SELECT COUNT(*) AS LargeTable
FROM DUAL
A related Connect Item discussing
SELECT 'test'
WHERE EXISTS (SELECT *)
is https://connect.microsoft.com/SQLServer/feedback/details/671475/select-test-where-exists-select
Because without the FROM clause DBMS cannot know [LargeTable] is a table. You tricked it in guessing it's a COLUMN NAME alias
You can try it and see
select count(*) 'eklmnjdklfgm'
select count(*) eklmnjdklfgm
select count(*) [eklmnjdklfgm]
select count(*)
The first 3 examples returns eklmnjdklfgm as column name
Count(*) returned 1 because your sentence is not of SQL.
1) In the first sentences, you accounts a table empty, with an only row, since you don't put to which table want to access.
2) In the second case:
declare #EmptyTable table ( Value int )
select COUNT(*) from #EmptyTable
returns
(No column name)
-----------
0
You put the variable, but don't determine to which table implement him. For this, do a count and go out 0.
This question already has answers here:
How to find rows in one table that have no corresponding row in another table
(6 answers)
Closed 9 years ago.
I'm trying to get rows from tblReqInfo when ReqID is between 5 and 8 and there is NO corresponding row in tblSomeTable
SELECT *
FROM tblReqInfo
WHERE (RI_ReqID BETWEEN 5 AND 8)
AND
CASE
WHEN NOT EXISTS
(SELECT CC_ReqID
FROM tblSomeTable
WHERE (CC_UserID = #CC_UserID) )
THEN 1
ELSE RI_ReqID NOT IN (SELECT CC_ReqID
FROM tblSomeTable
WHERE (CC_UserID = #CC_UserID) )
END
ORDER BY RI_ReqID
Unfortunately, the query configuration wizard can't even parse this. Can anyone discern what query might work for me?
Try this in SQL Server:
SELECT *
FROM tblReqInfo
WHERE (RI_ReqID BETWEEN 5 AND 8)
AND RI_ReqID NOT IN
(SELECT CC_ReqID
FROM tblSomeTable
WHERE CC_UserID = #CC_UserID)
The standard solution to this problem is to perform an outer join from the target table to the referent table and then grab the rows where there is no match in the referent:
SELECT target.*, referent.id
FROM target LEFT OUTER JOIN
referent ON (target.id = referent.id)
WHERE target.id BETWEEN 5 and 8
AND referent.id IS null
ORDER BY target.id