dbTalk Databases Forums  

SQL Query help

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


Discuss SQL Query help in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Spook
 
Posts: n/a

Default SQL Query help - 03-13-2007 , 09:04 PM






I have a SQL table with the following fields:

accounts, orderid's and datetime



Account OrderID Datetime
1 1 2007-03-01 09:30
1 2 2007-03-01 09:35
10 3 2007-03-01 10:30
2 4 2007-03-01 11:30
10 5 2007-03-01 12:30


Using Query Analyzer, I'd like to run a query where the results are a count
of orderId's by account on any given day like what I have below:



Account Orders Date
1 2 2007-03-01
2 1 2007-03-01
10 2 2007-03-01


Eventually getting it to this output:

Date TotalOrder
2007-03-01 5

Thanks for the help!



Reply With Quote
  #2  
Old   
M A Srinivas
 
Posts: n/a

Default Re: SQL Query help - 03-13-2007 , 11:35 PM






On Mar 14, 8:04 am, "Spook" <S... (AT) mailinator (DOT) com> wrote:
Quote:
I have a SQL table with the following fields:

accounts, orderid's and datetime

Account OrderID Datetime
1 1 2007-03-01 09:30
1 2 2007-03-01 09:35
10 3 2007-03-01 10:30
2 4 2007-03-01 11:30
10 5 2007-03-01 12:30

Using Query Analyzer, I'd like to run a query where the results are a count
of orderId's by account on any given day like what I have below:

Account Orders Date
1 2 2007-03-01
2 1 2007-03-01
10 2 2007-03-01

Eventually getting it to this output:

Date TotalOrder
2007-03-01 5

Thanks for the help!
Try this

declare @tbla table (account int,orderid int, record_date datetime)
insert into @tbla values (1,1,'2007-03-01 09:30')
insert into @tbla values (1,2,'2007-03-01 09:35')
insert into @tbla values (10,3,'2007-03-01 10:30')
insert into @tbla values (2,4,'2007-03-01 11:30')
insert into @tbla values (10,5,'2007-03-01 12:30')

select
T. from (
select account,convert(varchar(10),record_date,101) as record_date,
count(*) as NoofOrders
from @tbla
group by
account,
convert(varchar(10),record_date,101)
with cube ) T
where
(
(T.account is not null and T.record_date is not null )
OR
(T.account is null and T.record_date is null )
)

M A Srinivas



Reply With Quote
  #3  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: SQL Query help - 03-13-2007 , 11:44 PM



"Spook" <Spook (AT) mailinator (DOT) com> wrote

Quote:
I have a SQL table with the following fields:

accounts, orderid's and datetime



Account OrderID Datetime
1 1 2007-03-01 09:30
1 2 2007-03-01 09:35
10 3 2007-03-01 10:30
2 4 2007-03-01 11:30
10 5 2007-03-01 12:30


Using Query Analyzer, I'd like to run a query where the results are a
count of orderId's by account on any given day like what I have below:



Account Orders Date
1 2 2007-03-01
2 1 2007-03-01
10 2 2007-03-01

Here is a query to get you this one:

SELECT Account, COUNT(*) AS Orders, CONVERT(CHAR(10), [Datetime], 126) AS
Date
FROM Orders
GROUP BY Account, CONVERT(CHAR(10), [Datetime], 126)

Quote:
Eventually getting it to this output:

Date TotalOrder
2007-03-01 5

And here is the next:

SELECT CONVERT(CHAR(10), [Datetime], 126) AS Date, COUNT(*) AS TotalOrders
FROM Orders
GROUP BY CONVERT(CHAR(10), [Datetime], 126)

Regards,

Plamen Ratchev
http://www.SQLStudio.com





Reply With Quote
  #4  
Old   
--CELKO--
 
Posts: n/a

Default Re: SQL Query help - 03-14-2007 , 12:16 PM



the other thing you need to consider is adding a constraint to be sure
that the time is always set to 00:00:00 Hrs so that you do not have
fix it on the fly in your queries. Mop the floor but also fix the
leak.


Reply With Quote
  #5  
Old   
Spook
 
Posts: n/a

Default Re: SQL Query help - 03-14-2007 , 12:42 PM



Thank you all for your help! Plamen's idea was exactly what I needed. Celko,
We need the datetime to be accurate to verify the order times in case of
delays in dispatching.





"--CELKO--" <jcelko212 (AT) earthlink (DOT) net> wrote

Quote:
the other thing you need to consider is adding a constraint to be sure
that the time is always set to 00:00:00 Hrs so that you do not have
fix it on the fly in your queries. Mop the floor but also fix the
leak.




Reply With Quote
  #6  
Old   
avode
 
Posts: n/a

Default Re: SQL Query help - 03-15-2007 , 02:29 AM



Spook,

You may want to look at using computed columns,
see SQL Server Books Online for more information.

CREATE TABLE #journal(
account INTEGER NOT NULL,
orderid INTEGER NOT NULL,
record_date DATETIME NOT NULL,
yy AS DATEPART(YY, record_date),
mm AS DATEPART(MM, record_date),
dd AS DATEPART(DD, record_date),
PRIMARY KEY NONCLUSTERED(record_date, account, orderid),
UNIQUE CLUSTERED(yy, mm, dd, account, orderid));

INSERT INTO #journal VALUES ( 1, 1, '20070301 09:30');
INSERT INTO #journal VALUES ( 1, 2, '20070301 09:35');
INSERT INTO #journal VALUES (10, 3, '20070301 10:30');
INSERT INTO #journal VALUES ( 2, 4, '20070301 11:30');
INSERT INTO #journal VALUES (10, 5, '20070301 12:30');

SET STATISTICS IO ON;

SELECT account, COUNT(*), yy, mm, dd
FROM #journal
GROUP BY yy, mm, dd, account;

SET STATISTICS IO OFF;

DROP TABLE #journal;

---
Andrey Odegov
avodeGOV (AT) yandex (DOT) ru
(remove GOV to respond)


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.