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 |
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2008-11-17 : 17:12:17
|
This 'monster' is mucking up my day...I have a table that is created when I do a count of From_Date iterations:SELECT B.ID, B.From_Date, Count(B.From_Date) AS CountOfFrom_Date, B.Paid_AmtFROM BGROUP BY B.ID, B.From_Date, B.Paid_AmtHAVING (((B.Status)<>"D"));Here's the results of the code, ID From_Date CountOfFrom_Date Paid_Amt 5599 4/23/2008 1 $60.00 9699 4/11/2008 1 $40.92 0799 4/11/2008 1 $60.00 4799 5/14/2008 1 $60.00 7599 4/30/2008 1 $89.57However, I'd like to total those CountOfFrom_Date. Yknow, get a column that says '5'. I can do this in SAS, but I don't have SAS on my machine. I tried using SUM, but it treats the dates like an integer. Any idea which way to go on this? Also, I have an MS Access 'spec' question in the MS Access Forum than none dare touch!!Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-11-18 : 05:44:04
|
SELECT B.ID, B.From_Date, Count(B.From_Date) AS CountOfFrom_Date, B.Paid_Amt, ( SELECT Count(B.From_Date) FROM B WHERE (((B.Status)<>'D')) )Total_CountOfFrom_DateFROM BGROUP BY B.ID, B.From_Date, B.Paid_AmtHAVING (((B.Status)<>'D')) |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2008-11-18 : 05:52:21
|
[code]-- *** Test Data ***DECLARE @t TABLE( [ID] int NOT NULL ,From_Date datetime NOT NULL ,Paid_Amt money NOT NULL ,Status char(1) NOT NULL)INSERT INTO @t-- With test data best to put dates in ISO format to allow people to test-- without being concerned about locale settings.SELECT 5599, '2008-04-23', 60, 'A' UNION ALLSELECT 9699, '2008-04-11', 40.92, 'A' UNION ALLSELECT 799, '2008-04-11', 60, 'A' UNION ALLSELECT 4799, '2008-05-14', 60, 'A' UNION ALLSELECT 7599, '2008-04-30', 89.57, 'A'-- *** End Test Data ***-- Your querySELECT [ID], From_date, COUNT(From_date) AS CountOfFrom_Date, Paid_AmtFROM @t-- Only worth putting aggregate values in HAVING clauseWHERE Status <> 'D'GROUP BY [ID], From_date, Paid_Amt-- Normally best to count in the middle tier or front end.-- If really want an extra column you can use a derived table in SQL2000.SELECT T1.[ID], T1.From_date, COUNT(T1.From_date) AS CountOfFrom_Date, T1.Paid_Amt, T2.TotalFROM @t T1 CROSS JOIN ( SELECT COUNT(T21.From_date) AS Total FROM @t T21 WHERE T21.Status <> 'D' ) T2WHERE T1.Status <> 'D'GROUP BY T1.[ID], T1.From_date, T1.Paid_Amt, T2.Total-- You may also like to actually count the From_Date-- (ie Not the combination of ID, From_date and Paid_Amt)SELECT T1.[ID], T1.From_date, T3.CountOfFrom_Date, T1.Paid_Amt, T2.TotalFROM @t T1 CROSS JOIN ( SELECT COUNT(T21.From_date) AS Total FROM @t T21 WHERE T21.Status <> 'D' ) T2 JOIN ( SELECT T31.From_date, COUNT(*) AS CountOfFrom_Date FROM @t T31 WHERE T31.Status <> 'D' GROUP BY T31.From_date ) T3 ON T1.From_date = T3.From_dateWHERE T1.Status <> 'D'[/code] |
|
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2008-11-18 : 09:11:35
|
Thanks!Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
|
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2008-11-18 : 13:46:19
|
Too bad my MS Access Query doesn't like Cross Join (treats the command as a syntax error)....this would have solved my problem...quote: Originally posted by Ifor
-- *** Test Data ***DECLARE @t TABLE( [ID] int NOT NULL ,From_Date datetime NOT NULL ,Paid_Amt money NOT NULL ,Status char(1) NOT NULL)INSERT INTO @t-- With test data best to put dates in ISO format to allow people to test-- without being concerned about locale settings.SELECT 5599, '2008-04-23', 60, 'A' UNION ALLSELECT 9699, '2008-04-11', 40.92, 'A' UNION ALLSELECT 799, '2008-04-11', 60, 'A' UNION ALLSELECT 4799, '2008-05-14', 60, 'A' UNION ALLSELECT 7599, '2008-04-30', 89.57, 'A'-- *** End Test Data ***-- Your querySELECT [ID], From_date, COUNT(From_date) AS CountOfFrom_Date, Paid_AmtFROM @t-- Only worth putting aggregate values in HAVING clauseWHERE Status <> 'D'GROUP BY [ID], From_date, Paid_Amt-- Normally best to count in the middle tier or front end.-- If really want an extra column you can use a derived table in SQL2000.SELECT T1.[ID], T1.From_date, COUNT(T1.From_date) AS CountOfFrom_Date, T1.Paid_Amt, T2.TotalFROM @t T1 CROSS JOIN ( SELECT COUNT(T21.From_date) AS Total FROM @t T21 WHERE T21.Status <> 'D' ) T2WHERE T1.Status <> 'D'GROUP BY T1.[ID], T1.From_date, T1.Paid_Amt, T2.Total-- You may also like to actually count the From_Date-- (ie Not the combination of ID, From_date and Paid_Amt)SELECT T1.[ID], T1.From_date, T3.CountOfFrom_Date, T1.Paid_Amt, T2.TotalFROM @t T1 CROSS JOIN ( SELECT COUNT(T21.From_date) AS Total FROM @t T21 WHERE T21.Status <> 'D' ) T2 JOIN ( SELECT T31.From_date, COUNT(*) AS CountOfFrom_Date FROM @t T31 WHERE T31.Status <> 'D' GROUP BY T31.From_date ) T3 ON T1.From_date = T3.From_dateWHERE T1.Status <> 'D'
Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2008-11-20 : 05:29:36
|
I have been very careful never to learn anything about MS-Access but you could try replacing the CROSS JOIN with a comma (,). This is the old syntax.Also, you could try putting the query into a stored procedure and calling that from Access. |
|
|
|
|
|
|
|