|
i am using sql server database
please check the below table two columns say column A and Column B
A B
1 100
1 200
1 300
2 100
2 200
3 100
if the user give 100,200 and 300 i want return 1 ( which has all these values)
if the user give 100 and 200 i have to give 1 and 2. Hope this is clear.... Please help me to build the query
My small attempt...
modified on Thursday, June 4, 2009 7:50 AM
|
|
|
|
|
Hello this time the question is not clear.
Please be more specific.
This is a very poor way of asking your question. Please define the function mapping your input to output more specifically
Tell us the desired output.
Is it
For case 1:
Input 100,200,300
Output:
A B
1 100
1 200
1 300
For Case 2:
I/P: 100 , 200
O/P:
A B
1 100
1 200
2 100
2 200
OR
A B
1 100
2 200 ?
Please from next time whenever you post, give the sample output so that it will be easy to solve.
Also, are you passing a comma delimited value or what?
Nothing clear!:
Niladri Biswas
modified on Sunday, June 7, 2009 2:39 AM
|
|
|
|
|
Anyway
Assuming that
For case 1:
Input 100,200,300
Output:
A B
1 100
1 200
1 300
Solution
SELECT MIN(A), B
FROM tbl_test
WHERE B IN (100, 200, 300)
GROUP BY B;
For Case 2:
I/P: 100 , 200
O/P:
A B
1 100
1 200
2 100
2 200
Solution is
SELECT A, B
FROM tbl_test
WHERE B IN (100, 200)
ORDER BY A, B;
hope this helps.
And vote me
And from next time please define the function mapping your input to output more specifically
Niladri Biswas
|
|
|
|
|
Hello,
When I try to insert unicode chars (Arabic chars) in my database which has its default collation to (latin) these char are inserted incorectly even if I use the nvarchar data type is this because of the collation, can II use the Arabic bin collation to resolve this proplem or ther is an other raison?
Thanks
Dad
|
|
|
|
|
Good Morning All
I have Table Defined as
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[EXP_REL_SLOT_DOMN](
[SLOT] [int] NOT NULL,
[DOMN] [int] NOT NULL,
[PREF] [int] NOT NULL
) ON [PRIMARY]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'references TBL_SLOT_ALLC.ID' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'EXP_REL_SLOT_DOMN', @level2type=N'COLUMN', @level2name=N'SLOT'
indexed as
CREATE UNIQUE CLUSTERED INDEX [EXP_REL_SLOT_DOMN_INDEX] ON [dbo].[EXP_REL_SLOT_DOMN]
(
[SLOT] ASC,
[DOMN] ASC,
[PREF] ASC
)WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = ON, ONLINE = OFF) ON [PRIMARY]
And i have Two insert statements that takes 29 seconds each
--29 seconds
INSERT into tempslot
select distinct sd1.slot as s1, sd2.slot as s2
from [dbo].[EXP_REL_SLOT_DOMN] sd1
inner join [dbo].[EXP_REL_SLOT_DOMN] sd2
on sd1.domn = sd2.domn
and sd1.slot > sd2.slot
--29 seconds
INSERT into tempslot
select distinct sd1.slot as s1, sd2.slot as s2
from [dbo].[EXP_REL_SLOT_DOMN] sd1
inner join [dbo].[EXP_REL_SLOT_DOMN] sd2
on sd1.domn = sd2.domn
and sd1.slot < sd2.slot
How can i improve the Perfomance of this Insert statements
Thank you
|
|
|
|
|
Have you had a look at the execution plan? That should show where the most time is taken. Just in passing, why 2 inserts when 1 will do - unless I'm missing something this has the same effect
INSERT into tempslot
select distinct sd1.slot as s1, sd2.slot as s2
from [dbo].[EXP_REL_SLOT_DOMN] sd1
inner join [dbo].[EXP_REL_SLOT_DOMN] sd2
on sd1.domn = sd2.domn
and sd1.slot != sd2.slot
It may be quicker to use a temp table, take the distinct off this query and do a distinct from the temp table as it may have many less records to process.
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
Good Morning Ashfield
Thank you for your reply.
Ashfield: wrote
why 2 inserts when 1 will do - unless I'm missing something this has the same effect
If i use <> or != it will take 3:47 Seconds. by separating the Query it took less than that and there thing to note is that there is not Unique key in these table.
Thank you
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.somee.com
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
Something I just noticed, it would probably benefit to change your index to put on domn then slot. If you think about it, you are matching on domn and looking for slot within in that domn.
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
And consider telling us three more things;
1. Which version sql server, 2005 or 2008?
2. What is the description of tempslot?
3. What is the result when you run the select statements without the insert portion of the statement?
In other words, do they run faster by quite a bit or the same?
_____________________________
Those who study history are doomed to watch others repeat it. -Scott M.
|
|
|
|
|
Short answer: try removing the index.
Long answer:
How many records are you putting in? That's pretty critical to discussing this. 29 seconds with a billion records is different than 29 seconds with a 1000 records.
You might be better off without the index. I recommend you drop the index and retest your speeds without it.
Realize that indexes are great for lookup but there is a price to pay when you write to a table.
Every time you write to the table, the system has to rebuild the index and since you are using a clustered index you are reordering the data blocks in the same order as the index. So you write, then insert the next line, then reorder, then rewrite the whole mess. That's not exactly efficient.
And because it is a primary key the system will check the uniqueness of each line entered every one of those checks costs you.
Equally important is the fact that you are indexing all columns in one index. You really are just trading disk reads in one place for disk reads in another.
On the other side; when someone runs a select against this table what percentage of the data will be returned on a regular basis? Will it be 5% or less? Or will it be most of the table.
If you aren't going to see 5% or less then having an index is just a waste. Full table scans would be quicker.
With all of that in mind; if you must have the index then see if you can code the situation so that it drops the index, then inserts all the data in one fell swoop, then rebuilds the index.
_____________________________
Those who study history are doomed to watch others repeat it. -Scott M.
|
|
|
|
|
Good Morning smcnulty2000
Thanks for your reply
<b>smcnulty2000 wrote :</b>
Short answer: try removing the index.
Without indexes it run 2 Minutes 10 seconds
Thank you for the Advice.
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.somee.com
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
smcnulty2000 wrote: Short answer: try removing the index.
The index is on the source table, not the destination so its not going to help at all, - if it was the destination then I would agree (in principle) that it may help, but as you sya, how many records?
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
My error. I'm translating from another sql language but catching up.
At least now we know it is using the index.
Vuyiswa Maseko I'm eager to read how many records get processed.
Post the execution plan as well, if you can. That might get us some clues.
_____________________________
Those who study history are doomed to watch others repeat it. -Scott M.
|
|
|
|
|
I have an asp.net application (VB.Net, Reporting Services 2005) which includes a report viewer that displays a client-side report. The report displays normally, but the built-in find feature is not working from the report viewer on my webpage. When the report is displayed and I enter a search string in the find textbox and click Find, the report is redisplayed from the beginning, and does not indicate any "found" text. Maybe I wasn't using the right search criteria, but I couldn't find any reference to a problem concerning this.
The Report Viewer settings from the source are:
<rsweb:ReportViewer ID="IHEEView" runat="server" Width="100%" Height="99.3%" Font-Names="Verdana" Font-Size="8pt" ShowParameterPrompts="False" ShowCredentialPrompts="False" ShowDocumentMapButton="False" BorderColor="DimGray" BorderStyle="Solid" BorderWidth="1px" DocumentMapCollapsed="True" SizeToReportContent="True" AsyncRendering="False" ShowPageNavigationControls="False">
<LocalReport ReportPath="IHEEContractor.rdlc">
</LocalReport>
</rsweb:ReportViewer>
.
I am including Microsoft.ReporViewer.Common.dll, Microsoft.ReportViewer.ProcessingObjectModel.dll, and Microsoft.ReportViewer.WebForms.dll, all Version 8.0.0.0 in the bin folder.
This is due ASAP (isn't it always!?)
Help is greatly appreciated.
|
|
|
|
|
hi everyone,
iam just doing a sample task , of restoring 4 databases Parallely as well as sequentially, through C# code. (all together size of DB's are 2GB)
sequentially restoring database one by one.
Sequetial Total Time: 1 minute 20 Seconds (approx)
1st DB: 5 second
2nd DB: 30 Second
3rd DB: 25 Second
4th DB: 20 Second ( Total 1 munute 20 Second)
Parallel restoring database all atonce. The UI of sqlserver shows Restoring for all the databases simultaneously using C# threading code.
Parallel Total Time: 1 minute 45 Seconds (approx)
1st DB: 50 th second
2nd DB: 1 minute 20 th second
3rd DB: 1 minute 45th second
4th DB: 1 minute 45th second
the total parallel time should have been 30second because that was the max time taken by large database (2nd DB - 620MB approx), but it is not so in above case.
after several iteration the total Parallel time always takes more time than total Sequential time.
the same C# code other than restoring databases in SQL SERVER like (Copying files in code, Deploying IIS through code or any other processes) saves more than half of the time.
When it comes to sql server it takes more time(the same threading code). i need to give more specific reason to my team.
i could understand answer like stress will more on sqlserver ( server resources used more) which is not good.
can Someone tell me like specific reason does sqlserver can have only one active connection at a time ( Is this true or anything like this).
IS parallel restoring possible in Sqlserver, to put it simple.
looking for an urgent reply.
Thanks
Raja
|
|
|
|
|
hi ..
I am creating view in sql server 2000 as-
CREATE VIEW vwSam
As
(select admin.*, cust.FirstName+' '+cust.LastName as CustName
from adminmsgrecieved as admin,custdetails as cust
where admin.AdminId='111' and cust.CustId=admin.FromCustId )
From this i am getting an error invalid object adminmsgrecieved.
But when i run select query only then it works fine.
what problem may be???
and what should i do?
plz help me..
Thanx in adv
|
|
|
|
|
Try it without the parentheses.
_____________________________
Those who study history are doomed to watch others repeat it. -Scott M.
|
|
|
|
|
Have you made a typo? Received is usually spelled with ei after the c.
|
|
|
|
|
Try with "dbo." or "dbname." here dbname is your data base name infront of all data base objects like tables and views
hope it works
Thanks & Regards,
Anil Chelasani
|
|
|
|
|
Dear All, I have following statement to create procedure from an assembly
but i am getting error which says can't find type Assembly name
exec sp_configure 'clr enabled',1 --enable sql to execute CLR
reconfigure --install and run it
go
alter database TestTrigger set trustworthy on --enable Database to access external files
go
use TestTrigger
go
drop assembly WriteToFile
go
declare @dllPath varchar(500)
set @dllPath='C:\Documents and Settings\a.hamidy\My Documents\SharpDevelop Projects\WriteToFile\WriteToFile\bin\Debug\WriteToFile.dll'
create assembly WriteToFile from @dllPath with permission_set = external_access --create assembly from following dll file
go
--now create store procedure to access that dll file class + methods
create proc dbo.writeToFile
(
@filePath varchar(500),
@fileContent varchar(max)
)
as external name [WriteToFile].[WriteToFile].[writeInFile]
The last statement (create proc) gives this error
Msg 6505, Level 16, State 1, Procedure writeToFile, Line 2
Could not find Type 'WriteToFile' in assembly 'WriteToFile'.
Abdul Rahaman Hamidy
Database Developer
Kabul, Afghanistan
|
|
|
|
|
I solved it with this
as external name WriteToFile.[WriteToFile.WriteToFile].writeInFile
but now i am getting this error
Msg 6552, Level 16, State 3, Procedure writeToFile, Line 2
CREATE PROCEDURE for "writeToFile" failed because T-SQL and CLR types for parameter "@filePath" do not match.
let me mention the method paramater
public static void writeInFile(String filePath,string contents)
....
Abdul Rahaman Hamidy
Database Developer
Kabul, Afghanistan
|
|
|
|
|
i have solved again
in store procedure creation i used nvarchar instead of varchar
Abdul Rahaman Hamidy
Database Developer
Kabul, Afghanistan
|
|
|
|
|
Hi
iam working on a task management system. The database is sql server 2005. In my database iam keeping the task details such as TaskName,EstimatedStartTime,EstimatedFinishTime etc. i want to calculate the difference of Estimated start and finish time to get the estimated time required to finish the task. The problem is that there may be holidays(saturday,sunday) in between this dates. And there will be only 8 Hrs for a working day. So how will i calculate the estimated time required to finish the tasks. No matter the format of the estimated time required, it may in minutes,hours or in days. Anybody please do a help. Thanks in advance
|
|
|
|
|
try this
select DATEDIFF(hour,'08:00:00','16:00:00')
Instead of startdate and enddate parameters use your column data from your table.
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
|
|
|
|
|
Here is what I've used in the past ...
Notice that there is a commented out function call reference that allows for custom logic to provide for holidays to be not considered working days. You would have to code the "IsDateAHoliday" function yourself.
-- You can try this out by invoking the statement: select dbo.GetBusinessDays(cast ('10/01/2007' as datetime),cast('10/09/2007' as datetime))
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
ALTER function [dbo].[GetBusinessDays]
(
@StartDate datetime,
@EndDate datetime
)
returns int
as
begin
declare @DaysBetween int
declare @BusinessDays int
declare @Cnt int
declare @EvalDate datetime
select @DaysBetween = 0
select @BusinessDays = 0
select @Cnt=0
select @DaysBetween = datediff(Day,@StartDate,@endDate) + 1
while @Cnt < @DaysBetween
begin
select @EvalDate = @StartDate + @Cnt
--if (dbo.IsDateAHoliday(@EvalDate) = 0)
--BEGIN
if ((datepart(dw,@EvalDate) <> 1) and (datepart(dw,@EvalDate) <> 7))
BEGIN
select @BusinessDays = @BusinessDays + 1
END
--END
select @Cnt = @Cnt + 1
end
return @BusinessDays
end
|
|
|
|
|