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 |
|
mind_grapes
Yak Posting Veteran
71 Posts |
Posted - 2009-12-07 : 09:20:12
|
Hi all, hope someone can help me with this problem i have.i have the following:SELECT store_code, Store_Name, 0 as sequenceNr, 'Shop ' + store_name + ' = ' as OutputLineFROM stores WHERE storeStatus = 'ON' and store_code NOT IN ( select ar_store_code from pgm_action_responses ) UNION ALLSELECT s.store_code,s.Store_Name, pss_story_id as sequenceNr, st_title + ' st_status = ' + CASE WHEN st_status = '1' THEN 'yes' ELSE 'no' END as OutputLineFrom pgm_store_stories as ss inner join pgm_stories as st on ss.pss_story_id = st.st_id inner join stores as s on s.store_code = ss.pss_store_codeWhere pss_story_id > '7829' ORDER BY 1, 2 This SQL query is a modified version of an SQL query provided by a kind person from anbother forum, and does almost what i need it to do. However there are two additions things I was hoping to show.I'd like to display 'no' results only and then provide a total number of all 'no' per store_code?At the moment it displays like this:store_code | Store_name | sequenceNr | OutputLine001 | Support Centre | 7830 | Sony Cash back 02.11.09 st_status = yes001 | Support Centre | 7831 | Vacancy Report update 2.11.09 st_status = noAny pointers would be appreciated.Kind regardsMG |
|
|
Kumar_Anil
Yak Posting Veteran
68 Posts |
Posted - 2009-12-07 : 13:06:37
|
| MG,I dont know where the yes or no is pulled from. However as I see the union all I guess it should come from both the queries right ? But a quick glance tells me that the 'yes' or 'no' value is pulled from the second part right, so I would just add another "and" as st_status = "no". If not then you write an outer query to parse the last 2 parts of the string after trimming as follows - RIGHT(RTRIM(Outputline),2) = "no".regards,Anil |
 |
|
|
Kumar_Anil
Yak Posting Veteran
68 Posts |
Posted - 2009-12-07 : 13:40:26
|
| What I meant was the following -regards,Anilselect A.* from (SELECT store_code, Store_Name, 0 as sequenceNr, 'Shop ' + store_name + ' = ' as OutputLineFROM stores WHERE storeStatus = 'ON' and store_code NOT IN ( select ar_store_code from pgm_action_responses ) UNION ALLSELECT s.store_code,s.Store_Name, pss_story_id as sequenceNr, st_title + ' st_status = ' + CASE WHEN st_status = '1' THEN 'yes' ELSE 'no' END as OutputLineFrom pgm_store_stories as ss inner join pgm_stories as st on ss.pss_story_id = st.st_id inner join stores as s on s.store_code = ss.pss_store_codeWhere pss_story_id > '7829' )A where right(rtrim(A.outputline),2) = "no"Order by 1, 2 |
 |
|
|
mind_grapes
Yak Posting Veteran
71 Posts |
Posted - 2009-12-08 : 04:43:47
|
Hi Anil, you're great, thank you. it works a treat.I have to be as smart as you, a long way to go i think .It brings back results like this:001 shop one 7876 job that remains st_status = no001 shop one 7877 job that remains st_status = no001 shop one 7878 job that remains st_status = no002 shop two 7876 job that remains st_status = no002 shop two 7877 job that remains st_status = no002 shop two 7878 job that remains st_status = no002 shop two 7879 job that remains st_status = noDo you know if it is possible to have a total number after each shop, for example:001 shop one 7876 job that remains st_status = no001 shop one 7877 job that remains st_status = no001 shop one 7878 job that remains st_status = nototal = 3002 shop two 7876 job that remains st_status = no002 shop two 7877 job that remains st_status = no002 shop two 7878 job that remains st_status = no002 shop two 7879 job that remains st_status = nototal = 4etc.Any advice or pointers, would be appreciated.Kind regardsMG |
 |
|
|
mind_grapes
Yak Posting Veteran
71 Posts |
Posted - 2009-12-08 : 05:15:04
|
| HI Anil, I've been playing around with it, and in the database there are sequenceNr that are not appearing in the output. It stops at 7877, but there are numbers at 7920. I cant work out why this is?RegardsMG |
 |
|
|
mind_grapes
Yak Posting Veteran
71 Posts |
Posted - 2009-12-10 : 07:19:16
|
Hi all, i was over complicating it, something i always do.This did the trick in the end. select pss_store_code ,store_name ,pss_story_id ,st_title from stores as s full join pgm_store_stories as ss on s.store_code = ss.pss_store_code full join pgm_stories as st on ss.pss_story_id = st.st_id where s.storeStatus = 'on' AND st_release_date > '2009-12-01' and pss_store_code not in ( select ar_store_code from pgm_action_responses where ar_story_id > '7939' ) order by pss_store_code, pss_story_id But i was wondering if it is possible to return the number of columns in ,st_title, i know the following doesnt work, but i was thinking something like this: select pss_store_code ,store_name ,pss_story_id ,count("st_title")any ideas would be great.Kind regardsMG |
 |
|
|
|
|
|
|
|