Solved

help required on tsql

Posted on 2013-11-08
9
285 Views
Last Modified: 2013-11-11
Query 1:
select [Customer PO] from dbo.SAP_ZVBAK

Output:
[Customer PO]
5209741 10098475
5275291 10087977

Query 2:
select [PO Number] from dbo.CPOA

output:
PO Number
5209741
5275291
5275298

Main query:
select '$' + ' ' + CAST((SELECT SUM([Net Value]) FROM dbo.SAP_ZVBAK
WHERE SAP_ZVBAK.[Customer PO] = CPOA.[PO Number]) AS VARCHAR(50)) AS [X] from .....

Please guide me how to add LIKE condition for [Customer PO] column in above script in WHERE clause and I have to return two common rows (5209741,5275291) from above script using LIKE operator.

Thanks in advance.
0
Comment
Question by:sqldba2013
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 5

Expert Comment

by:dannygonzalez09
ID: 39633792
Try this

select '$' + ' ' + CAST((SELECT SUM([Net Value]) FROM dbo.SAP_ZVBAK SAP
INNER JOIN dbo.CPOA
ON SAP.[Customer PO] LIKE '%'+CONVERT(VARCHAR(50),CPOA.[PO Number])  + '%'
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 39633849
If the match string is always in the start, you may try this:

SELECT A.[Customer PO], B.[PO Number], CHARINDEX(B.[PO Number],A.[Customer PO],0) FROM dbo.SAP_ZVBAK A
JOIN dbo.CPOA B ON CHARINDEX( B.[PO Number], A.[Customer PO], 0 ) = 1

If the match string occurs anywhere, then

SELECT A.[Customer PO], B.[PO Number], CHARINDEX(B.[PO Number],A.[Customer PO],0) FROM dbo.SAP_ZVBAK A
JOIN dbo.CPOA B ON CHARINDEX( B.[PO Number], A.[Customer PO], 0 ) > 0

should work.
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 39633898
You can also go with danny's comments, when i tested both, using LIKE seem to be faster, compared to CharIndex, may be when the correct indexes are setup for the tables.

if you want to match only with the start of the PO numbers:

select '$' + ' ' + CAST((SELECT SUM([Net Value])
FROM dbo.SAP_ZVBAK
JOIN dbo.CPOA ON SAP_ZVBAK.[Customer PO] LIKE CAST(CPOA.[PO Number] AS VARCHAR(50)) + '%'

HTH.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 26

Expert Comment

by:Zberteoc
ID: 39634143
" using LIKE seem to be faster"

Of course it is because that query can use indexes if there are any. Wrapping columns in SQL functions denies the SQL engine to use indexes. Query:
SELECT
	c.[PO Number],
	'$ ' + CAST(SUM([Net Value]) as varchar(25)) as Total_Net
FROM 
	dbo.SAP_ZVBAK z
	inner join dbo.CPOA c
		ON z.[Customer PO] LIKE LTRIM(CAST(c.[PO Number] as varchar(25)))+'%'
GROUP BY
	c.[PO Number]
ORDER BY
	c.[PO Number]

Open in new window

0
 

Author Comment

by:sqldba2013
ID: 39635638
Thanks to all for your suggestions.

I have executed below query and the output (amount) is not showing in correct format. Please suggest how to modify below query for correct output format (ex; $1234.56)

select
'$' + ' ' + cast((select SUM([SO Net Value]) FROM dbo.SAP_ZVBAK z with(nolock)
inner join dbo.CPOA c with(nolock)
ON z.[Customer PO] LIKE LTRIM(CAST(c.[PO Number] as varchar(50)))+'%') as nvarchar(50))AS [Amount]

Output:
Amount
$ 5.41957e+008
0
 
LVL 26

Accepted Solution

by:
Zberteoc earned 250 total points
ID: 39635654
If you use that query you will not have the result on each [PO Number]. You will have one SUM per all [PO Number] that satisfy the condition together.

In order to get the format you want you will have to cast to decimal(my query):

SELECT
	c.[PO Number],
	'$ ' + CAST(cast(SUM([Net Value]) as decimal(18,2)) as varchar(25)) as Total_Net
FROM 
	dbo.SAP_ZVBAK z
	inner join dbo.CPOA c
		ON z.[Customer PO] LIKE LTRIM(CAST(c.[PO Number] as varchar(25)))+'%'
GROUP BY
	c.[PO Number]
ORDER BY
	c.[PO Number]

Open in new window

In the query you tried:
select
'$' + ' ' + cast(cast((select SUM([SO Net Value]) FROM dbo.SAP_ZVBAK z with(nolock)
inner join dbo.CPOA c with(nolock) 
ON z.[Customer PO] LIKE LTRIM(CAST(c.[PO Number] as varchar(50)))+'%') as decimal (10,2)) as nvarchar(50))AS [Amount]

Open in new window

0
 

Author Comment

by:sqldba2013
ID: 39635675
got the below error, pls suggest

Msg 8115, Level 16, State 6, Line 1
Arithmetic overflow error converting float to data type numeric.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 39635681
Increase decimal (10,2) to decimal (18,2)
0
 

Author Closing Comment

by:sqldba2013
ID: 39638572
--
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question