Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql syntax

Posted on 2013-01-22
2
Medium Priority
?
346 Views
Last Modified: 2013-01-23
Hello Experts
I am using attached syntax to get the following result

Result
================
Big Cat      15      1      2013      1      Amur leopard
Big Cat      16      1      2013      1      Lion
Big Cat      17      1      2013      1      Asiatic cheetah
Donkey      16      1      2013      2      Loppy
Horse      15      1      2013      2      NULL

There is a German verion of Bigcats which I am trying to show in SideName, when this condition satisfies

CASE Select giftcodeid from AdoptABigCat where isgermanreg = 1

THEN BC.BigCatName AS SideName

ELSE  BC.GermanBigCatName AS SideName

Similary it should check for all other registrations type.

I hope someone can advise how can I go about checking the type and then show appropriate in the same column

Desired Result
==============
Result
================
Big Cat      15      1      2013      1      Amur leopard
Big Cat      16      1      2013      1      Löwe
Big Cat      17      1      2013      1      Asiatischer Gepard
Donkey      16      1      2013      2      Loppy
Horse      15      1      2013      2      NULL
SELECT     	TOP (100) PERCENT 
		GCT.GiftCertificateTypeName, 
		DATEPART(day, GCH.GiftHistDateAdded) AS day, 
		DATEPART(month, GCH.GiftHistDateAdded) AS month, 
		DATEPART(year, GCH.GiftHistDateAdded) AS year, 
		COUNT(GCH.GiftCodeID) AS NumCertsSent, 
		COALESCE (PT.PonyName, DN.DonkeyName, BC.BigCatName) AS SideName
FROM         dbo.GiftCertificateHistory AS GCH INNER JOIN
                      dbo.GiftCertificate AS GC ON GCH.GiftCodeID = GC.GiftCodeID INNER JOIN
                      dbo.GiftCertificateType AS GCT ON GC.GiftCertificateTypeID = GCT.GiftCertificateTypeID LEFT OUTER JOIN
                      dbo.AdoptAPony AS AP ON GC.GiftCodeID = AP.GiftCodeID LEFT OUTER JOIN
                      dbo.PonyType AS PT ON PT.PonyID = AP.PonyID LEFT OUTER JOIN
                      dbo.AdoptADonkey AS AD ON GC.GiftCodeID = AD.GiftCodeID LEFT OUTER JOIN
                      dbo.DonkeyName AS DN ON DN.DonkeyID = AD.DonkeyID LEFT OUTER JOIN
                      dbo.AdoptABigCat AS ABC ON GC.GiftCodeID = ABC.GiftCodeID LEFT OUTER JOIN
                      dbo.BigCatName AS BC ON BC.BigCatID = ABC.BigCatID LEFT OUTER JOIN
WHERE     (GCH.StatusID = 'REGISTERED') AND 
(GCH.GiftHistDateAdded >= DATEADD(dd, - 7, CAST(CONVERT(VARCHAR(10), GETDATE(), 120) AS DATETIME))) AND
(GCH.GiftHistDateAdded < CAST(CONVERT(VARCHAR(10), GETDATE(), 120) AS DATETIME))

Open in new window

0
Comment
Question by:newbie27
2 Comments
 
LVL 6

Accepted Solution

by:
liija earned 2000 total points
ID: 38806487
Not sure if I understand what you are after but

How about replacing this:
...,	COALESCE (PT.PonyName, DN.DonkeyName, BC.BigCatName) AS SideName
FROM         dbo.GiftCertificateHistory AS GCH INNER JOIN
...

Open in new window


with something like this:
...,
	CASE 
WHEN ABC.isgermanreg = 1 AND GCT.GiftCertificateTypeName = 'Big Cat'
THEN BC.GermanBigCatName 
WHEN ABC.isgermanreg <> 1 AND GCT.GiftCertificateTypeName = 'Big Cat
THEN BC.BigCatName
WHEN ABC.isgermanreg = 1 AND GCT.GiftCertificateTypeName = 'Donkey'
THEN...
...END AS SideName
FROM         dbo.GiftCertificateHistory AS GCH INNER JOIN

Open in new window

0
 
LVL 8

Author Comment

by:newbie27
ID: 38809525
Oh thank you for your input, I was unable to figure out where and how to use it. This should work, I will post you back.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

886 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