I have to do range selection to get some data, and I have this query. Thanks to this question 1 and this question 2
QUERY QUESTION 1
SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY DKUREG ASC) AS NUMBER,
SIMDTA.ACRDKL.*
FROM SIMDTA.ACRDKL
WHERE DKKDCB = 1403 AND DKCOB = 70 AND DKBKTG = 4011 AND DKTHRG = 2019)
AS A WHERE NUMBER > = 1 AND NUMBER < = 100
QUERY QUESTION 2
SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY DKUREG ASC) AS NUMBER,
SIMDTA.ACRDKL.*
FROM SIMDTA.ACRDKL
WHERE DKKDCB = 1403 AND DKCOB = 70 AND DKBKTG = 4011 AND DKTHRG = 2019)
AS A WHERE A.NUMBER BETWEEN 1 AND 100
I've managed to get all the 100 data in DBVisualizer with this query.
But when I paste the code into Visual Studio 2013, I got this
.
Try to run the original SQLs directly in Db2 without the Visual Studio/MsDb2Client. If it works fine, there might be a problem with the Visual Studio/MsDb2Client rewrite component. If so, you may need to contact Microsoft.
Thanks.
Related
I'm trying to execute this query on my c# application, linked with SQL Server.
This is the query:
SELECT DISTINCT TOP(1) pil1.nome, pil1.cognome, pil1.scuderia, pil2.nome, pil2.cognome, pil2.scuderia, p1.punteggio AS punteggio1, p2.punteggio AS punteggio2, (p1.punteggio+p2.punteggio) AS punteggio_tot
FROM pilota pil1, pilota pil2, punti p1, punti p2
WHERE EXISTS
(SELECT *
FROM risultato r1, risultato r2
WHERE r1.pilota < r2.pilota
AND r1.posizione = p1.posizione
AND r2.posizione = p2.posizione
AND r1.data_gran_premio = #date
AND r2.data_gran_premio = #date
AND r1.pilota = pil1.codice
AND r2.pilota = pil2.codice)
AND pil1.scuderia = pil2.scuderia
AND pil1.codice <> pil2.codice
GROUP BY pil1.nome, pil2.nome, pil1.cognome, pil1.scuderia, pil2.cognome, pil2.scuderia, p1.punteggio, p2.punteggio
ORDER BY punteggio_tot ASC
The query is working properly cause I already checked it on workbrench and the output was correct, number and name of columns as well (to check it on workbrench i just changed TOP(1) in LIMIT 1).
But when I execute it on my application the name of the columns are different and the output is incomplete.
WORKBRECH OUTPUT: (correct)
SQL Server/ VISUAL STUDIO OUTPUT:
I am getting this error every-time I run this query on c#.
"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':= 0 + 1 as rnk1 from attendance_table, (select 0 := 0) r1 order by User_ID, V' at line 1"
When I run it on query builder problem does not occur everything is working fine. I have already added this line
cmd.Parameters.AddWithValue("#r1", 0);
cmd.Parameters.AddWithValue("#r2", 0);
can somebody help me with this?
Here's the select command.
select
t1.User_ID,concat(employee_profile.fname,' ',employee_profile.mname,' ',employee_profile.lname) as fullname,
CASE WHEN t1.Verify_State = '0' THEN t1.Verify_Date END AS time_in,
CASE WHEN t2.Verify_State = '1' THEN t2.Verify_Date END AS time_out,round(TIMESTAMPDIFF(minute,t1.Verify_Date,t2.Verify_Date)/60,2) as Total_hours_worked,timeshift.time_in,
timeshift.time_ou
from
(select * , #r1:=#r1+1 as rnk1 from attendance_table , (select #r1:=0) r1 order by User_ID, Verify_Date) as t1 join
(select * , #r2:=#r2+1 as rnk2 from attendance_table , (select #r2:=0) r2 order by User_ID, Verify_Date) as t2
on t1.User_ID=t2.User_ID
and t1.rnk1+1=t2.rnk2
and t1.Verify_State=0
and t2.Verify_State=1
left Join
employee_profile ON employee_profile.emp_id_no = t1.User_ID
left JOIN employee_timeshift ON employee_timeshift.emp_id_no = t1.User_ID
left JOIN timeshift on timeshift.id = employee_timeshift.timeshift_id
left JOIN timeshift_day on timeshift_day.timeshift_id = timeshift.id
where t1.Work_date BETWEEN '2018-04-09' AND '2018-04-14' and t1.Work_time <> t2.Work_time and timeshift_day.day_id = if(DATE_FORMAT(t1.Verify_Date,'%H:%i:%s') BETWEEN '02:00:00' and '06:00:00',(DAYOFWEEK(t1.Verify_Date) -1)-1,DAYOFWEEK(t1.Verify_Date)-1)
order by employee_profile.lname asc, t1.Verify_Date,t2.Verify_Date
With
cmd.Parameters.AddWithValue("#r1", 0);
you're replacing the #r1s in the query with 0 and thus create an assignment to a literal. E.g.
#r1:=#r1+1
becomes
0:=0+1
(assign 0to 0). That's no valid expression as values can only be assigned to variables.
Analog for #r2.
I guess you don't do that replacement, when you run the query directly, so it works there.
As you didn't post what you actually intended to do, I cannot give you any further advise.
I'm working in WinForms application. My requirement is loading the data from sql on demand(i.e load 100 records for a page, when moves to that page). So i tried below SqlCommand but it throws the exception at place of "ROW_NUMBER()" syntax in the below command,
SELECT *
FROM (SELECT *
, ROW_NUMBER() (ORDER BY [ID]) AS RowNum
FROM [tblVGTest]
WHERE [ID]) AS Temp
WHERE RowNum BETWEEN 0 AND 100
Please let me know, is there any mistakes in command or provide any suggestion for my scenario.
Thanks
You were forgetting using OVER() clause with ROW_NUMBER.
Try following Query.
SELECT * FROM (SELECT * , ROW_NUMBER() OVER (ORDER BY [ID]) AS RowNum
FROM [tblVGTest] ) AS Temp WHERE RowNum BETWEEN 0 AND 100
I have removed WHERE clause from it as it was not having any criteria. You can put if it's required for you.
If you use SQL Server Compact 4.0, you can use the OFFSET / FECTCH syntax:
SELECT * FROM TransactionHistory
ORDER BY TransactionDate DESC
OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a complicated query for simple thing, but I have managed to make it to work. The query works when idclient is 1, but when idclient is 5 there is a problem.
Problem is that that client didn't order anything, he just paid some amount. So there isn't a.price, practically whole table is blank, and I want result like -1200,00 or paid amount in minus. My problem is that first part of table does not exist so inner join is impossible, and there for second part is also non existing. Any suggestion for a "quick fix"? :)
SELECT SUM(a.price) - s.pay AS Remain
FROM (SELECT name,
( quantity * itprice ) * ( 100 - percent ) / 100 AS price,
idclient
FROM (SELECT order.iditem AS ID,
item.name,
SUM(order.quant) AS quantity,
order.percent,
item.itprice,
order.idclient
FROM item
inner join order
ON order.iditem = item.id
WHERE ( order.idclient = 1 )
GROUP BY order.iditem,
order.percent,
item.name,
item.itprice,
order.idclient) AS X) AS a
inner join (SELECT SUM(amount) AS Pay,
idcom
FROM payed
WHERE ( idcom = 1 )
GROUP BY idcom) AS s
ON a.idclient = s.idcom
GROUP BY s.idcom,
a.idclient,
s.pay
(there is maybe some typing error in code, but don't bother because I have translated my original code, so maybe some letter is lost in translation. Code is correct)
Is this always just fetching one row? At least it looks like it, and if that's the case you could just use variables with something like this:
declare #price decimal(10,2) = 0, #payment decimal(10,2) = 0
SELECT
#price = SUM(order.quant * item.itprice ) * ( 100 - order.percent ) / 100)
FROM
item
inner join order
ON order.iditem = item.id
WHERE
order.idclient = 1
SELECT
#payment = SUM(amount)
FROM
payed
WHERE
idcom = 1
select #price - #payment
I am trying to building a breadcrumb trail based on a parent/child relationship in a SQL database table. I have done these in the past, but what I need to do now is walk my way up through the hierarchy from the bottom up (backwards).
Here is what my DataTable looks like:
DocID | ParentDocID | Name
7 | 0 | Images (Root)
24 | 7 | JPG (Level 2)
34 | 24 | Size (Level 3)
So this is an N-tiered architecture. I always know what level I am at via a query string variable. I need to loop from the bottom up so that a user can bookmark what level in the tier structure they are at. I am having trouble figuring this out. I cannot use session or viewstate.
Desired Results:
User goes to this link:
Default.aspx?DocId=34 – Then they see the breadcrumb trail as such:
Home > Images > JPG > Size
Default.aspx?DocId=24 – Then they see the breadcrumb trail as such:
Home > Images > JPG
The “Home” element I have hard-coded. I also need each item in the trail to link except for the level you are on.
I would rather this be done in C# code. I also am open to SQL level Stored Procedure options.
Any help would be greatly appreciated. Thank-you in advance!
Here is some SQL I am trying but not getting the results I need. I hope this helps:
WITH Hierachy([DocID], [ParentDocID], [Name], [Level]
AS
(
SELECT [DocID], [ParentDocID], [Name], 0 AS [Level]
FROM [Documents] d
WHERE (d.[ParentDocID] = 7) AND ([Folder] = 1) AND ([Status] = 'A') AND ([AppGroupID] = 4)
UNION ALL
SELECT d.[DocID], d.[ParentDocID], d.[Name], dh.[Level] + 1
FROM [Documents] d
INNER JOIN Hierachy dh
--ON d.ParentDocID = dh.DocID
ON dh.[ParentDocID] = d.[DocID] -- bottom up apporach
WHERE ([Folder] = 1) AND ([Status] = 'A') AND ([AppGroupID] = 4)
)
SELECT [DocID], [ParentDocID], [Name]
FROM Hierachy
--WHERE [Level] > 0
I got it! Here is the SQL that works:
WITH Hierachy([DocID], [ParentDocID], [Name], [Level])
AS
(
SELECT [DocID], [ParentDocID], [Name], 0 AS [Level]
FROM [Documents] d
WHERE (d.[DocID] = #ParentDocID) AND ([Folder] = 1)
AND ([Status] = 'A') AND ([AppGroupID] = #AppGroupID)
UNION ALL
SELECT d.[DocID], d.[ParentDocID], d.[Name], dh.[Level] + 1
FROM [Documents] d
INNER JOIN Hierachy dh
ON dh.[ParentDocID] = d.[DocID]
WHERE ([Folder] = 1)
AND ([Status] = 'A') AND ([AppGroupID] = #AppGroupID)
)
SELECT [DocID], [ParentDocID], [Name]
FROM Hierachy
ORDER BY [Level] DESC
)
Sure you can. You can use CURSOR or WHILE LOOP or XML.
I have an example like you. Here's the WHILE LOOP with a temp Table, it selects parents of a node with a provided ID:
DECLARE #ID INT = 4
DECLARE #Parent_ID INT
DECLARE #Tree TABLE
(
ID int,
Value NVARCHAR(10),
Parent_ID int
)
SELECT #Parent_ID = Parent_ID FROM Tree
WHERE ID = #ID
WHILE #Parent_ID IS NOT NULL
BEGIN
INSERT INTO #Tree
SELECT * FROM Tree
WHERE ID = #Parent_ID
SELECT #Parent_ID = Parent_ID FROM Tree
WHERE ID = #Parent_ID
END
SELECT * FROM #Tree
This describes solving a similar problem, ie walking up a tree. Any inline sql, such as this is best executed within a stored procedure. Solving it in SQL is probably the best approach, as you already have all the data there.
Check below link you will get perfact answer for your question.
SQL Server: How to get all child records given a parent id in a self referencing table