Solved

sql syntax

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
grouping logic 6 49
SQL Query resolving a string conversion issue 26 38
SQL - SP needs a little help 9 22
Update data using formula 22 21
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

920 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

11 Experts available now in Live!

Get 1:1 Help Now