dbTalk Databases Forums  

How would I write this?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss How would I write this? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: How would I write this? - 05-19-2010 , 10:53 PM






Your sample data does not seem to correspond to the desired results.
After cleaning your data and code a bit, commenting out the components
that you did not provide (like the function and linked server),
running the code results in this:

doc totovrs totpnds totovr totpnd weekdat dowrdat
---- ----------- ----------- ----------- -----------
----------------------- -----------------------
009 0 6 0 0 2010-05-14
00:00:00.000 2010-05-28 00:00:00.000
200 0 6 0 0 2010-05-14
00:00:00.000 2010-05-28 00:00:00.000
009 0 0 0 6 2010-05-14
00:00:00.000 2010-05-28 00:00:00.000
200 0 0 0 6 2010-05-14
00:00:00.000 2010-05-28 00:00:00.000

Not really sure how you need this data summarized. Here is just a
guess. eliminate both insert queries into PendingDiary and use the
following insert query:

INSERT INTO PendingDiary
(doc, totovrs, totpnds, totovr, totpnd, weekdat, dowrdat)
SELECT doc,
COUNT(CASE WHEN clear = 'O' AND SDW = 'Y' THEN doc END) AS
totovrs,
COUNT(CASE WHEN SDW = 'Y' THEN doc END) AS totpnds,
COUNT(CASE WHEN clear = 'O' AND SDW = 'N' THEN doc END) AS
totovr,
COUNT(CASE WHEN SDW = 'Y' THEN doc END) AS totopnd,
weekdat,
dowrdat
FROM #temppend
WHERE SDW IN ('Y', 'N')
GROUP BY doc, weekdat, dowrdat;

This results in the following summarized data:

doc totovrs totpnds totovr totpnd weekdat dowrdat
---- ----------- ----------- ----------- -----------
----------------------- -----------------------
009 0 6 0 6 2010-05-14
00:00:00.000 2010-05-28 00:00:00.000
200 0 6 0 6 2010-05-14
00:00:00.000 2010-05-28 00:00:00.000


--
Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
  #12  
Old   
jj297
 
Posts: n/a

Default Re: How would I write this? - 05-20-2010 , 07:58 AM






On May 19, 10:53*pm, Plamen Ratchev <Pla... (AT) SQLStudio (DOT) com> wrote:
Quote:
Your sample data does not seem to correspond to the desired results.
After cleaning your data and code a bit, commenting out the components
that you did not provide (like the function and linked server),
running the code results in this:

doc *totovrs * * totpnds * * totovr * * *totpnd * * *weekdat dowrdat
---- ----------- ----------- ----------- -----------
----------------------- -----------------------
009 *0 * * * * * 6 * * * * * 0 * * * * * 0 * * * * * 2010-05-14
00:00:00.000 2010-05-28 00:00:00.000
200 *0 * * * * * 6 * * * * * 0 * * * * * 0 * * * * * 2010-05-14
00:00:00.000 2010-05-28 00:00:00.000
009 *0 * * * * * 0 * * * * * 0 * * * * * 6 * * * * * 2010-05-14
00:00:00.000 2010-05-28 00:00:00.000
200 *0 * * * * * 0 * * * * * 0 * * * * * 6 * * * * * 2010-05-14
00:00:00.000 2010-05-28 00:00:00.000

Not really sure how you need this data summarized. Here is just a
guess. eliminate both insert queries into PendingDiary and use the
following insert query:

INSERT INTO PendingDiary
(doc, totovrs, totpnds, totovr, totpnd, weekdat, dowrdat)
SELECT doc,
* * * *COUNT(CASE WHEN clear = 'O' AND SDW = 'Y' THEN doc END) AS
totovrs,
* * * *COUNT(CASE WHEN SDW = 'Y' THEN doc END) AS totpnds,
* * * *COUNT(CASE WHEN clear = 'O' AND SDW = 'N' THEN doc END) AS
totovr,
* * * *COUNT(CASE WHEN SDW = 'Y' THEN doc END) AS totopnd,
* * * *weekdat,
* * * *dowrdat
FROM #temppend
WHERE SDW IN ('Y', 'N')
GROUP BY doc, weekdat, dowrdat;

This results in the following summarized data:

doc *totovrs * * totpnds * * totovr * * *totpnd * * *weekdat dowrdat
---- ----------- ----------- ----------- -----------
----------------------- -----------------------
009 *0 * * * * * 6 * * * * * 0 * * * * * 6 * * * * * 2010-05-14
00:00:00.000 2010-05-28 00:00:00.000
200 *0 * * * * * 6 * * * * * 0 * * * * * 6 * * * * * 2010-05-14
00:00:00.000 2010-05-28 00:00:00.000

--
Plamen Ratchevhttp://www.SQLStudio.com
Plamen,

Thanks so much I'm sorry I left out so much. I inherited this code
and as you can see it's so confusing to me. The Programmer retired so
I'm left figuring it out. Anyway what you presented worked it wasn't
coming out tallied but now it is. Sorry for bothering you with this
as it was a complicated procedure. Thanks for you help.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.