Solved

Hide or show row in sql result depending on vaule

Posted on 2014-11-29
12
66 Views
Last Modified: 2014-12-08
Hi,

I have a query looking like this:
SELECT o.orderID, o.invoiceID, o.customerID, c.firstname + ' ' + c.lastname AS Name, o.invoiceDate, o.PayDate, o.ocr, o.orderDate, ISNULL(paymentStatusID, 0) AS paymentStatusID, o.paymentTypeID, orderStatusID, ISNULL(printed,0) AS printed, currencyID, paymentType, shipmentType, IsNull(sentOrder, 0) AS sentOrder  
FROM orders AS o INNER JOIN Customer AS c ON o.customerID = c.CustomerID  
INNER JOIN bklShipmentType AS bst ON bst.shipmentTypeID = o.shipmentTypeID 
INNER JOIN bklPaymentType AS bpt ON bpt.paymentTypeID = o.paymentTypeID 
WHERE (o.invoiceID > '0') AND (o.orgID = 11) AND orderSaved = 1 AND orderTypeID = 1 AND orderStatusID NOT IN (5, 8) AND o.paymentTypeID <> 12 
ORDER BY orderDate DESC 

Open in new window


The result of the query looks like this:
sql.png
I want to change the query so that if paymentTypeID is 5 or 15 then paymentStatusID must be 2 or else the row shall not be part of the resultset.

How can I accomplish that?

Peter
0
Comment
Question by:peternordberg
  • 5
  • 4
  • 3
12 Comments
 
LVL 18

Accepted Solution

by:
SimonAdept earned 500 total points
ID: 40471672
Hi, please try with this where clause. I've broken it into separate lines for clarity, you don't have to.

WHERE (o.invoiceID > '0')
AND (o.orgID = 11)
AND orderSaved = 1
AND orderTypeID = 1
AND orderStatusID NOT IN (5, 8)
AND
(
   o.paymentTypeID  NOT IN(12,5,15)
   OR
   o.paymentTypeID  IN(5,15) AND paymentStatusID=2
)
0
 
LVL 58

Expert Comment

by:Gary
ID: 40471718
Might want to bracket that OR condition
0
 
LVL 18

Expert Comment

by:SimonAdept
ID: 40471964
@Gary - 'might want to' for readability or because of unexpected results? I'm always willing to defer to greater expertise, but have tested this on SQL fiddle and appears fine as is. I'd agree it could be bracketed for clarity, but I normally rely on T-SQL operator precedence AND>OR and dislike extraneous parentheses.

Here's the whole select statement for testing, with extra parentheses for clarity...
SELECT o.orderID, o.invoiceID, o.customerID, c.firstname + ' ' + c.lastname AS Name, o.invoiceDate, o.PayDate, o.ocr, o.orderDate, ISNULL(paymentStatusID, 0) AS paymentStatusID, o.paymentTypeID, orderStatusID, ISNULL(printed,0) AS printed, currencyID, paymentType, shipmentType, IsNull(sentOrder, 0) AS sentOrder  
FROM orders AS o INNER JOIN Customer AS c ON o.customerID = c.CustomerID  
INNER JOIN bklShipmentType AS bst ON bst.shipmentTypeID = o.shipmentTypeID 
INNER JOIN bklPaymentType AS bpt ON bpt.paymentTypeID = o.paymentTypeID 
WHERE (o.invoiceID > '0')
AND (o.orgID = 11)
AND orderSaved = 1
AND orderTypeID = 1
AND orderStatusID NOT IN (5, 8)
AND
( o.paymentTypeID  NOT IN(12,5,15) OR (o.paymentTypeID  IN(5,15) AND paymentStatusID=2)) 
ORDER BY orderDate DESC 

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 40472025
More for readability, not everyone knows the order and just makes more sense when you look back at it.
(...and I think it's better practise but thats a personal view)
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40473277
Not for readability. It really needs the brackets there or else will filter only for paymentStatusID=2 no matter the paymentTypeID.
0
 
LVL 18

Expert Comment

by:SimonAdept
ID: 40473986
I don't wish to seem disagreeable, but on my 2008R2 system, this works as advertised. It does not filter out PaymentStatusID values <>2, apart from records with PaymentTypeID in (5,15)

I've  simplified the table structure and query to omit the joins in the example below.
CREATE TABLE [dbo].[Orders](
	[OrderID] [int] NULL,
	[InvoiceID] [int] NULL,
	[OrgID] [int] NULL,
	[OrderSaved] [int] NULL,
	[OrderTypeID] [int] NULL,
	[OrderStatusID] [int] NULL,
	[PaymentTypeID] [int] NULL,
	[PaymentStatusID] [int] NULL
) ON [PRIMARY]
GO

INSERT [dbo].[Orders] 
VALUES (1, 1, 11, 1, 1, 1, 12, 2)
,(2, 2, 11, 1, 1, 1, 5, 2)
,(3, 3, 11, 1, 1, 1, 5, 3)
,(4, 4, 11, 1, 1, 1, 15, 2)
, (5, 5, 11, 1, 1, 1, 15, 3)
,(6, 6, 11, 1, 1, 1, 1, 14)
,(7, 7, 11, 1, 1, 1, 12, 3)
,(8, 8, 11, 1, 1, 1, 1, 7)
GO

SELECT o.orderID, o.invoiceID 
, ISNULL(paymentStatusID, 0) AS paymentStatusID, o.paymentTypeID, orderStatusID
FROM orders AS o  
 
WHERE (o.invoiceID > '0')
AND (o.orgID = 11)
AND orderSaved = 1
AND orderTypeID = 1
AND orderStatusID NOT IN (5, 8)
AND
o.paymentTypeID  NOT IN(12,5,15) OR o.paymentTypeID  IN(5,15) AND paymentStatusID=2

Open in new window


table content and query results
@Vitor, I'd really appreciate if you would paste my code into a query and run it to see if you get different results to me.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40474034
Simon, I did test it and worked as you said but then means that it's a SQL Server way of work. If you want to transfer the query to another RDBMS I don't think it will work as expected.
Then at least for readability you should use the brackets so the author knows that if for some reason he needs to move the paymentStatusID=2 he needs to move everything between the brackets.
0
 
LVL 18

Expert Comment

by:SimonAdept
ID: 40474043
Point taken about bracketing for readability and portability, Vitor. I appreciate you taking the time to test it. Thanks.
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40474047
It was also good that you had the time to clarify it. I was so sure that the brackets would make all the difference in logic but you showed me that SQL Server uses the precedence of the operators. Thanks for that also. Living and learning :)
0
 
LVL 58

Expert Comment

by:Gary
ID: 40474055
Should be the case in all DB's, certainly in MySQL it would work without the brackets.
0
 
LVL 18

Expert Comment

by:SimonAdept
ID: 40474067
Thanks both. In that case I stand by my earlier post as being the correct solution (with assistance from Vitor and Gary) :-)
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40475377
No points for me please. My statement wasn't right.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

758 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now