I have 2 tables in sql-server 2005 express DB
1. ItemSize Table
CREATE TABLE [dbo].[ItemSize](
[ID] [int] IDENTITY(1,1) NOT NULL,
[iSize] [int] NULL
) ON [PRIMARY]
2. SalesLog Table
CREATE TABLE [dbo].[SalesLog](
[SalesID] [int] IDENTITY(1,1) NOT NULL,
[MemoNo] [int] NULL,
[ProductCode] [int] NULL,
[Quantity] [int] NULL,
[Price] [int] NULL,
[pGroup] [int] NULL,
[pName] [nvarchar](30) NULL,
[pSize] [int] NULL,
[BillDate] [nchar](10) NULL,
CONSTRAINT [PK_SalesLog] PRIMARY KEY CLUSTERED
(
[SalesID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ItemSize table contains different size of drink like
100ml
200ml
300ml
400ml
500ml
I want ItemSize table columns (iSize) data as column like
ItemName 100ml 200ml 300ml 400ml 500ml
Drink A 10 20 5 4 30
Drink B 20 10 15 35 40
and data will be arranged from Sales table according to size and Product Name as shown above.
I have similar query to do so but its not dynamic. i mean if i add new size to ItemSize table then it will not be avilable to report. So i need solution for this.
Currently I am using this query to fetch sales according to item size
select i.gName,
isnull((select
sum(Quantity)from saleslog where BillDate='01-06-2010' and pSize=0 and
pGroup=i.gCode),0) as '0ml',
isnull((select
sum(Quantity)from saleslog where BillDate='01-06-2010' and pSize=180 and
pGroup=i.gCode),0) as '180ml',
isnull((select
sum(Quantity)from saleslog where BillDate='01-06-2010' and pSize=375 and
pGroup=i.gCode),0) as '375ml',
isnull((select
sum(Quantity)from saleslog where BillDate='01-06-2010' and pSize=500 and
pGroup=i.gCode),0) as '500ml',
isnull((select
sum(Quantity)from saleslog where BillDate='01-06-2010' and pSize=650 and
pGroup=i.gCode),0) as '650ml',
isnull((select
sum(Quantity)from saleslog where BillDate='01-06-2010' and pSize=750 and
pGroup=i.gCode),0) as '750ml',
isnull((select
sum(Quantity)from saleslog where BillDate='01-06-2010' and pSize=1000 and
pGroup=i.gCode),0) as '1000ml',
isnull((select
sum(Quantity)from saleslog where BillDate='01-06-2010' and
pSize=2000 and pGroup=i.gCode),0) as '2000ml'
from saleslog as s
inner join ItemGroup as i on s.pGroup=i.gCode
where BillDate='01-06-2010'
group by i.gCode, i.gName