Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
SunnyDee
Yak Posting Veteran
79 Posts |
Posted - 2005-05-15 : 14:54:30
|
Hello SQL Gurus:I'm trying to get results from 3 result sets. The difference between the sets is based on the chrWhiteCodePrecision values. I'm posting the query below. I'm hoping someone can suggest an easier way to accomplish this, as the performance is not great.Thanks for any suggestions.select Export.Extract_FileName as Extract_FileName, sum(Export.Total_Count) as Total_Count, min(left(Convert(char(10),Export.Exported,101),12)) as Exported, min(Exclus.Total_Count) as Bulk_Total_Count, left(Convert(char(10),Exclus.Set_Date,101),12) as Bulk_Set_Date, min(Ram.Total_Count) as Ram_Total_Count, left(Convert(char(10),Ram.Set_Date,101),12) as Ram_Set_Datefrom (Select tbl_WhiteCode.chrExtractFileName as Extract_FileName, count(tbl_WhiteCode.pkWhiteCode) as Total_Count, min(tbl_WhiteCode.dteExportTimeStamp) as Exportedfrom tbl_Gate inner join tbl_WhiteCode on tbl_Gate.fkWhiteCode=tbl_WhiteCode.pkWhiteCode inner join tbl_Report_Key on tbl_Gate.pkGateGuid=tbl_Report_Key.fkGuidKey and tbl_Report_Key.fkReport=@fkReport and intRecycleBin=0 and chrExtractFileName is not nullGroup by tbl_WhiteCode.chrExtractFileName, tbl_WhiteCode.dteExportTimeStamp) as Export left join(Select tbl_WhiteCode.chrExtractFileName as Extract_FileName, count(tbl_WhiteCode.pkWhiteCode) as Total_Count, tbl_WhiteCode.dteImportTimeStamp as Set_Datefrom tbl_Gate inner join tbl_WhiteCode on tbl_Gate.fkWhiteCode=tbl_WhiteCode.pkWhiteCode inner join tbl_Report_Key on tbl_Gate.pkGateGuid=tbl_Report_Key.fkGuidKey Where tbl_Report_Key.fkUser=@fkUser and tbl_Report_Key.fkReport=@fkReport and intRecycleBin=0 and chrExtractFileName is not nulland left(tbl_WhiteCode.chrWhiteCodePrecision,1)='B'Group by tbl_WhiteCode.chrExtractFileName, tbl_WhiteCode.dteImportTimeStamp) as Exclus on Export.Extract_FileName=Exclus.Extract_FileName left join(Select tbl_WhiteCode.chrExtractFileName as Extract_FileName, count(tbl_WhiteCode.pkWhiteCode) as Total_Count, tbl_WhiteCode.dteImportTimeStamp as Set_Datefrom tbl_Gate inner join tbl_WhiteCode on tbl_Gate.fkWhiteCode=tbl_WhiteCode.pkWhiteCode inner join tbl_Report_Key on tbl_Gate.pkGateGuid=tbl_Report_Key.fkGuidKey and tbl_Report_Key.fkReport=@fkReport and intRecycleBin=0 and chrExtractFileName is not nulland left(tbl_WhiteCode.chrWhiteCodePrecision,1)<>'B'Group by tbl_WhiteCode.chrExtractFileName, tbl_WhiteCode.dteImportTimeStamp) as Ram on Export.Extract_FileName=Ram.Extract_FileNamegroup by Export.Extract_FileName, Exclus.Set_Date, Ram.Set_DateOrder by right(Export.Extract_FileName,4), left(Export.Extract_FileName,4) |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-05-15 : 23:37:05
|
This would be much easier if you would include a CREATE TABLE and INSERT statements so we could see what you're doing here. Give us something that we can actually run and look at. Also, use [ c o d e ] and [ / c o d e ] tags (without the spaces) around your post so it formats it correctly. Then it will look something like this:SET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GO/****** Object: Stored Procedure dbo.sp_change_owner_all Script Date: 7/17/2004 9:42:04 AM ******/if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_change_owner_all]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[sp_change_owner_all]GOCREATE PROCEDURE sp_change_owner_all--Name: sp_change_owner_all----Purpose: Change the owner of all objects in a database owned by a given user to another.----Type: Utility----Format: EXEC sp_change_owner_all 'derrick','dbo'----Example_1: EXEC sp_change_owner_all 'derrick','dbo' --Changes owner from common to dbo.----Author: Date: Type: Description:--Derrick Leggett 02/27/2004 Created Created procedure to help in web project. -- Creates a new clone of an existing website.--@txt_old_name VARCHAR(55),@txt_new_name VARCHAR(55)ASDECLARE @objects TABLE( int_id INT PRIMARY KEY IDENTITY(1,1), txt_name VARCHAR(255))INSERT @objects( txt_name) SELECT so.name FROM sysobjects so INNER JOIN sysusers su ON so.uid = su.uid WHERE su.name = @txt_old_name AND type IN ('P','U','V')DECLARE @int_counter INT, @int_max INT, @txt_object VARCHAR(256), @txt_sql VARCHAR(2500)SELECT @int_counter = (SELECT MIN(int_id) FROM @objects), @int_max = (SELECT MAX(int_id) FROM @objects)WHILE @int_counter <= @int_maxBEGINSELECT @txt_object = (SELECT txt_name FROM @objects WHERE int_id = @int_counter)SELECT @txt_sql = 'sp_changeobjectowner ''' + @txt_old_name + '.' + @txt_object + ''',''' + @txt_new_name + ''EXEC(@txt_sql)SELECT @int_counter = @int_counter + 1ENDGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOThat's NOT a good example of how you should code. MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2005-05-16 : 03:56:23
|
You could replace all the three queries with just one, if they are differentiated only by one where condition, using the CASE expression:Select tbl_WhiteCode.chrExtractFileName as Extract_FileName,count(tbl_WhiteCode.pkWhiteCode) as ExportCount,SUM(CASE WHEN left(tbl_WhiteCode.chrWhiteCodePrecision,1)='B' THEN 1 ELSE 0 END) AS BulkCount,SUM(CASE WHEN left(tbl_WhiteCode.chrWhiteCodePrecision,1)<>'B' THEN 1 ELSE 0 END) AS RamCount,tbl_WhiteCode.dteImportTimeStamp as Set_Datefrom tbl_Gateinner join tbl_WhiteCodeon tbl_Gate.fkWhiteCode=tbl_WhiteCode.pkWhiteCodeinner join tbl_Report_Keyon tbl_Gate.pkGateGuid=tbl_Report_Key.fkGuidKeyWhere tbl_Report_Key.fkUser=@fkUserand tbl_Report_Key.fkReport=@fkReport and intRecycleBin=0 and chrExtractFileName is not nullGroup by tbl_WhiteCode.chrExtractFileName,tbl_WhiteCode.dteImportTimeStamp OS |
 |
|
|
SunnyDee
Yak Posting Veteran
79 Posts |
Posted - 2005-05-16 : 18:43:04
|
| Thank you for your response. I was able to use some of what you had suggested, but I'm not sure I can improve on this situation because I am having difficulty incorporating: [tbl_WhiteCode.dteImportTimeStamp as Set_Date] using a CASE Statement.I guess you are saying that I can only utilize the Case Statement with ONE Where condition, and it will not work with two? When I run Query Analyzer, I am taking a big hit on the WHERE Clauses. |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2005-05-17 : 10:50:05
|
| You can have mutiple and nested case statements. What exactly do you need to do with the dates? What is the business logic here? I'm having a bit of difficulty reading it from your query above, so it would help if you could explain what you need.OS |
 |
|
|
|
|
|
|
|