SQL Server :: IF ELSE With Sub Query?
Sep 4, 2010for some reason i am always getting an incorrect syntax error in the following queryI have tried 2 ways to write this query and both failed, Query 1:
[Code]....
Query 2:
[Code]....
for some reason i am always getting an incorrect syntax error in the following queryI have tried 2 ways to write this query and both failed, Query 1:
[Code]....
Query 2:
[Code]....
By right clicking on my database i created a query in server explorer. But where are this query stored can't find them back. I should aspect that their is a folder query's like there is a folder tables but this isn't the case.
View 10 RepliesI am trying to create statitics for game downloads and am having dificulty with a sub part of my query. the goup by command is not having the disired result and i think i have been looking at it too long to see what i am doing wrong. [Code]....
At the moment it is producing each result in many rows when only 4 rows should appear. its the download column that is doing it. needs to be sub queried or something to display total in relation to month,gameid,userid.
Month-download-GameID-UserID
1 1
1 1
1 1
1 1
I am trying to display this query result in an aspx page.
It is very slow in loading. Here is the query. The inner query inside the outer quesry is the problem.(Please see the underlined part in the query) - (If I remove that part it is very fast.)
select
top 500
--This column is the issue
,Governing_Class=( case when exists (select top 1 tqc.class_code from
t_quote_class tqc
inner join t_quote_class_premium tqcm on tqc.class_code =tqcm.
class_code
where tqc.appid=pi.appid and tqc.class_code not in('8742' ,'8810','7380')
order by tqcm.premium_amt desc
)
then ( select top 1 tqc.class_code from
t_quote_class tqc
inner join t_quote_class_premium tqcm on tqc.class_code =tqcm.
class_code
where tqc.appid=pi.
appid
order by tqcm.premium_amt desc
)
......... From tables
I am a biggner in SQL DB . but i started a complicated and painfull work in SQL SERVER 2008. the problem convert Oracle hierarchical query to SQL query. the query
SELECT DISTINCT
LEVEL LVL,
SCH.NSCHEDULE_SL,
SCH.NSCHEDULE_SL_FM,
SCH.CSHED_CNAME
FROM FA_SCHEDULES SCH
WHERE LEVEL = 1
AND NSCHEDULE_SL_FM IS NULL
AND NBRANCH_SL = 2
CONNECT BY PRIOR SCH.NSCHEDULE_SL_FM = SCH.NSCHEDULE_SL
AND NBRANCH_SL = 2
i have tow query in one stored procedure
first query and the second one every of them return only value
i want to copy the result of tow values in a new row in new table
as i want to check on this tow values in ado.net
i want to get only this tow values as a row not first and secon query
only a row has result of first and second query results
[Code]....
How can I SELECT second if first SELECT returns nothing?
View 8 RepliesUsing sql server 2005.
I have an Query A of fields that returns X number of records.
I create a Query B of same fields that returns X-80 number of records.
I want to find out the 80 records that are in Query A but are not in Query B. what is the syntax for this?
Trying to get a query going in SQL server but I get bad/ errors in the query.I have a 2 tables. One is the "Project" table while the other is the "Task" table.Table structure is as follows.
Project table => ProjectID,ProjectName
Task table => TaskID, TaskName,ProjectID,Quarter, Hours
A project can have many task. The ProjectID in Task table is the foreign key which reference the project ID.What I need to do is to SUM all the hours in the Task table and group it by quarter and then group it by ProjectID. Output of the SQL should be in the form of projectID,Quarter, Hours.
I want to transfer the data from one server to another by sql query. I want query. I dont know how to write?
View 2 RepliesI have the following query that uses a linked server that is on a different VLAN of our network:
[Code]....
It works fine in Query Analyzer but when I put it into Visual Web Developer 2008 Express as a SQL Data Source I get an error:
"OLE DB Provider 'MSDASQL' reported an error. Authentication failed."
In order to terminate a SQL Server query within a VB application, we use the Dispose method to close and dispose the database connection. That has been the only way we have found to actually stop a query from running and release the resources.
However, the next time a new connection is created to the same database, we are getting the error "'ObjectDisposedException - Safe Handle has been closed.". It seems like it is always trying to reuse the disposed connection from the connection pool, and it is not reinitializing it. By using a different connection string, we can avoid the error, since now it's not using the connection pool. But there must be a better way to either stop the query or to reinitialize the disposed connection.
is this correct:
select * from tablename where xyz like 'dan%' and like 'bill%'
i want to list entries where xyz is daniel, bill steffen.....
consider the follwing table, this table actually shows which id was given a new id
i.e 11 was assigned 13
again 13 was assigned 15
15 was reassigned 17....and so on actually 11,13,15,17,19 represent the same user (hope u understood)
customerId rejoiningId
11 13
13 15
15 17
20 21
17 19
48 50
21 25
i need to loop through all the rows and find the relation that which all ids are interlinked
say the user enters the rejoining id -13,hence the result should come out to be as 11,13,15,17,19
if user enters 21result should be 20,21,25
it takes a bit of looping statements in query that i couldnt figure out
i have a big query and many data
when i try to execute this query i got this error
how can i run this query complete?
I am new to development and want to know the professional way to deal with dates in SQL Server. In my applications mainly we deal with the DATE datatype and no concern with time part. Also maintaining the format dd/mm/yyyy So for example if I have the table with the following structure.
EmployeeTable
---------------
emp_id int
emp_name varchar(50)
join_date date
and if I want to query "join_date" in between start date and end date and pass the dd/mm/yyyy as stored procedure criteria and want to query.What is the professional way to handle dates? I always convert date in varchar and then do the comparison which I guess is the unprofessional way of doing it.
What's wrong this T-SQL query :
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SQLData As New System.Data.SqlClient.SqlConnection("Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True")
[code]...
I have a requirement in my app to find out if a certain row exists in a table and then if it does get some fields from another row
I have two parameters an EventId and a UserId.
This is the normal statement I would use to get that row
[Code]....
Then I would check if the result > 0. If it is greater than 0 this is the statement I would use to get the other details
[Code]....
So how can I join this into one statement? This is what I have tried
[Code]....
like to get DateFormat "28/07/09 10:29" - "dd/mm/yy hh:mm". My query is below
[Code]....
Which style is fixed this format? Here, time will be shown in 24 hours format.
i am having problems updating my user_tbl and i was wondering if anyone could hely me with the syntax. I am only new to this.
Here is my code :
UPDATE User_tbl
SET User_tbl.user_credit_limit = user_credit_limit - '" +
txtCalender.Text + "'
* product_price
FROM
User_tbl
INNER
JOIN Order_tbl ON
User_tbl.users_id
= Order_tbl.customer_id
INNER
JOIN Order_details_tbl ON
Order_tbl.order_id
= Order_details_tbl.order_id
INNER
JOIN Product_tbl ON
Order_details_tbl.product_id
= Product_tbl.product_id
WHERE
users_id = (select username LIKE '"+ txtUser1.Text.Trim() +"')
[Code]....
The way im reading it, is that the only thing being returned is records that have a task_due_date GREATER than 120 back from today, which would be (2010-03-25 13:39:15.093)Is that a true assumption?
I am trying to produce a DDL box with the following listing from a DB query.
13245
12345-
12346
12348
12348-R
Some 5 digit codes do not include a suffix in the record, the following query will only show the 5 digit codes with a suffix.
SELECT itemCode + '-' + itemSuffix AS code, keyID, itemCode, itemSuffix
FROM item
ORDER BY itemCode
How can I get the codes without a suffix to appear with the codes that have a suffix? The itemSuffix field in the DB is NULL for no data records.
I have a query:
[Code]....
was working well until I rebuild all indexes.After rebuild: The index "IX_Message" is like that:But, in QA, SQL using PK...But when I use query like that:
[Code]....
QA is:I rebuild, reorganize, delete, create again, restart sql, update statistics, etc
How we use a function in a sql query.me some example of using functions in query.Please donot provide me example for using inbuilt sql aggregate functions.Please give some userdefined sql functions example in a query.
View 2 RepliesHow to make sql query run faster . Below is my query:
SELECT [FieldValue2] = case isdate(FieldValue2) When '1' then
case isdate(FieldValue4) When '0' then
dbo.GetCountagingDay(FieldValue2, getdate(), '') -1
else
'0'
end
else
'0'
end
FROM test