Showing posts with label total. Show all posts
Showing posts with label total. Show all posts

Wednesday, March 28, 2012

Query problem

I have a count code that works just fine...to get the total..

I need to modify it to select the total where zip = Session("zip")

can anyone help me?

Dim cmd3AsNew Data.SqlClient.SqlCommand("Select count (*) from CustomerInfo", MyConn)

cmd.Connection.Open()

Dim count3AsInteger = cmd3.ExecuteScalar()'this contains the number of records

Label_registeredusers.Text = count3

cmd.Connection.Close()

you can use a parameter in your query to limit the results The parameterized query will be much safer than simply concatenating in the zip code as text in your sql statement as that would expose you to the possibility of a sql injection attack.

Dim paramZipAs New System.Data.SqlClient.SqlParameterparamZip.ParameterName ="@.zip"param.Value = Session("zip")Dim cmd3As New Data.SqlClient.SqlCommand("Select count (*) from CustomerInfo WHERE zip=@.zip", MyConn) cmd3.Parameters.Add(paramZip)cmd.Connection.Open()Dim count3As Integer = cmd3.ExecuteScalar()'this contains the number of recordsLabel_registeredusers.Text = count3cmd.Connection.Close()
|||

You could do this:

Dim cmd3AsNew Data.SqlClient.SqlCommand("Select count (*) from CustomerInfo WHERE zip = '" & Session("zip") &"'", MyConn)

But it would be better to use command Parameters like :|||

A lot easier than I thought... Thank you very much!

sql

Monday, March 26, 2012

query problem

Hey,
Struggeling with a query, maybe someone can point me out.
Table structure : per contract per observation year the total payments
Contract Observationyear Paiments
A 1999 10
A 2000 4
A 2001 3
A 2002 2
B 1999 3
B 2000 1
B 2001 8
B 2002 2
Question : per contract (1line per contract) the sum of the payments on 2001
and 2002 taking the previous years into account.
Contract 2001 2002
A 17 19
B 12 14
I tried already several querys but no luck.
Thanks for help
JacSELECT contract,
SUM(CASE WHEN observationyear<=2001 THEN payment END) AS yr2001,
SUM(CASE WHEN observationyear<=2002 THEN payment END) AS yr2002,
SUM(CASE WHEN observationyear<=2003 THEN payment END) AS yr2003,
SUM(CASE WHEN observationyear<=2004 THEN payment END) AS yr2004,
SUM(CASE WHEN observationyear<=2005 THEN payment END) AS yr2005
FROM ContractPayments
GROUP BY contract
David Portas
SQL Server MVP
--|||Thanks david .
I did the following and it was quite wrong :
SELECT contract,
> SUM(CASE observationyear WHEN 2005 THEN 0
when 2004 then 0
when 2003 then 0
when 2002 then 0
else payment END) AS yr2001,
> SUM(CASE observationyear WHEN 2005 THEN 0
when 2004 then 0
when 2003 then 0
else payment END) AS yr2002
FROM ContractPayments
GROUP BY contract
That was the result :
Column 'observationyear is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
"David Portas" wrote:

> SELECT contract,
> SUM(CASE WHEN observationyear<=2001 THEN payment END) AS yr2001,
> SUM(CASE WHEN observationyear<=2002 THEN payment END) AS yr2002,
> SUM(CASE WHEN observationyear<=2003 THEN payment END) AS yr2003,
> SUM(CASE WHEN observationyear<=2004 THEN payment END) AS yr2004,
> SUM(CASE WHEN observationyear<=2005 THEN payment END) AS yr2005
> FROM ContractPayments
> GROUP BY contract
> --
> David Portas
> SQL Server MVP
> --
>|||It works for me - see below. Please include CREATE and INSERT
statements like this with future posts so that both you and those who
reply can test solutions against some actual data:
CREATE TABLE ContractPayments (contract CHAR(1) NOT NULL,
observationyear INTEGER NOT NULL, payment INTEGER NOT NULL, PRIMARY KEY
(contract,observationyear))
INSERT INTO ContractPayments (contract, observationyear, payment)
SELECT 'A', 1999, 10 UNION ALL
SELECT 'A', 2000, 4 UNION ALL
SELECT 'A', 2001, 3 UNION ALL
SELECT 'A', 2002, 2 UNION ALL
SELECT 'B', 1999, 3 UNION ALL
SELECT 'B', 2000, 1 UNION ALL
SELECT 'B', 2001, 8 UNION ALL
SELECT 'B', 2002, 2
SELECT contract,
SUM(CASE observationyear WHEN 2005 THEN 0
WHEN 2004 THEN 0
WHEN 2003 THEN 0
WHEN 2002 THEN 0
ELSE payment END) AS yr2001,
SUM(CASE observationyear WHEN 2005 THEN 0
WHEN 2004 THEN 0
WHEN 2003 THEN 0
ELSE PAYMENT END) AS yr2002
FROM ContractPayments
GROUP BY contract
Result:
contract yr2001 yr2002
-- -- --
A 17 19
B 12 14
(2 row(s) affected)
David Portas
SQL Server MVP
--

Query prob?

Hi,

I got table that contains prodcode, qty and Box, I want to display all product and their total QTY. like


Item

prodcode

qty

box


1

ADEN100550003006

2

000001


2

ADEN100550003006

3

000001


3

ADEN100550003006

6

000001


4

ADEN100550003006

12

000001


5

ADEN100550003006

15

000001


6

ADEN100550003006

18

000001


7

ADEN100550003006

21

000001


8

ADEN100550003006

24

000001


9

ADEN100550003006

36

000001


10

ADEN100550003006

48

000001


11

ADEN100550003006

72

000001


12

ADEN100550003006

144

000001


13

ADEN100550003006

157

000001


14

ADEN100550003006

360

000001


*** all thses product got same bar code so I want to sum all these QTY and display the total QTY.
I am executing this query right nw.

select WHBOXDET.prodcode,WHBOXDET.qty,WHBOXDET.box ,sum (WHBOXDET.qty) from WHBOXDET where prodcode = 'ADEN100550003006' GROUP BY prodcode,box,qty ;

But it Displays result like :

Created: 26/07/2007 12:55:03

Item

prodcode

qty

box

EXPR

1

ADEN100550003006

2

000001

20

2

ADEN100550003006

3

000001

15

3

ADEN100550003006

6

000001

54

4

ADEN100550003006

12

000001

60

5

ADEN100550003006

15

000001

15

6

ADEN100550003006

18

000001

18

7

ADEN100550003006

21

000001

21

8

ADEN100550003006

24

000001

24

9

ADEN100550003006

36

000001

36

10

ADEN100550003006

48

000001

48

11

ADEN100550003006

72

000001

72

12

ADEN100550003006

144

000001

144

13

ADEN100550003006

157

000001

157

14

ADEN100550003006

360

000001

360

Plz tell me how to do it.
Thx

You need to remove "WHBOXDET.qty" from your select list and "qty" from your group by, otherwise it is factored into the uniqueness of each row. It should look like this:

Code Snippet

SELECT WHBOXDET.prodcode, WHBOXDET.box, sum(WHBOXDET.qty) qty from WHBOXDET where prodcode = 'ADEN100550003006' GROUP BY prodcode, box

|||sorted