Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql syntax

Posted on 2013-01-22
2
Medium Priority
?
344 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 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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 extract information from SQL Server on Database, Connection and Server properties

715 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