Solved

sql syntax

Posted on 2013-01-22
2
337 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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

762 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

19 Experts available now in Live!

Get 1:1 Help Now