473,404 Members | 2,213 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,404 software developers and data experts.

i would like to know an SQL statement that display sum totals, item id and item Name

Hi guys, i have purchases table where purchase items are selected and a daily purchase report generated. i use SQL to select item ID and Date of purchase then sum all the daily purchases.

since i have multiple items with unique IDs,like 'B001' FOR COCA-COLA, 'F001' FOR Fanta and so on, i would like When i select an item e.g 'B001' the name should also appear in some field after pressing run instead of only totals so that i can have 'fanta opening stock'or 'coca-cola opening stock' based on my input. the folloing is my current SQL query.
Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS [NUMBER OF PURCHASES], Sum([UNITS PURCHASED]) AS [OPENING STOCK]
  2. FROM PURCHASES
  3. WHERE (((PURCHASES.[Item ID]) Like [ENTER ITEM CODE:] & "*") AND ((PURCHASES.[Date Purchased]) Like [ENTER DATE OF PURCHASE:] & "*"));
currently i only get two fiedls[number of purchase] and [opening stock] i want item ID and Name to also appear on the query. thanks
Jun 19 '10 #1

✓ answered by NeoPa

A better way to handle getting the correct name for something like this would be to store the data in an Item or Product table which you link into the [PURCHASES] table by storing the Primary Key of the Product table in the [PURCHASES] records.

4 2564
NeoPa
32,556 Expert Mod 16PB
Do you have the name of the item stored in [PURCHASES] as well as the [Item ID]?

If so, then you shouldn't really (See Normalisation and Table structures).

However, if this is your structure then you can do it, albeit with a good expectation of confusing data if anything is not exactly as it should be.
Expand|Select|Wrap|Line Numbers
  1. SELECT   [Item ID]
  2.         ,[NameField]
  3.         ,Count(*) AS [NUMBER OF PURCHASES]
  4.         ,Sum([UNITS PURCHASED]) AS [OPENING STOCK]
  5. FROM     [PURCHASES]
  6. WHERE    ((PURCHASES.[Item ID] Like [ENTER ITEM CODE:] & '*')
  7.   AND    (PURCHASES.[Date Purchased] Like [ENTER DATE OF PURCHASE:] & '*'))
  8. GROUP BY [Item ID]
  9.         ,[NameField]
Jun 19 '10 #2
NeoPa
32,556 Expert Mod 16PB
A better way to handle getting the correct name for something like this would be to store the data in an Item or Product table which you link into the [PURCHASES] table by storing the Primary Key of the Product table in the [PURCHASES] records.
Jun 19 '10 #3
Thanks Neopa, indeed i have atable called purchases with primary key and another table called products with a primary key. i.ve linked the two tables and created a form called purchases. but then on the report i need to see each i tem seperately on a single page. like total coca cola purchases and fanta purchases summary. since my report is created from the purchases query, the reason why i decided to sum all purchases. thanks
Jun 19 '10 #4
NeoPa
32,556 Expert Mod 16PB
In that case your SQL would need to include a JOIN to the [PRODUCTS] table in th FROM clause.

Glad this has helped & Welcome to Bytes!
Jun 19 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: michaaal | last post by:
My address bar displays the following info... http://www.in-house-domain.com/cust.asp http://www.in-house-domain.com/phnumb.asp http://www.in-house-domain.com/Invect/qult11.asp I would like...
5
by: trpplayer | last post by:
Dear, I want to create an page where they have to enter ther name in an prompt. by the name they entered i want to open different html page's example: <SCRIPT LANGUAGE="JAVASCRIPT"...
1
by: Claudia Fong | last post by:
Hi everyone I have 6 items "inside" the combobox. I would like to display the first item (eg 2005) when load the form. Another items in the combobox are 2004,2003,2002,2001 and 2000. In VB,...
23
by: Bob Greschke | last post by:
I miss being able to do something like this in Python 1f (I = a.find("3")) != -1: print "It's here: ", I else: print "No 3's here" where I gets assigned the index returned by find() AND the...
3
by: divya | last post by:
Hi, I have a table tblbwday with 2 fields Name and Birthday.I have written this script for displaying evryday names of the people on that day. <% set objConn...
1
by: sharmilah | last post by:
Hi All Thanks for the replies to my previous questions. I have some code down there to enable editing of a field called 'Usr_Surname' if the value of the variable 'authorise' is one . If the...
2
sharijl
by: sharijl | last post by:
Can someone explain the correct way to query SQL with a like or statement. so I want to SELECT * from table where column like 'Something%' or like 'somethingelse%' I tried it and get syntax...
16
Ranjan kumar Barik
by: Ranjan kumar Barik | last post by:
Hello !!! I am Ranjan. can the selected item of a combo box be displayed on the same page. I mean when I just click the item of a combo box it will atonce displayed on the page. Thanks !!!
1
by: =?Utf-8?B?Um9iZXJ0IFNtaXRo?= | last post by:
Hi, I have a problem whereby I have a gridview bound to a dataset with a large number of items, However when I select an item near the bottom by using the scrollbar, when the gridview gets...
1
by: JTB07 | last post by:
Hello, I have a form that displays project information, and on that form is a subform that displays info about invoices related to that project. This info includes things like invoice number,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.