Solved

sql syntax

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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…
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.

739 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