473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

SQL Query help

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!
Mar 14 '07 #1
5 3077
On Mar 14, 8:04 am, "Spook" <S...@mailinator.comwrote:
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

Mar 14 '07 #2
"Spook" <Sp***@mailinator.comwrote in message
news:Jz**************@newssvr23.news.prodigy.net.. .
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)
>
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

Mar 14 '07 #3
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.

Mar 14 '07 #4
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--" <jc*******@earthlink.netwrote in message
news:11**********************@p15g2000hsd.googlegr oups.com...
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.

Mar 14 '07 #5
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
av******@yandex.ru
(remove GOV to respond)

Mar 15 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
7
by: Simon Bailey | last post by:
How do you created a query in VB? I have a button on a form that signifies a certain computer in a computer suite. On clicking on this button i would like to create a query searching for all...
4
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
36
by: Liam.M | last post by:
hey guys, I have one last problem to fix, and then my database is essentially done...I would therefore very much appreciate any assistance anyone would be able to provide me with. Currently I...
5
by: elitecodex | last post by:
Hey everyone. I have this query select * from `TableName` where `SomeIDField` 0 I can open a mysql command prompt and execute this command with no issues. However, Im trying to issue the...
10
by: aaronrm | last post by:
I have a real simple cross-tab query that I am trying to sum on as the action but I am getting the "data type mismatch criteria expression" error. About three queries up the food chain from this...
11
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction....
4
by: Doris | last post by:
It does not look like my message is posting....if this is a 2nd or 3rd message, please forgive me as I really don't know how this site works. I want to apologize ahead of time for being a novice...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.