Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL SYNTAX

Posted on 2009-04-01
4
Medium Priority
?
205 Views
Last Modified: 2012-05-06


Hello Experts,

I have a SQL statement where I want to do a slight change to affect the data being displayed in the CSV file.

Basically, I am trying to send registration details to the third party when the customer accept to recieve the newsletter
from them other i only want to send only GiftcodeID and "Anonymous" text for the rest of the columns

EX:

id       name             addrss             registriondate
=========================================================
1        Anonymous      Anonymous      20-03-1009
2      mike                    london            21-03-2009
3      clinton            mexico            24-03-2009
4      Anonymous      Anonymous      24-03-2009


Please can someone help?

thanks
s
0
Comment
Question by:newbie27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 8

Author Comment

by:newbie27
ID: 24037785
Please see the query attached

SELECT DISTINCT 
                      TOP (100) PERCENT dbo.GiftCertificate.GiftCodeID, dbo.GiftCertificate.CustID, dbo.GiftCertificate.CertRegTitle, dbo.GiftCertificate.CertRegFirstName, 
                      dbo.GiftCertificate.CertRegLastName, dbo.GiftCertificate.CertRegAddr1, dbo.GiftCertificate.CertRegAddr2, dbo.GiftCertificate.CertRegAddr3, 
                      dbo.GiftCertificate.CertRegCity, dbo.GiftCertificate.CertRegCounty, dbo.GiftCertificate.CertRegPostCode,  
                      dbo.Country.CountryName, dbo.GiftCertificateHistory.GiftHistDateAdded AS DateReg
FROM         dbo.GiftCertificate INNER JOIN
                      dbo.GiftCertificateHistory ON dbo.GiftCertificate.GiftCodeID = dbo.GiftCertificateHistory.GiftCodeID INNER JOIN
                      dbo.Country ON dbo.GiftCertificate.CertRegCountryID = dbo.Country.CountryID INNER JOIN
						BritishWildlife on giftcertificate.giftcodeid = BritishWildlife.giftcodeid	
 
WHERE     (dbo.GiftCertificate.GiftCodeID IN
                          (SELECT     GiftCodeID
                            FROM          dbo.GiftCertificateHistory AS GiftCertificateHistory_1
                            WHERE      (StatusID = 'REGISTERED'))) AND 
 
 (dbo.GiftCertificate.CurrentStatusID <> 'LIVE') AND 
                      (dbo.GiftCertificateHistory.StatusID = 'REGISTERED') 
AND (dbo.GiftCertificate.CurrentStatusID <> 'CERT_CANCELLED')
AND dbo.GiftCertificate.GiftCertificateTypeID='WILDLIFE'
 AND dbo.GiftCertificateHistory.GiftHistDateAdded > '2009-03-01'
    AND dbo.GiftCertificateHistory.GiftHistDateAdded < '2009-03-31 23:59'
and BritishWildlife.newsletter=1
ORDER BY dbo.GiftCertificate.GiftCodeID

Open in new window

0
 
LVL 8

Author Comment

by:newbie27
ID: 24037792
If  BritishWildlife.newsletter=1 means the customer has opted in otherwise if its  BritishWildlife.newsletter=0 then no details should be sent, so the data should read as "Anonymous"

Please advise
thanks for any help
0
 
LVL 4

Accepted Solution

by:
amazingwolf1 earned 2000 total points
ID: 24037841
So, basically you want to print ALL users, but put different data for name etc by a flag. I attached a revised version of your query.
What I did was put a "Case" condition before the first name and last name, so we will output them only if BritishWildlife.newsletter = 1. Also, I canged the inner join to BritishWildlife into left join, so you will get ALL users in your system.

HTH
SELECT DISTINCT 
                      TOP (100) PERCENT 
					dbo.GiftCertificate.GiftCodeID, 
					dbo.GiftCertificate.CustID, 
					dbo.GiftCertificate.CertRegTitle, 
					(
						CASE WHEN BritishWildlife.newsletter = 1 THEN dbo.GiftCertificate.CertRegFirstName
						ELSE 'Anonymous'
						END
					) AS CertRegFirstName,
					(
						CASE WHEN BritishWildlife.newsletter = 1 THEN dbo.GiftCertificate.CertRegLastName
						ELSE 'Anonymous'
						END
					) AS CertRegLastName,
					dbo.GiftCertificate.CertRegAddr1, 
					dbo.GiftCertificate.CertRegAddr2, 
					dbo.GiftCertificate.CertRegAddr3, 
                    dbo.GiftCertificate.CertRegCity, 
					dbo.GiftCertificate.CertRegCounty, dbo.GiftCertificate.CertRegPostCode,  
                    dbo.Country.CountryName, 
					dbo.GiftCertificateHistory.GiftHistDateAdded AS DateReg
FROM         
					dbo.GiftCertificate 
INNER JOIN
                    dbo.GiftCertificateHistory ON dbo.GiftCertificate.GiftCodeID = dbo.GiftCertificateHistory.GiftCodeID 
INNER JOIN
                    dbo.Country ON dbo.GiftCertificate.CertRegCountryID = dbo.Country.CountryID 
LEFT JOIN
					BritishWildlife on giftcertificate.giftcodeid = BritishWildlife.giftcodeid	
 
WHERE     (dbo.GiftCertificate.GiftCodeID 
			IN
                          (SELECT     GiftCodeID
                            FROM          dbo.GiftCertificateHistory AS GiftCertificateHistory_1
                            WHERE      (StatusID = 'REGISTERED'))) 
			AND 
 
			(dbo.GiftCertificate.CurrentStatusID <> 'LIVE') AND 
                      (dbo.GiftCertificateHistory.StatusID = 'REGISTERED') 
			AND 
				(dbo.GiftCertificate.CurrentStatusID <> 'CERT_CANCELLED')
			AND dbo.GiftCertificate.GiftCertificateTypeID='WILDLIFE'
			AND dbo.GiftCertificateHistory.GiftHistDateAdded > '2009-03-01'
			AND dbo.GiftCertificateHistory.GiftHistDateAdded < '2009-03-31 23:59'
			--and BritishWildlife.newsletter=1
ORDER BY dbo.GiftCertificate.GiftCodeID

Open in new window

0
 
LVL 8

Author Comment

by:newbie27
ID: 24037995
thanks for the help, that was exactly how i wanted it to show...

regards
s
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

618 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