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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Not sum typeid = 18
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ozandalkiran
Starting Member

Turkey
10 Posts

Posted - 02/05/2013 :  03:19:58  Show Profile  Reply with Quote
hi
i wanna query
we have two tables salesall and stations.
we have to make sum of the daily sales. group by
product, stations, saledate but i dont want to add sumution which sales typeid = 18
how can i do
please help me

SELECT
convert(varchar(10),[SaleBegin],104) as SaleBegin
,S.[EPDKLicenseCode]
,[ProductID]
,sum([Volume]) as TotalVolume
FROM [PUMPOMAT_HO].[dbo].[SalesAll]
inner join stations S
on
S.RID = SalesAll.StationID
where (SaleBegin >='20130201' and SaleBegin < '20130205') and (ProductID= 22 or ProductID=23 )
group by S.[EPDKLicenseCode],SaleBegin,Volume,Total,ProductID

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/05/2013 :  03:24:31  Show Profile  Reply with Quote
sounds like this to me

SELECT 
[SaleBegin]
,S.[EPDKLicenseCode]
,[ProductID]
,sum(case when typeid=18 then 0 else [Volume] end) as TotalVolume
FROM [PUMPOMAT_HO].[dbo].[SalesAll]
inner join stations S
on
S.RID = SalesAll.StationID
where (SaleBegin >='20130201' and SaleBegin < '20130205') and (ProductID= 22 or ProductID=23 )
group by S.[EPDKLicenseCode],SaleBegin,ProductID

please dont convert dates to varchar for formatting, do it at your front end application

If above didnt give you what you were looking for, post some sample data and your expected output

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ozandalkiran
Starting Member

Turkey
10 Posts

Posted - 02/05/2013 :  03:57:04  Show Profile  Reply with Quote
thanks.
but i had extra problem when in group by date, for exampe should be shown 01.01.2013 22 140 01.01.2013 22 260 it is wrong
should be 01.01.2013 400
how can i solve this problem

quote:
Originally posted by visakh16

sounds like this to me

SELECT 
[SaleBegin]
,S.[EPDKLicenseCode]
,[ProductID]
,sum(case when typeid=18 then 0 else [Volume] end) as TotalVolume
FROM [PUMPOMAT_HO].[dbo].[SalesAll]
inner join stations S
on
S.RID = SalesAll.StationID
where (SaleBegin >='20130201' and SaleBegin < '20130205') and (ProductID= 22 or ProductID=23 )
group by S.[EPDKLicenseCode],SaleBegin,ProductID

please dont convert dates to varchar for formatting, do it at your front end application

If above didnt give you what you were looking for, post some sample data and your expected output

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/05/2013 :  03:59:35  Show Profile  Reply with Quote
post some data in understandable format and explain rules for getting your output

see below for guidelines

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.08 seconds. Powered By: Snitz Forums 2000