SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 More Distinct
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tranquilraven
Starting Member

19 Posts

Posted - 06/11/2012 :  12:15:19  Show Profile  Reply with Quote
I am trying to create a CSV file that lists each state only once as well as each zip code for the state only once. Instead I am getting the states listed over and over and the zip code listed over and over. Could someone please teach me how to do this properly. Below is a sample of the data being created and below that is my sql query.


CA	92345	7.75	0
CA	92345	7.75	2.32
CA	92345	7.75	2.55
CA	92345	7.75	2.79
CA	92345	7.75	3.1
CA	92346	7.75	1.39
CA	92363	7.75	2.79
CA	92371	7.75	3.1
CA	92372	7.75	0.46
CA	92374	7.75	0.23
CA	92376	7.75	0
CA	92392	7.75	0
CA	92392	7.75	0.92
CA	92392	7.75	2.32
CA	92392	7.75	2.79
CA	92392	7.75	11.61
CA	92392	7.75	15.5
CA	92394	7.75	0.46
CA	92395	7.75	0
CA	92395	7.75	0.15
CA	92395	7.75	1.86
CA	92395	7.75	2.32
CA	92397	7.75	0
CA	92399	7.75	2.32
CA	92399	7.75	15.5
CA	92404	7.75	0
CA	92404	7.75	2.32
CA	92405	8	0
CA	92405	8	1.76
CA	92407	7.75	0
CA	92407	7.75	0.79



Here is my query:



SELECT DISTINCT o.ShipState AS 'U.S. State', t.TaxPostalCode AS 'Zip Code', t.Tax1_Percent AS 'Tax Rate', SUM( o.SalesTax1 ) AS 'Total Sales Tax'
FROM Orders AS o
INNER JOIN Tax AS t ON o.ShipPostalCode = t.TaxPostalCode
GROUP BY o.ShipState, t.TaxPostalCode, t.Tax1_Percent, o.SalesTax1
ORDER BY o.ShipState, t.TaxPostalCode, t.Tax1_Percent

JimL
SQL Slinging Yak Ranger

USA
1530 Posts

Posted - 06/11/2012 :  12:37:10  Show Profile  Visit JimL's Homepage  Reply with Quote
change
GROUP BY o.ShipState, t.TaxPostalCode, t.Tax1_Percent, o.SalesTax1
ORDER BY o.ShipState, t.TaxPostalCode, t.Tax1_Percent

To
GROUP BY o.ShipState, t.TaxPostalCode, t.Tax1_Percent
ORDER BY o.ShipState, t.TaxPostalCode










Jim
Users <> Logic

Edited by - JimL on 06/11/2012 12:37:39
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000