Top utilising Query in SQL server
Hi,
Is there any query to get the top utilization query of the day?
Hi,
Top CPU utilizing query
--This might take some time to give result on busy systemselect top 10
sum(qs.total_worker_time) as total_cpu_time,
sum(qs.execution_count) as total_execution_count,
count(*) as number_of_statements,
t.text
from
sys.dm_exec_query_stats qs
cross apply sys.dm_exec_sql_text(qs.sql_handle) as t
group by t.text
order by sum(qs.total_worker_time) desc
For memory utilization there is no perfect way to find out if query has completed. but
sys.dm_exec_query_memory_grants would help you
SELECT mg.granted_memory_kb, mg.session_id, t.text, qp.query_plan
FROM sys.dm_exec_query_memory_grants AS mg
CROSS APPLY sys.dm_exec_sql_text(mg.sql_handle) AS t
CROSS APPLY sys.dm_exec_query_plan(mg.plan_handle) AS qp
ORDER BY 1 DESC OPTION (MAXDOP 1)
Please mark this reply as answer if it solved your issue or vote as helpful if it helped so that other forum members can benefit from it.
My TechNet Wiki Articles
Similar Messages
-
Get definition of an index from query in SQL Server
Hi,
I need to get definition of an index using query in SQL Server 2008 as we can get definition of a stored procedure using sp_helptext command.
Thanks In advance,
JiteshI have worked on the script and updated the script as per my need. Now I am able to generate the script for a specific index. Here is the script:-
CREATE PROCEDURE ScriptCreateDropIndexes_SP
@TableName SYSNAME
,@SchemaName SYSNAME = 'dbo'
,@SORT_IN_TEMPDB VARCHAR(3) = 'OFF'
,@DROP_EXISTING VARCHAR(3) = 'OFF'
,@STATISTICS_NORECOMPUTE VARCHAR(3) = 'OFF'
,@ONLINE VARCHAR(3) = 'OFF'
,@Index_Name NVARCHAR(1000)
,@is_Create int = 0
AS
Parameters
@Schemaname - SchemaName to which the table belongs to. Default value 'dbo'.
@Tablename - TableName for which the Indexes need to be scripted.
@SORT_IN_TEMPDB - Runtime value for SORT_IN_TEMPDB option in create index.
Valid Values ON \ OFF. Default = 'OFF'
@DROP_EXISTING - Runtime value for DROP_EXISTING option in create index.
Valid Values ON \ OFF. Default = 'OFF'
@STATISTICS_NORECOMPUTE - Runtime value for STATISTICS_NORECOMPUTE option in create index.
Valid Values ON \ OFF. Default = 'OFF'
@ONLINE - Runtime value for ONLINE option in create index.
Valid Values ON \ OFF. Default = 'OFF'
SET NOCOUNT ON
IF @SORT_IN_TEMPDB NOT IN ('ON','OFF')
BEGIN
RAISERROR('Valid value for @SORT_IN_TEMPDB is ON \ OFF',16,1)
RETURN
END
IF @DROP_EXISTING NOT IN ('ON','OFF')
BEGIN
RAISERROR('Valid value for @DROP_EXISTING is ON \ OFF',16,1)
RETURN
END
IF @STATISTICS_NORECOMPUTE NOT IN ('ON','OFF')
BEGIN
RAISERROR('Valid value for @STATISTICS_NORECOMPUTE is ON \ OFF',16,1)
RETURN
END
IF @ONLINE NOT IN ('ON','OFF')
BEGIN
RAISERROR('Valid value for @ONLINE is ON \ OFF',16,1)
RETURN
END
DECLARE @IDXTable TABLE
Schema_ID INT
,Object_ID INT
,Index_ID INT
,SchemaName SYSNAME
,TableName SYSNAME
,IndexName SYSNAME
,IsPrimaryKey BIT
,IndexType INT
,CreateScript VARCHAR(MAX) NULL
,DropScript VARCHAR(MAX) NULL
,ExistsScript VARCHAR(MAX) NULL
,Processed BIT NULL
INSERT INTO @IDXTable
Schema_ID
,Object_ID
,Index_ID
,SchemaName
,TableName
,IndexName
,IsPrimaryKey
,IndexType
SELECT ST.Schema_id
,ST.Object_id
,SI.Index_id
,SCH.Name
,ST.Name
,SI.Name
,SI.is_primary_key
,SI.Type
FROM SYS.INDEXES SI
JOIN SYS.TABLES ST
ON SI.Object_ID = ST.Object_ID
JOIN SYS.SCHEMAS SCH
ON SCH.schema_id = ST.schema_id
WHERE SCH.Name = 'dbo'
AND ST.Name = 'group_master'
AND SI.name = 'uq_group_master__parent_id'
AND SI.Type IN (1,2,3)
DECLARE @SchemaID INT
DECLARE @TableID INT
DECLARE @IndexID INT
DECLARE @isPrimaryKey BIT
DECLARE @IndexType INT
DECLARE @CreateSQL VARCHAR(MAX)
DECLARE @IndexColsSQL VARCHAR(MAX)
DECLARE @WithSQL VARCHAR(MAX)
DECLARE @IncludeSQL VARCHAR(MAX)
DECLARE @WhereSQL VARCHAR(MAX)
DECLARE @SQL VARCHAR(MAX)
DECLARE @DropSQL VARCHAR(MAX)
DECLARE @ExistsSQL VARCHAR(MAX)
DECLARE @IndexName SYSNAME
DECLARE @TblSchemaName SYSNAME
SELECT @TblSchemaName = QUOTENAME(@Schemaname) + '.' + QUOTENAME(@TableName)
SELECT @CreateSQL = ''
SELECT @IndexColsSQL = ''
SELECT @WithSQL = ''
SELECT @IncludeSQL = ''
SELECT @WhereSQL = ''
WHILE EXISTS(SELECT 1
FROM @IDXTable
WHERE CreateScript IS NULL)
BEGIN
SELECT TOP 1 @SchemaID = Schema_ID
,@TableID = Object_ID
,@IndexID = Index_ID
,@isPrimaryKey = IsPrimaryKey
,@IndexName = IndexName
,@IndexType = IndexType
FROM @IDXTable
WHERE CreateScript IS NULL
AND SchemaName = @SchemaName
AND TableName = @TableName
ORDER BY Index_ID
IF @isPrimaryKey = 1
BEGIN
SELECT @ExistsSQL = ' EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N''' + @TblSchemaName + ''') AND name = N''' + @IndexName + ''')'
SELECT @DropSQL = ' ALTER TABLE '+ @TblSchemaName + ' DROP CONSTRAINT [' + @IndexName + ']'
END
ELSE
BEGIN
SELECT @ExistsSQL = ' EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N''' + @TblSchemaName + ''') AND name = N''' + @IndexName + ''')'
SELECT @DropSQL = ' DROP INDEX [' + @IndexName + '] ON ' + @TblSchemaName
END
IF @IndexType IN (1,2)
BEGIN
SELECT @CreateSQL = CASE
WHEN SI.is_Primary_Key = 1 THEN
'ALTER TABLE ' + @TblSchemaName
+ ' ADD CONSTRAINT [' + @IndexName + '] PRIMARY KEY ' + SI.type_desc
WHEN SI.Type IN (1,2) THEN
' CREATE ' + CASE SI.is_Unique
WHEN 1 THEN ' UNIQUE ' ELSE '' END + SI.type_desc + ' INDEX ' + QUOTENAME(SI.Name) + ' ON ' + @TblSchemaName
END
,@IndexColsSQL = ( SELECT SC.Name + ' '
+ CASE SIC.is_descending_key
WHEN 0 THEN ' ASC '
ELSE 'DESC'
END + ','
FROM SYS.INDEX_COLUMNS SIC
JOIN SYS.COLUMNS SC
ON SIC.Object_ID = SC.Object_ID
AND SIC.Column_ID = SC.Column_ID
WHERE SIC.OBJECT_ID = SI.Object_ID
AND SIC.Index_ID = SI.Index_ID
AND SIC.is_included_column = 0
ORDER BY SIC.Key_Ordinal
FOR XML PATH('')
,@WithSQL =' WITH (PAD_INDEX = ' + CASE SI.is_padded WHEN 1 THEN 'ON' ELSE 'OFF' END + ',' + CHAR(13) +
' IGNORE_DUP_KEY = ' + CASE SI.ignore_dup_key WHEN 1
THEN 'ON' ELSE 'OFF' END + ',' + CHAR(13) +
' ALLOW_ROW_LOCKS = ' + CASE SI.Allow_Row_Locks WHEN
1 THEN 'ON' ELSE 'OFF' END + ',' + CHAR(13) +
' ALLOW_PAGE_LOCKS = ' + CASE SI.Allow_Page_Locks WHEN
1 THEN 'ON' ELSE 'OFF' END + ',' + CHAR(13) +
CASE SI.Type WHEN 2 THEN 'SORT_IN_TEMPDB = ' + @SORT_IN_TEMPDB
+',DROP_EXISTING = ' + @DROP_EXISTING + ',' ELSE '' END +
CASE WHEN SI.Fill_Factor > 0 THEN ' FILLFACTOR =
' + CONVERT(VARCHAR(3),SI.Fill_Factor) + ',' ELSE '' END +
' STATISTICS_NORECOMPUTE = ' + @STATISTICS_NORECOMPUTE
+ ', SORT_IN_TEMPDB = ' + @SORT_IN_TEMPDB +
', ONLINE = ' + @ONLINE + ') ON ' + QUOTENAME(SFG.Name)
,@IncludeSQL = ( SELECT QUOTENAME(SC.Name) + ','
FROM SYS.INDEX_COLUMNS SIC
JOIN SYS.COLUMNS SC
ON SIC.Object_ID = SC.Object_ID
AND SIC.Column_ID = SC.Column_ID
WHERE SIC.OBJECT_ID
= SI.Object_ID
AND SIC.Index_ID = SI.Index_ID
AND SIC.is_included_column = 1
ORDER BY SIC.Key_Ordinal
FOR
XML PATH('')
,@WhereSQL = SI.Filter_Definition
FROM SYS.Indexes SI
JOIN SYS.FileGroups SFG
ON SI.Data_Space_ID =SFG.Data_Space_ID
WHERE Object_ID = @TableID
AND Index_ID = @IndexID
SELECT @IndexColsSQL = '(' + SUBSTRING(@IndexColsSQL,1,LEN(@IndexColsSQL)-1) + ')'
IF LTRIM(RTRIM(@IncludeSQL)) <> ''
SELECT @IncludeSQL = ' INCLUDE (' + SUBSTRING(@IncludeSQL,1,LEN(@IncludeSQL)-1) + ')'
IF LTRIM(RTRIM(@WhereSQL)) <> ''
SELECT @WhereSQL = ' WHERE (' + @WhereSQL + ')'
END
IF @IndexType = 3
BEGIN
SELECT @CreateSQL = ' CREATE ' + CASE
WHEN SI.Using_xml_index_id IS NULL THEN ' PRIMARY '
ELSE '' END
+ SI.type_desc + ' INDEX ' + QUOTENAME(SI.Name) + ' ON ' + @TblSchemaName
,@IndexColsSQL = ( SELECT SC.Name + ' '
+ ','
FROM SYS.INDEX_COLUMNS SIC
JOIN SYS.COLUMNS SC
ON SIC.Object_ID = SC.Object_ID
AND SIC.Column_ID = SC.Column_ID
WHERE SIC.OBJECT_ID = SI.Object_ID
AND SIC.Index_ID = SI.Index_ID
AND SIC.is_included_column = 0
ORDER BY SIC.Key_Ordinal
FOR XML PATH('')
,@WithSQL =' WITH (PAD_INDEX = ' + CASE SI.is_padded WHEN 1 THEN 'ON' ELSE 'OFF' END + ',' + CHAR(13) +
' ALLOW_ROW_LOCKS = ' + CASE SI.Allow_Row_Locks WHEN
1 THEN 'ON' ELSE 'OFF' END + ',' + CHAR(13) +
' ALLOW_PAGE_LOCKS = ' + CASE SI.Allow_Page_Locks WHEN
1 THEN 'ON' ELSE 'OFF' END + ',' + CHAR(13) +
CASE SI.Type WHEN 2 THEN 'SORT_IN_TEMPDB = OFF,DROP_EXISTING
= OFF,' ELSE '' END +
CASE WHEN SI.Fill_Factor > 0 THEN ' FILLFACTOR =
' + CONVERT(VARCHAR(3),SI.Fill_Factor) + ',' ELSE '' END +
'SORT_IN_TEMPDB = OFF ' + ') '
,@IncludeSQL = ' USING XML INDEX [' + (SELECT Name
FROM SYS.XML_Indexes SIP
WHERE SIP.Object_ID = SI.Object_ID
AND SIP.Index_ID = SI.Using_XML_Index_ID) + '] FOR PATH '
FROM SYS.XML_Indexes SI
WHERE SI.Object_ID = @TableID
AND SI.Index_ID = @IndexID
SELECT @IndexColsSQL = '(' + SUBSTRING(@IndexColsSQL,1,LEN(@IndexColsSQL)-1) + ')'
END
SELECT @CreateSQL = @CreateSQL
+ @IndexColsSQL + CASE WHEN @IndexColsSQL <> '' THEN CHAR(13) ELSE ''
END
+ ISNULL(@IncludeSQL,'') + CASE WHEN @IncludeSQL <> '' THEN CHAR(13) ELSE
'' END
+ ISNULL(@WhereSQL,'') + CASE WHEN @WhereSQL <> '' THEN CHAR(13) ELSE
'' END
--+ @WithSQL
UPDATE @IDXTable
SET CreateScript = @CreateSQL
,DropScript = @DropSQL
,ExistsScript = @ExistsSQL
WHERE Schema_ID = @SchemaID
AND Object_ID = @TableID
AND Index_ID = @IndexID
END
-- PRINT REPLICATE('-',100)
-- PRINT 'DROP Indexes'
-- PRINT REPLICATE('-',100)
if @is_Create = 0
begin
UPDATE @IDXTable
SET Processed = 0
WHERE SchemaName = @SchemaName
AND TableName = @TableName
WHILE EXISTS(SELECT 1
FROM @IDXTable
WHERE ISNULL(Processed,0) = 0
AND SchemaName = @SchemaName
AND TableName = @TableName )
BEGIN
SELECT @SQL = ''
SELECT TOP 1 @SchemaID = Schema_ID
,@TableID = Object_ID
,@IndexID = Index_ID
,@SQL = 'IF ' + ExistsScript + CHAR(13) + DropScript + CHAR(13)
FROM @IDXTable
WHERE ISNULL(Processed,0) = 0
AND SchemaName = @SchemaName
AND TableName = @TableName
ORDER BY IndexType DESC,Index_id DESC
PRINT @sql
UPDATE @IDXTable
SET Processed = 1
WHERE Schema_ID = @SchemaID
AND Object_ID = @TableID
AND Index_ID = @IndexID
END
end
--PRINT REPLICATE('-',100)
--PRINT 'Create Indexes'
-- PRINT REPLICATE('-',100)
if @is_Create = 1
begin
UPDATE @IDXTable
SET Processed = 0
WHERE SchemaName = @SchemaName
AND TableName = @TableName
WHILE EXISTS(SELECT 1
FROM @IDXTable
WHERE ISNULL(Processed,0) = 0
AND SchemaName = @SchemaName
AND TableName = @TableName )
BEGIN
SELECT @SQL = ''
SELECT TOP 1 @SchemaID = Schema_ID
,@TableID = Object_ID
,@IndexID = Index_ID
,@SQL = 'IF NOT ' + ExistsScript + CHAR(13) + CreateScript + CHAR(13)
FROM @IDXTable
WHERE ISNULL(Processed,0) = 0
AND SchemaName = @SchemaName
AND TableName = @TableName
ORDER BY IndexType DESC,Index_id DESC
PRINT @sql
UPDATE @IDXTable
SET Processed = 1
WHERE Schema_ID = @SchemaID
AND Object_ID = @TableID
AND Index_ID = @IndexID
END
end -
Different output of same query in SQL Server and Oracle
I have two tables table1 and table2
--table1 has two columns c1 int and c2 varchar. there are not constraints added in it. it has data as given below
c1 c2
6 d
5 j
102 g
4 g
103 f
3 h
501 j
1 g
601 n
2 m
--table2 has only one column c1 int. there are not constraints added in it. it has data as given below
c1
6
1
4
3
2
now when i run below given query in sql server and oracle it gives me different result
select *
from table1
inner join (SELECT ROW_NUMBER() OVER (order by c1 ASC) AS c1 from table2) table2 on table2.c1=table1.c1
sql server output
c1 c2 c1
1 g 1
2 m 2
3 h 3
4 g 4
5 j 5
oracle output
C1 C2 C1
5 j 5
4 g 4
3 h 3
1 g 1
2 m 2
If you notice the first column in both output. It is sorted in sql server and not in oracle.
Why it is behaving differently in oracle? Is there any way I can solve this in oracle?
Thanks,
JigsIt is NOT behaving "differently" in Oracle; you just haven't specified an order that you expect your results to be in, so you're going to get output in whatever order the database fancies displaying it (ie. no guarenteed order). This is an artifact of how the database chooses to put together the data, and different databases (or even datasets within the same database) can and most likely will behave differently.
Even SQL Server won't guarentee to always get your data in an ordered fashion if you exclude the order by clause, even if you think it has always output the data in an ordered fashion.
Your solution is to add an order by clause, in BOTH databases, to force the order of the output data. -
Help with translating SQL query from SQL Server syntax to Oracle syntax
Hi,
is it someone that can help me translate following SQL query from SQL Server syntax to Oracle syntax.
SELECT ID,
[LMT(MTR)] = MAX(case when TYPE = 'LMT' then VALUE end),
[AAD(KGM)] = MAX(case when TYPE = 'AAD' then VALUE end),
[VOL(MTQ)] = MAX(case when TYPE = 'VOL' then VALUE end)
FROM yourtable
GROUP BY ID
Your help is highly appreciated, thanks in advance.Like this,
SELECT ID,
MAX(case when TYPE = 'LMT' then VALUE end) LMT_MTR,
MAX(case when TYPE = 'AAD' then VALUE end) AAD_KGM ,
MAX(case when TYPE = 'VOL' then VALUE end) VOL_MTQ
FROM yourtable
GROUP BY ID-Arun -
Converting MDX query into SQL Server 2008
We have MDX query based OLAP CUbes.
To reduce MDX based dependenies, we have to convert MDX based cube to sql server 2008 based queries.
For this I need expert advise to convert below query to sql server 2008 based query :
CREATE MEMBER CURRENTCUBE.Measures.[Ack Lost]
AS 'Sum(PeriodsToDate([Time].[Year], [Time].CurrentMember ), [Measures].[Lost])',
FORMAT_STRING = "#,#",
VISIBLE = 1;Hi Sachin,
According to your description, you need to convert the MDX query to T-SQL query, right?
Your MDX query is calculated measure that return the total value from the first sibling and ending with the given member. In T-SQL query we can use the query as Yogisha provided to achieve the same requirement. Now you need to a tool to convert all the MDX
related SSAS cube queries to MS SQL Server 2008 based queries.
Although MDX has some similarities with T-SQL these languages are in many ways different. Beginning to learn and comprehend SQL Server Analysis Services (SSAS) MDX queries can be difficult after one has spent years writing queries in T-SQL. Currently, there
is no such a tool to convert a MDX query to T-SQL query since the structure difference between MDX and T-SQL. So you need to convert them manually, please refer to the links below to see the details.
http://www.mssqltips.com/sqlservertip/2916/comparison-of-queries-written-in-tsql-and-sql-server-mdx/
https://sqlmate.wordpress.com/2013/11/12/t-sql-vs-mdx-2/
http://technet.microsoft.com/en-us/library/aa216779(v=sql.80).aspx
Regards,
Charlie Liao
TechNet Community Support -
Exam 70-461 : Querying Microsoft SQL Server 2012
Hello friends,
I would like to know whether i can get previous exam questions&answers or not ? Do you offer any website for practising to that exam ?Hi, coskun.arif.
What do you mean by asking for previous exam questions? I want to help but Microsoft never shares their exam questions/answers - even for exams no longer offered.
Below, I have included some resources you can use to help prepare for this exam.
The 70-461 exam is described here:
http://www.microsoft.com/learning/en-us/exam-70-461.aspx
You can take the free training at Microsoft Virtual Academy (MVA) with review questions for the 70-461 exam here:
http://www.microsoftvirtualacademy.com/training-courses/querying-microsoft-sql-server-2012-databases-jump-start
Here is a book on Amazon.com for studying for this exam:
http://www.amazon.com/Training-Kit-Exam-70-461-Microsoft/dp/0735666059
MeasureUp sells a practice exam for 70-461 here:
http://www.measureup.com/70-461-Querying-Microsoft-SQL-Server-2012-P4155.aspx
Transcender also sells a 70-461 practice exam here:
https://www.transcender.com/practice-exam/microsoft/70-461.kap
Good luck!
Best wishes, Davin Mickelson -
Measuring the cpu and ram consumption of a given query in sql server
hello there how you doing and am new to this sql server forum and i suddenly popped to ask questions related to sql server
and my question goes like this
in sql server i have table called testt and it have more thna10000000 records
and i have written a query select * from testt limit 9000000; and what i want is to measure the
CPU consumption of this given query=???????????????
and
RAM consumption of this given query=???????????????
Time consumption of this given query=?????????????? this i will get it from the result bar but i don't know whether it is correct or not
any help on this it may be query, tool or configurations all are acceptable and appreciated
10Q for letting me ask questions and feeling freely.Please check these queries and this may helps you to get whats happening on with CPU currently..
SELECT getdate() as "RunTime", st.text, qp.query_plan, a.* FROM sys.dm_exec_requests a CROSS APPLY sys.dm_exec_sql_text(a.sql_handle) as st CROSS APPLY sys.dm_exec_query_plan(a.plan_handle) as qp order by CPU_time desc
Top 50 CPU usage :
SELECT TOP 50 st.text
,st.dbid
,st.objectid
,qs.total_worker_time
,qs.last_worker_time
,qp.query_plan
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
ORDER BY qs.total_worker_time DESC
SP_Whoisactive --- download from : http://www.brentozar.com/responder/log-sp_whoisactive-to-a-table/
Activity Monitor and sp_who2 active also will give some idea...
Reference links...
http://channel9.msdn.com/posts/SQL-server-High-CPU--Part-1
http://channel9.msdn.com/posts/Troubleshooting-SQL-server-High-CPU--Part-2
http://jeffstevenson.karamazovgroup.com/2008/09/identifying-high-cpu-sql-processes.html
http://searchsqlserver.techtarget.com/tutorial/Step-1-CPU-usage
================
Execute this script and and start doing your analysis in your server so that you will get more idea how its working and all the details...
Sample CPU Test : Dont try this in production and try this in your test or Dev server..
-- Query to Keep CPU Busy for 2 0 Seconds
DECLARE @T DATETIME, @F BIGINT;
SET @T = GETDATE();
WHILE DATEADD(SECOND,20,@T)>GETDATE()
SET @F=POWER(2,30);
You can easily change the parameter in the DATEADD and can make it run for 50 seconds.
-- Query to Keep CPU Busy for 50 Seconds
DECLARE @T DATETIME, @F BIGINT;
SET @T = GETDATE();
WHILE DATEADD(SECOND,50,@T)>GETDATE()
SET @F=POWER(2,30);
Raju Rasagounder MSSQL DBA -
Using "Top" keyword in Microsoft SQL Server Gateway?
I want to query the top 5 records in sqlserver. Such as select top 5 * from table1
But, oracle doesn't support "top"keyword and
Gateway doesn't support rowid. Is there a solution?
ThanksThere is indeed no direct gateway support for this SQL Server specific 'top' function. However with the use of the DBMS_HS_PASSTHROUGH it should be possible to use the top-function. The DBMS_HS_PASSTHROUGH package allows you to move a SQL-statement 'as-is' to the remote non-Oracle database.
Have a look at an example of the DBMS_HS_PASSTHROUGH usage (with a resultset) at the MetaLink document with DocID: 412491.1
Regards,
Ed -
Hi All
below query giving me error in query generator but working well in Sql server.
error: [Microsoft][SQL Server Native Client 10.0][SQL Server]Must specify table to select from. '' (SWEI)
Select
T1.U_grp01 As 'BA',T3.DocDate As 'Posting Date',Month(T3.DocDate) As 'PostMonth',Year(T3.DocDate) As 'PostYear',
'AR Invoice' As 'Type',T3.DocNum As 'Doc No',T3.CardCode As 'Cust. Code',T3.CardName As 'Cust. Name',T5.SlpName As 'Sale Emp. Name',
T4.IndustryC As 'Channel Type',T6.CityB As 'BillToCity',T7.Name As 'BillToState',T6.CityS As 'ShipToCity',T8.Name As 'ShipToState',
T4.U_Une_Zone As 'Zone',
T2.ItmsGrpNam As 'L1',T0.LineNum As 'Row No',T0.ItemCode As 'ItemCode',T0.Dscription As 'Item Name',t0.whscode,
T0.Quantity As 'Quantity',T0.StockPrice As 'COGS Price',IsNull(Sum(T0.Quantity * T0.StockPrice),0) As 'COGS Value',T0.VatSum As 'Tax Amount',
IsNull((Case When T3.DocType='I' Then (Case When T0.Currency = 'INR' Then T0.PriceBefDi Else (T0.PriceBefDi * T0.Rate) End) Else T0.Price End) ,0) As 'Sales Price',
IsNull((Case When T3.DocType='I' Then (Case When T0.Currency = 'INR' Then T0.INMPrice Else (T0.INMPrice * T0.Rate) End) Else T0.Price End) ,0) As 'Sales Price',
IsNull((Case When T3.DocType='I' Then (Case When T0.Currency = 'INR' Then Sum(T0.Quantity * T0.INMPrice) Else Sum(T0.Quantity * T0.INMPrice * T0.Rate) End) Else T0.LineTotal End) ,0) As 'Sales Value',t9.linetotal as 'Freight',
T3.DocType As 'DocType',
(SELECT DISTINCT ISNULL (SUM(INV4.TaxSum),0)
FROM INV4
WHERE INV4.StaType = -90
AND INV4.DocEntry = T3.DocEntry
AND INV4.LineNum = T0.LineNum) AS 'BED',
(SELECT DISTINCT ISNULL (SUM(INV4.TaxSum),0)
FROM INV4
WHERE INV4.StaType = -60
AND INV4.DocEntry = T3.DocEntry
AND INV4.LineNum = T0.LineNum) AS 'Cess',
(SELECT DISTINCT ISNULL (SUM(INV4.TaxSum),0)
FROM INV4
WHERE INV4.StaType = 9
AND INV4.DocEntry = T3.DocEntry
AND INV4.LineNum = T0.LineNum) AS 'HeCess',
(SELECT DISTINCT ISNULL (SUM(INV4.TaxSum),0)
FROM INV4
WHERE (INV4.StaType = 1 or inv4.staType = 8)
AND INV4.DocEntry = T3.DocEntry
AND INV4.LineNum = T0.LineNum) AS 'VAT',
(SELECT DISTINCT ISNULL (SUM(INV4.TaxSum),0)
FROM INV4
WHERE INV4.StaType = 8
AND INV4.DocEntry = T3.DocEntry
AND INV4.LineNum = T0.LineNum) AS 'CST'
From INV1 T0
Left Join OITM T1 On T1.ItemCode=T0.ItemCode
Left Join OITB T2 On T2.ItmsGrpCod=T1.ItmsGrpCod
Left Join OINV T3 On T3.DocEntry=T0.DocEntry
Left Join OCRD T4 On T4.CardCode=T3.CardCode
Left Join OSLP T5 On T5.SlpCode=T4.SlpCode
Left Join INV12 T6 On T6.DocEntry=T0.DocEntry
Left Join OCST T7 On T7.Code=T6.StateB and T7.Country='IN'
Left Join OCST T8 On T8.Code=T6.StateS and T8.Country='IN'
left join inv3 t9 on t9.docentry = t3.docentry
WHERE T3.[DocDate] >= [%0] and T3.[DocDate] <= [%1] and t3.U_UNE_GCAT = '2'
Group By T1.U_grp01,T3.DocEntry,T3.DocNum,T3.DocDate,T0.LineNum,T3.CardCode,T3.CardName,T5.SlpName,T4.IndustryC,T4.U_Une_Zone,
T6.CityB,T7.Name,T6.CityS,T8.Name,T0.VatSum,T0.Currency,T0.Rate,T3.DocType,T0.Price,T0.LineTotal,
T2.ItmsGrpNam,T0.ItemCode,T0.Dscription,T0.StockPrice,T0.INMPrice,T0.PriceBefDi,T0.Quantity,t9.linetotal,t0.WhsCode
Union All
Select
T1.U_grp01 As 'BA',T3.DocDate As 'Posting Date',Month(T3.DocDate) As 'PostMonth',Year(T3.DocDate) As 'PostYear',
'AR Invoice' As 'Type',T3.DocNum As 'Doc No',T3.CardCode As 'Cust. Code',T3.CardName As 'Cust. Name',T5.SlpName As 'Sale Emp. Name',
T4.IndustryC As 'Channel Type',T6.CityB As 'BillToCity',T7.Name As 'BillToState',T6.CityS As 'ShipToCity',T8.Name As 'ShipToState',
T4.U_Une_Zone As 'Zone',
T2.ItmsGrpNam As 'L1',T0.LineNum As 'Row No',T0.ItemCode As 'ItemCode',T0.Dscription As 'Item Name',t0.whscode,
T0.Quantity As 'Quantity',T0.StockPrice As 'COGS Price',IsNull(Sum(T0.Quantity * T0.StockPrice),0) As 'COGS Value',T0.VatSum As 'Tax Amount',
IsNull((Case When T3.DocType='I' Then (Case When T0.Currency = 'INR' Then T0.PriceBefDi Else (T0.PriceBefDi * T0.Rate) End) Else T0.Price End) ,0) As 'Sales Price',
IsNull((Case When T3.DocType='I' Then (Case When T0.Currency = 'INR' Then T0.INMPrice Else (T0.INMPrice * T0.Rate) End) Else T0.Price End) ,0) As 'Sales Price',
IsNull((Case When T3.DocType='I' Then (Case When T0.Currency = 'INR' Then Sum(T0.Quantity * T0.INMPrice) Else Sum(T0.Quantity * T0.INMPrice * T0.Rate) End) Else T0.LineTotal End) ,0) As 'Sales Value',t9.linetotal as 'Freight',
T3.DocType As 'DocType',
(SELECT DISTINCT ISNULL (SUM(rin4.TaxSum),0)
FROM rin4
WHERE rin4.StaType = -90
AND rin4.DocEntry = T3.DocEntry
AND rin4.LineNum = T0.LineNum) AS 'BED',
(SELECT DISTINCT ISNULL (SUM(rin4.TaxSum),0)
FROM rin4
WHERE rin4.StaType = -60
AND rin4.DocEntry = T3.DocEntry
AND rin4.LineNum = T0.LineNum) AS 'Cess',
(SELECT DISTINCT ISNULL (SUM(rin4.TaxSum),0)
FROM rin4
WHERE rin4.StaType = 9
AND rin4.DocEntry = T3.DocEntry
AND rin4.LineNum = T0.LineNum) AS 'HeCess',
(SELECT DISTINCT ISNULL (SUM(rin4.TaxSum),0)
FROM rin4
WHERE (rin4.StaType = 1 or rin4.staType = 8)
AND rin4.DocEntry = T3.DocEntry
AND rin4.LineNum = T0.LineNum) AS 'VAT',
(SELECT DISTINCT ISNULL (SUM(rin4.TaxSum),0)
FROM rin4
WHERE rin4.StaType = 8
AND rin4.DocEntry = T3.DocEntry
AND rin4.LineNum = T0.LineNum) AS 'CST'
From rin1 T0
Left Join OITM T1 On T1.ItemCode=T0.ItemCode
Left Join OITB T2 On T2.ItmsGrpCod=T1.ItmsGrpCod
Left Join Orin T3 On T3.DocEntry=T0.DocEntry
Left Join OCRD T4 On T4.CardCode=T3.CardCode
Left Join OSLP T5 On T5.SlpCode=T4.SlpCode
Left Join rin12 T6 On T6.DocEntry=T0.DocEntry
Left Join OCST T7 On T7.Code=T6.StateB and T7.Country='IN'
Left Join OCST T8 On T8.Code=T6.StateS and T8.Country='IN'
left join rin3 t9 on t9.docentry = t3.docentry
WHERE T3.[DocDate] >= [%0] and T3.[DocDate] <= [%1] and t3.U_UNE_GCAT = '2'
Group By T1.U_grp01,T3.DocEntry,T3.DocNum,T3.DocDate,T0.LineNum,T3.CardCode,T3.CardName,T5.SlpName,T4.IndustryC,T4.U_Une_Zone,
T6.CityB,T7.Name,T6.CityS,T8.Name,T0.VatSum,T0.Currency,T0.Rate,T3.DocType,T0.Price,T0.LineTotal,
T2.ItmsGrpNam,T0.ItemCode,T0.Dscription,T0.StockPrice,T0.INMPrice,T0.PriceBefDi,T0.Quantity,t9.linetotal,t0.WhsCode
Thanks in AdvanceHi deepak..
try this
/* SELECT FROM OSRT P1 */
DECLARE @FROM AS DATE
/* WHERE */
SET @FROM = /* P1.FromDate */ '[%1]'
/* SELECT FROM OSRT P2 */
DECLARE @TO AS DATE
/* WHERE */
SET @TO = /* P2.ToDate */ '[%2]';
SELECT T1.U_grp01 AS 'BA',
T3.DocDate AS 'Posting Date',
MONTH(T3.DocDate) AS 'PostMonth',
YEAR(T3.DocDate) AS 'PostYear',
'AR Invoice' AS 'Type',
T3.DocNum AS 'Doc No',
T3.CardCode AS 'Cust. Code',
T3.CardName AS 'Cust. Name',
T5.SlpName AS 'Sale Emp. Name',
T4.IndustryC AS 'Channel Type',
T6.CityB AS 'BillToCity',
T7.Name AS 'BillToState',
T6.CityS AS 'ShipToCity',
T8.Name AS 'ShipToState',
T4.U_Une_Zone AS 'Zone',
T2.ItmsGrpNam AS 'L1',
T0.LineNum AS 'Row No',
T0.ItemCode AS 'ItemCode',
T0.Dscription AS 'Item Name',
t0.whscode,
T0.Quantity AS 'Quantity',
T0.StockPrice AS 'COGS Price',
ISNULL(SUM(T0.Quantity * T0.StockPrice), 0) AS 'COGS Value',
T0.VatSum AS 'Tax Amount',
ISNULL(
CASE
WHEN T3.DocType = 'I' THEN (
CASE
WHEN T0.Currency = 'INR' THEN T0.PriceBefDi
ELSE (T0.PriceBefDi * T0.Rate)
END
ELSE T0.Price
END
0
) AS 'Sales Price',
ISNULL(
CASE
WHEN T3.DocType = 'I' THEN (
CASE
WHEN T0.Currency = 'INR' THEN T0.INMPrice
ELSE (T0.INMPrice * T0.Rate)
END
ELSE T0.Price
END
0
) AS 'Sales Price',
ISNULL(
CASE
WHEN T3.DocType = 'I' THEN (
CASE
WHEN T0.Currency = 'INR' THEN SUM(T0.Quantity * T0.INMPrice)
ELSE SUM(T0.Quantity * T0.INMPrice * T0.Rate)
END
ELSE T0.LineTotal
END
0
) AS 'Sales Value',
t9.linetotal AS 'Freight',
T3.DocType AS 'DocType',
SELECT DISTINCT ISNULL(SUM(INV4.TaxSum), 0)
FROM INV4
WHERE INV4.StaType = -90
AND INV4.DocEntry = T3.DocEntry
AND INV4.LineNum = T0.LineNum
) AS 'BED',
SELECT DISTINCT ISNULL(SUM(INV4.TaxSum), 0)
FROM INV4
WHERE INV4.StaType = -60
AND INV4.DocEntry = T3.DocEntry
AND INV4.LineNum = T0.LineNum
) AS 'Cess',
SELECT DISTINCT ISNULL(SUM(INV4.TaxSum), 0)
FROM INV4
WHERE INV4.StaType = 9
AND INV4.DocEntry = T3.DocEntry
AND INV4.LineNum = T0.LineNum
) AS 'HeCess',
SELECT DISTINCT ISNULL(SUM(INV4.TaxSum), 0)
FROM INV4
WHERE (INV4.StaType = 1 OR inv4.staType = 8)
AND INV4.DocEntry = T3.DocEntry
AND INV4.LineNum = T0.LineNum
) AS 'VAT',
SELECT DISTINCT ISNULL(SUM(INV4.TaxSum), 0)
FROM INV4
WHERE INV4.StaType = 8
AND INV4.DocEntry = T3.DocEntry
AND INV4.LineNum = T0.LineNum
) AS 'CST'
FROM INV1 T0
LEFT JOIN OITM T1
ON T1.ItemCode = T0.ItemCode
LEFT JOIN OITB T2
ON T2.ItmsGrpCod = T1.ItmsGrpCod
LEFT JOIN OINV T3
ON T3.DocEntry = T0.DocEntry
LEFT JOIN OCRD T4
ON T4.CardCode = T3.CardCode
LEFT JOIN OSLP T5
ON T5.SlpCode = T4.SlpCode
LEFT JOIN INV12 T6
ON T6.DocEntry = T0.DocEntry
LEFT JOIN OCST T7
ON T7.Code = T6.StateB
AND T7.Country = 'IN'
LEFT JOIN OCST T8
ON T8.Code = T6.StateS
AND T8.Country = 'IN'
LEFT JOIN inv3 t9
ON t9.docentry = t3.docentry
WHERE T3.[DocDate] >= @FROM
AND T3.[DocDate] <= @TO
AND t3.U_UNE_GCAT = '2'
GROUP BY
T1.U_grp01,
T3.DocEntry,
T3.DocNum,
T3.DocDate,
T0.LineNum,
T3.CardCode,
T3.CardName,
T5.SlpName,
T4.IndustryC,
T4.U_Une_Zone,
T6.CityB,
T7.Name,
T6.CityS,
T8.Name,
T0.VatSum,
T0.Currency,
T0.Rate,
T3.DocType,
T0.Price,
T0.LineTotal,
T2.ItmsGrpNam,
T0.ItemCode,
T0.Dscription,
T0.StockPrice,
T0.INMPrice,
T0.PriceBefDi,
T0.Quantity,
t9.linetotal,
t0.WhsCode
UNION ALL
SELECT T1.U_grp01 AS 'BA',
T3.DocDate AS 'Posting Date',
MONTH(T3.DocDate) AS 'PostMonth',
YEAR(T3.DocDate) AS 'PostYear',
'AR Invoice' AS 'Type',
T3.DocNum AS 'Doc No',
T3.CardCode AS 'Cust. Code',
T3.CardName AS 'Cust. Name',
T5.SlpName AS 'Sale Emp. Name',
T4.IndustryC AS 'Channel Type',
T6.CityB AS 'BillToCity',
T7.Name AS 'BillToState',
T6.CityS AS 'ShipToCity',
T8.Name AS 'ShipToState',
T4.U_Une_Zone AS 'Zone',
T2.ItmsGrpNam AS 'L1',
T0.LineNum AS 'Row No',
T0.ItemCode AS 'ItemCode',
T0.Dscription AS 'Item Name',
t0.whscode,
T0.Quantity AS 'Quantity',
T0.StockPrice AS 'COGS Price',
ISNULL(SUM(T0.Quantity * T0.StockPrice), 0) AS 'COGS Value',
T0.VatSum AS 'Tax Amount',
ISNULL(
CASE
WHEN T3.DocType = 'I' THEN (
CASE
WHEN T0.Currency = 'INR' THEN T0.PriceBefDi
ELSE (T0.PriceBefDi * T0.Rate)
END
ELSE T0.Price
END
0
) AS 'Sales Price',
ISNULL(
CASE
WHEN T3.DocType = 'I' THEN (
CASE
WHEN T0.Currency = 'INR' THEN T0.INMPrice
ELSE (T0.INMPrice * T0.Rate)
END
ELSE T0.Price
END
0
) AS 'Sales Price',
ISNULL(
CASE
WHEN T3.DocType = 'I' THEN (
CASE
WHEN T0.Currency = 'INR' THEN SUM(T0.Quantity * T0.INMPrice)
ELSE SUM(T0.Quantity * T0.INMPrice * T0.Rate)
END
ELSE T0.LineTotal
END
0
) AS 'Sales Value',
t9.linetotal AS 'Freight',
T3.DocType AS 'DocType',
SELECT DISTINCT ISNULL(SUM(rin4.TaxSum), 0)
FROM rin4
WHERE rin4.StaType = -90
AND rin4.DocEntry = T3.DocEntry
AND rin4.LineNum = T0.LineNum
) AS 'BED',
SELECT DISTINCT ISNULL(SUM(rin4.TaxSum), 0)
FROM rin4
WHERE rin4.StaType = -60
AND rin4.DocEntry = T3.DocEntry
AND rin4.LineNum = T0.LineNum
) AS 'Cess',
SELECT DISTINCT ISNULL(SUM(rin4.TaxSum), 0)
FROM rin4
WHERE rin4.StaType = 9
AND rin4.DocEntry = T3.DocEntry
AND rin4.LineNum = T0.LineNum
) AS 'HeCess',
SELECT DISTINCT ISNULL(SUM(rin4.TaxSum), 0)
FROM rin4
WHERE (rin4.StaType = 1 OR rin4.staType = 8)
AND rin4.DocEntry = T3.DocEntry
AND rin4.LineNum = T0.LineNum
) AS 'VAT',
SELECT DISTINCT ISNULL(SUM(rin4.TaxSum), 0)
FROM rin4
WHERE rin4.StaType = 8
AND rin4.DocEntry = T3.DocEntry
AND rin4.LineNum = T0.LineNum
) AS 'CST'
FROM rin1 T0
LEFT JOIN OITM T1
ON T1.ItemCode = T0.ItemCode
LEFT JOIN OITB T2
ON T2.ItmsGrpCod = T1.ItmsGrpCod
LEFT JOIN [dbo].[Orin] T3
ON T3.DocEntry = T0.DocEntry
LEFT JOIN OCRD T4
ON T4.CardCode = T3.CardCode
LEFT JOIN OSLP T5
ON T5.SlpCode = T4.SlpCode
LEFT JOIN rin12 T6
ON T6.DocEntry = T0.DocEntry
LEFT JOIN OCST T7
ON T7.Code = T6.StateB
AND T7.Country = 'IN'
LEFT JOIN OCST T8
ON T8.Code = T6.StateS
AND T8.Country = 'IN'
LEFT JOIN rin3 t9
ON t9.docentry = t3.docentry
WHERE T3.[DocDate] >= @FROM
AND T3.[DocDate] <= @TO
AND t3.U_UNE_GCAT = '2'
GROUP BY
T1.U_grp01,
T3.DocEntry,
T3.DocNum,
T3.DocDate,
T0.LineNum,
T3.CardCode,
T3.CardName,
T5.SlpName,
T4.IndustryC,
T4.U_Une_Zone,
T6.CityB,
T7.Name,
T6.CityS,
T8.Name,
T0.VatSum,
T0.Currency,
T0.Rate,
T3.DocType,
T0.Price,
T0.LineTotal,
T2.ItmsGrpNam,
T0.ItemCode,
T0.Dscription,
T0.StockPrice,
T0.INMPrice,
T0.PriceBefDi,
T0.Quantity,
t9.linetotal,
t0.WhsCode
rgds
Kennedy -
Require help with Pivot table query in SQL Server 2008
Hi,
I have a query regarding converting columns to rows in SQL Server 2008. Please look at the table below.
I need the output to look something like this :
The columns for the children can be dynamic or fixed ( max of 6 children) based on the Family_ID. For Example: A family can have 1 child or more than 1 child.
Not sure how to go about it. Would appreciate your help :)Looks like you need dynamic pivot on multiple columns. I have two articles on this topic, start from this one
T-SQL:
Dynamic Pivot on Multiple Columns
It has reference to my other blog post.
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles -
Hi,
I'm trying to execute some SQL queries and I just don't understand what's wrong.
I�m using Tomcat and SQL Server in order to do this, but when I�m try to execute a query with a INNER JOIN statements Tomcat raise a SQL exception... at the very first time I thought there was a problem with database connection but I realize that a simple query to a table works pretty well. then I found out some problems with JDBC:ODBC.... so I install JDBC for SQL Server 2000 and test with the same simple query and works..... so, I come to a conclusion.... INNER JOIN or JOIN statements can't be used in JDBC..... please... somebody tell I�m wrong and give me a hand...
I'm using TOMCAT 4 and JDK 1.4 SQL Server 2000
Error occurs when executeQuery() is called.... not prepareStatement().... ??????
Driver DriverRecResult = (Driver)Class.forName(driver).newInstance();
Connection ConnRecResult = DriverManager.getConnection(DSN,user,password);
PreparedStatement StatementRecResult = ConnRecResult.prepareStatement(query);
ResultSet RecResult = StatementRecResult.executeQuery(); <---- Exception raise here
So much tahnks in advance,That's exactly what I think, driver it's raising the exception, but I don't know why.... i test the same query with INNER JOIN directly from SQL Query Analyser and it's works perfectly, my problem ain't SQL, but JSP and JDBC 'cause i'm a newbie about these issues.
Common sense tell me possible problems lie in SQLServer drivers 'cause i run the same pages on JRUN through jdbc:odbc and do works well, but by now i just depend on Tomcat.....
I've installed SQL Server drivers for JDBC but i just find it doesn't work fully... could be the version of JDK i've installed? what version do i need?
( I'm running Tomcat 4 with JDK 1.4 & SQL Server 2000 W2K )
thanks for reply. -
Outer join query for SQL server from Oracle
Hi All,
My question is regarding making queries from Oracle to SQL Server database thorugh DBLink.
In my oracle database I have a DBLink emp.world for SQL Server database.
I need to query SQL Server data from oracle (so that this query can be combined with other oracle tables).
Query is given below:
SELECT
a."EmpID" as "Employee ID",
a."EmpStatus" "Employee Status"
b."EmpSub" as "Employee Subjects"
FROM
[email protected] a
left outer join [email protected] b on a."EmpID" = b."suEmpID"
ORDER BY a."EmpID";My problem is when I run the same query from oracle, it does not show the EmpID that does not exist in Subjects table, but when run from actual SQL Server database, it shows all the records.
Samples are given below:
Run from Oracle
Employee ID Employee Status Employee Subjects
101 Active Maths
102 Active Maths
102 Active Physics
104 Inactive Chemistry
Run form SQL Server
Employee ID Employee Status Employee Subjects
101 Active Maths
102 Active Maths
102 Active Physics
103 Active NULL
104 Inactive ChemistryI am not sure why in oracle outer join for SQL server tables is not working. What is the right way for outer join in this case.
I am using oracle database 10gR2 and SQL Server 2005.
Please Help.
Thanks.SELECT
a."EmpID" as "Employee ID",
a."EmpStatus" "Employee Status"
b."EmpSub" as "Employee Subjects"
FROM
[email protected] a
left outer join [email protected] b on a."EmpID" = b."suEmpID"
ORDER BY a."EmpID";
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm#sthref3175
From your description, it appears you may need a right outer join. You want to get back all the rows from 'B', not from 'A'. Try a right join and let us know. -
Query in sql server from Oracle 9.2
Hi
How can I to connect and execute query from Oracle without to use others products ( DG4MSQL ....etc) ?
using 9.2.08mkirtley wrote:
Hi,
I say it is 'free' because Dg4ODBC is included in your RDBMS license and you just need to supply a third party ODBC driver.
See this note -
Gateway and Generic Connectivity Licensing Considerations (Doc ID 232482.1)
You need to install and configure it in an ORACLE_HOME on a machine somewhere then you can use database links from to connect to it and select from non-Oracle databases.
Have a look at these notes depending on the platfrom you plan to use -
How to Setup DG4ODBC on Linux x86 32bit (Doc ID 466228.1)
How to Configure DG4ODBC on 64bit Unix OS (Linux, Solaris, AIX, HP-UX Itanium) to Connect to Non-Oracle Databases Post Install (Doc ID 561033.1)
How to Setup DG4ODBC (Oracle Database Gateway for ODBC) on Windows 32bit (Doc ID 466225.1)
How to Setup DG4ODBC (Oracle Database Gateway for ODBC) on 64bit Windows Operating Systems (Doc ID 1266572.1)
As I said in my earlier update if SQL*Server is your non-Oracle database then an standalone install on Windows is the best option to be able to use the Microsoft ODBC driver, although there is now a MS ODBC available on Linux x86-64 bit.
Please read through the notes I have referred to because they should have the information you need.
Regards,
MikeThank you for informations, I search, because My client is 9.2.08, but I am a DBA and do not have access to UNIX server where is Database -
Converting Oracle query to Sql Server with date manipulation
The following SSIS statement works great in Oracle, and it parses successfully in the "Sql Task" window. In Oracle it returns
this result:
FROM_DT THRU_DT
16-Jan-2014 23-Jan-2014
select
case
when to_char(to_date(sysdate,'dd-mon-yyyy'),'dd') <=7 then to_date('24'||to_char(add_months(sysdate,-1),'mmyyyy'),'ddmmyyyy')
when to_char(to_date(sysdate,'dd-mon-yyyy'),'dd') <=15 then to_date('01'||to_char(sysdate,'mmyyyy'),'ddmmyyyy')
when to_char(to_date(sysdate,'dd-mon-yyyy'),'dd') <=23 then to_date('08'||to_char(sysdate,'mmyyyy'),'ddmmyyyy')
else to_date('16'||to_char(sysdate,'mmyyyy'),'ddmmyyyy')
end as from_dt,
case
when to_char(to_date(sysdate,'dd-mon-yyyy'),'dd') <=7 then trunc(last_day(add_months(sysdate,-1)))
when to_char(to_date(sysdate,'dd-mon-yyyy'),'dd') <=15 then to_date('07'||to_char(sysdate,'mmyyyy'),'ddmmyyyy')
when to_char(to_date(sysdate,'dd-mon-yyyy'),'dd') <=23 then to_date('15'||to_char(sysdate,'mmyyyy'),'ddmmyyyy')
else to_date('23'||to_char(sysdate,'mmyyyy'),'ddmmyyyy')
end as thru_dt
from sys.dual
How can I convert this to Sql Server ?>>You mean you get the data from Oracle and tries to insert it into SQL Server?
- no, sorry, I want to convert the syntax to Sql Server
>>Are
you running the query in OLEDB provider connected to the Oracle's database?
- yes, this works fine now (after fixing this redundancy:
to_char(to_date(sysdate,'dd-mon-yyyy'),'dd')change toto_char(sysdate,'dd') -
How to convert the following query to sql server 2005
hi
i have the following query in ms access and i want to convert it into sql server 2005 express edition
SELECT iif(Max(BNo) is null,1,Max(BNo)+1) AS BNo from ( SELECT iif(Max(Product.BNo) is null,0,Max(Product.BNo)) AS BNo FROM Product union all SELECT iif(Max(grndetail.BNo) is null,0,Max(grndetail.BNo)) AS BNo FROM grndetail UNION ALL SELECT iif(Max(srdetail.BNo) is null,0,Max(srdetail.BNo)) AS BNo FROM srdetail ) as t
how to do thisThe construct involving case when MAX sounds suspicious. Can you explain what the query is supposed to be doing, the structure of your table, then we can re-write it?
E.g. if you provide your table, some data and desired result, you'll get better answer than direct translation of this suspicious query.
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles
Maybe you are looking for
-
BAPIs to create sales orders for Returns and Debit Memo Request
Hi, I have to create a sales order for document type Returns as well as a debit memo request.The BAPI 'BAPI_SALESORDER_CREATEFROMDAT2' is giving the following error message for Debit Memo Request document creation as well as for Returns. 761 Unpermit
-
Cannot open any RAW files on PS CS6? what could be causing this?
This is the message i keep getting... "Could not complete your request because Photoshop does not recognize this type of file."
-
I have a problem with my PC mobile device that the keyboard stop working and the mouse also
I have a problem with my PC mobile device that the keyboard stop working and the mouse also
-
Macintosh HD and Bootcamp Installer Disagree on Free space on Hard Drive
Im having a hard drive issue. I have 500 gb and macosx shows about 300gb free. I'm attempting to install windows and the assistant gives about 100gb of space but I'd like 200 or so for windows. Theres a discrepancy in available hard drive space! I ha
-
Profanity does not allow to post in forum
I have been trying to post a message regarding my laptop but keep getting the red x stating about the wording. There are no swear words just details of my problems yet to get it posted i actually cut it down to the laptop series and the operating sys