Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
hayleye
Starting Member
6 Posts |
Posted - 2010-06-14 : 09:01:11
|
| I hope someone can help..My query is bringing my case statement through on different rows, what i need is for it to be brought back on one row which will hopefully look like this:Year | March | June | September| December |2009 | Received | Not Received | Received | Received |what its doing now is the following:Year | March | June | September| December |2009 | Received | | | |2009 | | Not Received | | |2009 | | |Received | |2009 | | | |Received |My case statement looks like this:case when code = 'Q' and (SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11)) like '31/03/%' then 'March' when code = 'Q' and (SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11)) like '30/06/%' then 'June' when code = 'Q' and (SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11)) like '30/09/%' then 'September' when code = 'Q' and (SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11)) like '31/12/%' then 'December' else 'Not Received' end as status,Can anyone please help? |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
hayleye
Starting Member
6 Posts |
Posted - 2010-06-14 : 10:01:20
|
| Sorry about that. Im using SQL 2005My query is like this:select account_group_name, accountgroup.code,SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11) AS account_date,case when accountgroup.code = 'Q' and (SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11)) like '31/03/%' then 'March'when accountgroup.code = 'Q' and (SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11)) like '30/06/%' then 'June'when accountgroup.code = 'Q' and (SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11)) like '30/09/%' then 'September'when accountgroup.code = 'Q' and (SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11)) like '31/12/%' then 'December'else 'Not Received' end as statusfrom account Is this possible to put it on one row? possibly a coalesce function? Im not 100% sure on how to do it though. |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-06-14 : 10:07:13
|
Try thisselect account_group_name, accountgroup.code,SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11) AS account_date,max(case when accountgroup.code = 'Q' and (SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11)) like '31/03/%' then 'March'when accountgroup.code = 'Q' and (SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11)) like '30/06/%' then 'June'when accountgroup.code = 'Q' and (SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11)) like '30/09/%' then 'September'when accountgroup.code = 'Q' and (SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11)) like '31/12/%' then 'December'else 'Not Received' end) as statusfrom accountgroup by account_group_name, accountgroup.code,SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11) Also why are you changing account_date to a varchar and doing the compares. Chk out DATEPART and DATENAME funcitons. |
 |
|
|
hayleye
Starting Member
6 Posts |
Posted - 2010-06-14 : 10:11:43
|
| our dates have times in them which i dont need and the report im doing is for quarter periods that the accounts werent received.This is then going in ssrs.Ill try that, hopefully it will work. |
 |
|
|
hayleye
Starting Member
6 Posts |
Posted - 2010-06-14 : 10:19:58
|
| Also, do I need to rather have 4 case statements so that each of the 4 months are on their own line because some acount have 3 of the 4 months received? |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-06-14 : 10:21:01
|
| What constitues a "Not Received" value ...I did not get that part. |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-06-14 : 10:22:00
|
This should give you some ideaselect account_group_name, accountgroup.code,SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11) AS account_date,max(case when accountgroup.code = 'Q' and datename(month,account_date) = 'March' then 'Received' else null end) as 'March'max(case when accountgroup.code = 'Q' and datename(month,account_date) = 'June' then 'Received' else null end) as 'June'max(case when accountgroup.code = 'Q' and datename(month,account_date) = 'September' then 'Received' else null end) as 'September'max(case when accountgroup.code = 'Q' and datename(month,account_date) = 'December' then 'Received' else null end) as 'December'from accountgroup by account_group_name, accountgroup.code,SUBSTRING(CONVERT(varchar(20),(account_date), 103), 0, 11) |
 |
|
|
hayleye
Starting Member
6 Posts |
Posted - 2010-06-14 : 10:22:27
|
| Not received is when the account has not been received for the quarter? it just what i put in as my else in the case. This is to see which accounts are missing for which quarters. |
 |
|
|
hayleye
Starting Member
6 Posts |
Posted - 2010-06-14 : 10:29:47
|
| Thank you, its working now, I used datepart to bring in the year from my account_date fields and grouped it on that.Now i have one line per year. |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-06-14 : 10:32:06
|
Great. You are welcome. |
 |
|
|
|
|
|
|
|