Challenge for SQL Query Experts

Hello Everybody.
I'm sorry but my English language is not good enough.
I Have 2 tables.
First 'tb_Transaction_In' Contains all sales  invoices (items Output from the stock)
Second 'tb_Transaction_Out' Contains all purchases invoices  (items input  to the stock)
I want to add  to every Invoice (which has many items) a column which calculate the cost value.
example:
stock 1 has an input invoice:
Quantity of the invoice items 'x' is 10
UTPrice is 100 for the one 'x'
so my stock now has 10 pieces of x and the cost of the x is 100$
=====================
then the first Output:
Quantity of the invoice items 'x' is 3
Current Cost is 100$ for the one 'x'
My stock has 7 pieces of x 
=====================
then the second input:
Quantity of the invoice items 'x' is 5
UTPrice is 75$ for the one 'x'
so my stock now has 7+5 pieces of x and the cost of the x is (100*7 + 75*5) / 7+5
=====================
then the seconde Output:
Quantity of the invoice items 'x' is 10
Current Cost is 89.5 $ for the one 'x'
My stock has 2 pieces of x 
=====================
ID Stock
Product Price
QtyIn QtyOut
CurrentQty Cost
1 R1
x 100$
10 0 10
100$
2 R1
x 150$
0 3 7
100$
3 R3
x 75$ 5
0 12
89.5
4 R2
x 105$
0 10 2
89.5
This is the DDL:
USE [6]
GO
/****** Object:  Table [dbo].[tb_Transaction_In]    Script Date: 25/03/2014 1:19:25 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tb_Transaction_In](
[Date] [datetime] NOT NULL,
[Quantity] [decimal](18, 5) NOT NULL,
[ProductCode] [nvarchar](50) NOT NULL,
[StockCode] [nvarchar](23) NOT NULL,
[Type] [nvarchar](5) NOT NULL,
[UTPrice] [decimal](18, 5) NULL,
[NotUsedQty] [decimal](18, 5) NULL,
[ID] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_dbo.tb_Transaction_in] PRIMARY KEY CLUSTERED 
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[tb_Transaction_Out]    Script Date: 25/03/2014 1:19:25 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tb_Transaction_Out](
[Date] [datetime] NOT NULL,
[Quantity] [decimal](18, 5) NOT NULL,
[ProductCode] [nvarchar](50) NOT NULL,
[StockCode] [nvarchar](50) NOT NULL,
[Type] [nvarchar](5) NOT NULL,
[PendingAmount] [decimal](18, 5) NULL,
[UTPrice] [decimal](18, 5) NULL,
[ID] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_dbo.tb_Transaction_out] PRIMARY KEY CLUSTERED 
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(48.00000 AS Decimal(18, 5)), N'TX121/50/W', N'RI', N'AI', CAST(11.09000 AS Decimal(18,
5)), NULL, N'3b626123-799f-4e92-8fec-003cbeb2f6fc')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'RVS/SP-5-669-59', N'RI', N'AI', CAST(0.00045 AS Decimal(18,
5)), NULL, N'b65e6927-969f-4bfb-aa0e-0273066bbdea')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501341DBC AS DateTime), CAST(20.00000 AS Decimal(18, 5)), N'YYIS-ICF2S13H1LDK*255', N'RP255', N'ST', CAST(61.08273
AS Decimal(18, 5)), NULL, N'82de4604-ad11-40ae-a8d2-0926444123cd')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A5014657A5 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'BJ3361L', N'RO', N'RSL', CAST(7905.00000 AS Decimal(18,
5)), NULL, N'8f3350d5-5cb7-49da-8b69-0e0c61d6c546')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'YYDG-CDM-R 35W', N'RI', N'AI', CAST(152.58000 AS Decimal(18,
5)), NULL, N'f10eca1d-7215-4015-89de-10f629bc4091')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A50121C4DC AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'AF700F0/1T1.PT.01', N'RT', N'ST', CAST(82.79003 AS Decimal(18,
5)), NULL, N'617576aa-5c6a-406c-8d76-168f8ba9d62b')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A5012F0B4C AS DateTime), CAST(2.00000 AS Decimal(18, 5)), N'YYHE-SIS180 220V*255', N'RP255', N'ST', CAST(49.64832 AS
Decimal(18, 5)), NULL, N'a524aea7-2c75-4818-b6ac-196e80aef011')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(12.00000 AS Decimal(18, 5)), N'FO191116', N'RI', N'AI', CAST(132.52000 AS Decimal(18,
5)), NULL, N'e1804c98-a753-44d2-9fe0-1b41a04099d2')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501341DBC AS DateTime), CAST(21.00000 AS Decimal(18, 5)), N'YYIS-IZT-2S26-M5-LD*255', N'RP255', N'ST', CAST(243.82820
AS Decimal(18, 5)), NULL, N'93217ef1-a1c5-47af-8c49-1c8cacaf7f35')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'CIDV-ITACA/48*GOLD', N'RT', N'AI', CAST(441.94000 AS Decimal(18,
5)), NULL, N'a9de52c4-a814-4db8-8db8-1e7a391f6450')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'FO25131', N'RI', N'AI', CAST(45.70000 AS Decimal(18, 5)),
NULL, N'57480184-b8a7-43f3-a522-effc3733bcb7')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'NNSRC-181', N'RP303', N'AI', CAST(272.04000 AS Decimal(18,
5)), NULL, N'292d363f-9be4-471c-b868-f41fc1a74138')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A5012F0B4C AS DateTime), CAST(42.00000 AS Decimal(18, 5)), N'YYHE-TC126-42*255', N'RP255', N'ST', CAST(47.80049 AS
Decimal(18, 5)), NULL, N'732adced-785d-4c0a-b675-f436e9fcd430')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'SKDVW0808EA', N'RI', N'AI', CAST(3862.27000 AS Decimal(18,
5)), NULL, N'f00853e4-f585-4cf7-994c-f4edf8cb421b')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(3.00000 AS Decimal(18, 5)), N'LLIL-89147', N'RI', N'AI', CAST(62.50219 AS Decimal(18,
5)), NULL, N'3e7a26cd-ece2-44b0-8416-f500e5bb0bfb')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A5012F0B4C AS DateTime), CAST(100.00000 AS Decimal(18, 5)), N'YYHE-SI218-40*255', N'RP255', N'ST', CAST(52.80909 AS
Decimal(18, 5)), NULL, N'90ad3dcd-6478-4b86-bdec-f7818ad7156f')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(135.00000 AS Decimal(18, 5)), N'LA4903020*294', N'RI', N'AI', CAST(379.04000 AS Decimal(18,
5)), NULL, N'0b62da54-5db6-465f-8229-f79a93c4dbf1')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'DN921/FL 2X28*303', N'RI', N'AI', CAST(69.74000 AS Decimal(18,
5)), NULL, N'270cb795-83c2-43b1-ae43-f8bef36b3470')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'FO173011/220V*407', N'RI', N'AI', CAST(118.00000 AS Decimal(18,
5)), NULL, N'cca4ac36-7538-45d0-90f8-f9a941515b74')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A5012F0B4C AS DateTime), CAST(12.00000 AS Decimal(18, 5)), N'YYHE-SI217-32-UNI*255', N'RP255', N'ST', CAST(51.15577
AS Decimal(18, 5)), NULL, N'9da49db4-8bfb-488d-a153-fd36ef21df47')
INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A600DE48DC AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'LLIL-84700*253', N'RI', N'ST', CAST(143.01118 AS Decimal(18,
5)), NULL, N'01831955-485b-4d6c-9665-fda401b8a5d1')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A500C6CA8F AS DateTime), CAST(25.00000 AS Decimal(18, 5)), N'KHBG-4345*318', N'RI', N'ST', CAST(0.00000 AS Decimal(18,
5)), CAST(1431.68531 AS Decimal(18, 5)), N'77d8fe2b-b7f7-4afb-baf2-00371e49ef50')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A600DE48DC AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'FO81177', N'RP318', N'ST', CAST(0.00000 AS Decimal(18,
5)), CAST(125.19275 AS Decimal(18, 5)), N'64bb5a74-4f36-40bb-8a15-0178d9ce3532')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A500E4483C AS DateTime), CAST(2202.00000 AS Decimal(18, 5)), N'TNSP-0594*020', N'RI', N'TO', CAST(0.00000 AS Decimal(18,
5)), CAST(4.88155 AS Decimal(18, 5)), N'a6d2c5e8-315e-427f-9745-01c5f6857f62')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A50120C690 AS DateTime), CAST(10.00000 AS Decimal(18, 5)), N'ES651900S*253', N'RI', N'AO', CAST(0.00000 AS Decimal(18,
5)), CAST(94.01000 AS Decimal(18, 5)), N'ae8a9ba7-74db-4f7d-b830-01f099ac24de')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A501139E98 AS DateTime), CAST(29.00000 AS Decimal(18, 5)), N'KHZM-42176950*255', N'RI', N'ST', CAST(0.00000 AS
Decimal(18, 5)), CAST(361.52127 AS Decimal(18, 5)), N'2611d667-8db3-4e71-a543-0245258a5788')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A501203C48 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'TN802/21', N'RP255', N'ST', CAST(0.00000 AS Decimal(18,
5)), CAST(222.08500 AS Decimal(18, 5)), N'01027e8a-e576-4605-a420-03524fab994e')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A500BE530C AS DateTime), CAST(13.00000 AS Decimal(18, 5)), N'ES651900S*253', N'RP253', N'SL', CAST(0.00000 AS Decimal(18,
5)), CAST(120.00000 AS Decimal(18, 5)), N'dce64410-15b5-4ecc-88b4-04e2b7eac306')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A5011CE020 AS DateTime), CAST(5.00000 AS Decimal(18, 5)), N'LA9801023*313', N'RI', N'TO', CAST(0.00000 AS Decimal(18,
5)), CAST(1288.96600 AS Decimal(18, 5)), N'3ce3c4b9-b0b4-45c6-b14e-058cb08608d2')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A500D8910A AS DateTime), CAST(4.00000 AS Decimal(18, 5)), N'LA4906203*294', N'RI', N'ST', CAST(0.00000 AS Decimal(18,
5)), CAST(62.34000 AS Decimal(18, 5)), N'25332af7-8f8c-489f-9e96-06427e4b64db')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A50103903E AS DateTime), CAST(24.00000 AS Decimal(18, 5)), N'YYLT-TRWW 155 HFP*294', N'RP313', N'SL', CAST(0.00000
AS Decimal(18, 5)), CAST(325.00000 AS Decimal(18, 5)), N'4550841e-8ec9-4528-99d9-fcbef5215030')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A5012F0B4C AS DateTime), CAST(2.00000 AS Decimal(18, 5)), N'YYHE-SIS180 220V*255', N'RI', N'ST', CAST(0.00000 AS
Decimal(18, 5)), CAST(49.64832 AS Decimal(18, 5)), N'10018f9c-a6ba-47c4-aff3-fd515f18c883')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A5012F0B4C AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'YYHE-SH150IZ*255', N'RI', N'ST', CAST(0.00000 AS Decimal(18,
5)), CAST(74.20504 AS Decimal(18, 5)), N'2a43869a-4300-486d-b534-fe336cc37aeb')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A500BE530C AS DateTime), CAST(4.00000 AS Decimal(18, 5)), N'YYVL-00358', N'RP253', N'SL', CAST(0.00000 AS Decimal(18,
5)), CAST(80.00000 AS Decimal(18, 5)), N'c22c794d-cefe-40a2-b813-fea8aa2cc5fb')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A500DBEC64 AS DateTime), CAST(50.00000 AS Decimal(18, 5)), N'TX203/50/BR', N'RI', N'ST', CAST(0.00000 AS Decimal(18,
5)), CAST(33.85686 AS Decimal(18, 5)), N'3367f8a0-75ff-476e-b3ca-ff095ed25ba6')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A500BE530C AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'TN0453/3/21', N'RP253', N'SL', CAST(0.00000 AS Decimal(18,
5)), CAST(250.00000 AS Decimal(18, 5)), N'6c87e1ad-98f8-485d-87e6-ff238eb2317e')
INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A501341DBC AS DateTime), CAST(244.00000 AS Decimal(18, 5)), N'YYIS-ICF2S26H1LDK*255', N'RI', N'ST', CAST(0.00000
AS Decimal(18, 5)), CAST(61.08273 AS Decimal(18, 5)), N'2d04ada8-ee92-4f28-9cbe-ffb855ebd93b')
ALTER TABLE [dbo].[tb_Transaction_In] ADD  CONSTRAINT [DF_tb_Transaction_In_Date]  DEFAULT (getdate()) FOR [Date]
GO
ALTER TABLE [dbo].[tb_Transaction_In] ADD  CONSTRAINT [DF_tb_Transaction_In_Quantity]  DEFAULT ((0)) FOR [Quantity]
GO
ALTER TABLE [dbo].[tb_Transaction_In] ADD  CONSTRAINT [DF_tb_Transaction_in_ID]  DEFAULT (newid()) FOR [ID]
GO
ALTER TABLE [dbo].[tb_Transaction_Out] ADD  CONSTRAINT [DF_tb_Transaction_Out_Date]  DEFAULT (getdate()) FOR [Date]
GO
ALTER TABLE [dbo].[tb_Transaction_Out] ADD  CONSTRAINT [DF_tb_Transaction_Out_Quantity]  DEFAULT ((0)) FOR [Quantity]
GO
ALTER TABLE [dbo].[tb_Transaction_Out] ADD  CONSTRAINT [DF_tb_Transaction_Out_Type]  DEFAULT ((0)) FOR [Type]
GO
ALTER TABLE [dbo].[tb_Transaction_Out] ADD  CONSTRAINT [DF_tb_Transaction_oUT_ID]  DEFAULT (newid()) FOR [ID]
GO

You can do it by means of computed column based on a UDF. 
so in your table just add a column like below
CREATE TABLE
cost as dbo.GetItemCode(ItemCode)
and function would be as below
CREATE FUNCTION dbo.GetItemCode
@ItemCode varchar(20)
RETURNS Numeric(10,2)
AS
BEGIN
DECLARE @WAP Numeric(10,2)
SELECT @WAP = SUM((COALESCE(i.Qty,0) - COALESCE(o.Qty,0)) * i.UTPrice)* 1.0/NULLIF(SUM(COALESCE(i.Qty,0) - COALESCE(o.Qty,0)),0)
FROM (SELECT ProductCode,StockCode,UTPrice,SUM(Quantity) AS Qty
FROM transaction_in
GROUP BY ProductCode,StockCode,UTPrice)i
FULL OUTER JOIN SELECT ProductCode,StockCode,UTPrice,SUM(Quantity) AS Qty
FROM transaction_out
GROUP BY ProductCode,StockCode,UTPrice) o
ON o.ProductCode = i.ProductCode
AND o.StockCode = i.StockCode
RETURN (@WAP)
END
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

Similar Messages

  • Inline transform for Sql Query not working in SAP MII 12.1 Version 12.1.8 B

    Hi All,
    I applied an xslt for an sql query which returns an xml file.
    I used inline transform icon in sql query to load an xsl file which has to return me a string
    Any idea why is not working for me..?
    My Sample XML file:
                                     <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="/XMII/CM/BatchDisposition/BatchQueueManagement/StyleSheets/ProductXsl.xsl"?>
    <Rowsets DateCreated="2011-05-05T07:27:45" EndDate="2011-05-05T07:27:45" StartDate="2011-05-05T06:27:45" Version="12.1.8 Build(20)">
         <Rowset>
              <Columns>
                   <Column Description="ProductName" MaxRange="1" MinRange="0" Name="ProductName" SQLDataType="12" SourceColumn="ProductName"/>
              </Columns>
              <Row>
                   <ProductName>Asprin 100mg Tablets 12 x10 strip</ProductName>
              </Row>
              <Row>
                   <ProductName>Asprin 300mg Tablets 12 x10 strip</ProductName>
              </Row>
              <Row><ProductName>Ibprooven 200mg Tablets 12 x 10 strip</ProductName></Row>
              <Row><ProductName>RipTide 50mg Tablets 40 x10 strip</ProductName></Row>
              <Row><ProductName>Seroquel 200mg Tablets 6 x10 strip</ProductName></Row>
              <Row><ProductName>Seroquel 400mg Tablets 12 x10 strip</ProductName></Row>
         </Rowset>
    </Rowsets>
    My Sample XSl File:
                                    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
         <xsl:text>["</xsl:text>
         <xsl:for-each select="Rowsets/Rowset/Row">
              <xsl:value-of select="ProductName"/>
              <xsl:if test="position() &lt; last()">
                   <xsl:text>","</xsl:text>
                    </xsl:if>
              <xsl:if test="position()=last()">
                           <xsl:text>"]</xsl:text>
                    </xsl:if>
         </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>
    Any Suggestions  are Welcome:
    Thanks

    Something like this should work...
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
         <xsl:template match="/">
              <Rowsets DateCreated="{Rowsets/@DateCreated}" Version="{Rowsets/@Version}" StartDate="{Rowsets/@StartDate}" EndDate="{Rowsets/@EndDate}">
                   <xsl:copy-of select="/Rowsets/FatalError"/>
                   <xsl:copy-of select="/Rowsets/Messages"/>
                   <Rowset>
                   <Columns>
                        <Column Description="Mycol1" MaxRange="1" MinRange="0" Name="Mycol1" SQLDataType="12" SourceColumn="Mycol1" />
                   </Columns>
                   <Row>
                   <Mycol1>
                   <xsl:text>["</xsl:text>
                        <xsl:for-each select="/Rowsets/Rowset/Row">
                             <xsl:value-of select="." />
                             <xsl:choose>
                                  <xsl:when test="position() &lt; last()"><xsl:text>","</xsl:text></xsl:when>
                                  <xsl:otherwise><xsl:text>"]</xsl:text></xsl:otherwise>
                             </xsl:choose>
                        </xsl:for-each>
                   </Mycol1>
                   </Row>
                   </Rowset>
              </Rowsets>
         </xsl:template>
    </xsl:stylesheet>

  • Get a insert session value for SQL query at report

    Hi friends
    I created a global temp table and procedure to support web search form.
    and a search result report. The procudure
    gets search result from multip tables and
    insert into temp table --recordsearch. I can get value from temp table  by call procedure
    at SQL*Plus.
    However, I can not get this value by web report.
    How can I get this insert session value and pass to SQL query for report?
    Thanks,
    Newweb
    CREATE GLOBAL TEMPORARY TABLE recordsearch
    (emp_id          VARCHAR2(200),
    ssn               VARCHAR2(9),
    fname          VARCHAR2(200),
    lname           VARCHAR2(200),
    m_name          VARCHAR2(200)
    ) ON COMMIT PRESERVE ROWS;

    it possible that your web form does not have a persistent, dedicated connection. if you have connection pooling for example, multiple sessions will see the same instance of the GTT, so if one deletes it, then nobody sees it (or you can see others data). if the connections are not persistent, then they can disconnect between calls, deleting the GTT table.

  • Xml parsing error while selecting whole result set for sql query

    Hi All,
    I am having xml parsing error while selecting whole query result set. The data is coming fine for default result set of 50 rows.
    My exception is below.
    ORA-31011: XML parsing failed
    ORA-19202: Error occurred in XML processing
    LPX-00234: namespace prefix "xsi" is not declared
    Error at line 1
    ORA-06512: at "SYS.XMLTYPE", line 254
    ORA-06512: at line 1
    *31011. 00000 - "XML parsing failed"*
    **Cause: XML parser returned an error while trying to parse the document.*
    **Action: Check if the document to be parsed is valid.*
    My sql query is below that is giving results for default result set of 50 rows.
    select extract(xmlType(clob_xml_colm_name), '//v2:node1//childnode/text()','xmlns:v2="namespace_url"').getStringVal()  from table_name
    My sql developer version is below.
    Java(TM) Platform     1.7.0_04
    Oracle IDE     3.1.07.42
    Versioning Support     3.1.07.42
    My database version is below.
    Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bit
    Please could any one help me urgently as the sql query is supposed to be correct as it is returning query results, but the problem happening when i try to select whole result set.
    Thanks and regards,

    What does the XML look like? It appears that some of the supposed XML stored as a CLOB is not really valid XML. Find the row in the table that is causing your issue and review the "XML" in it.

  • Create a validation for SQL Query (updateable report)

    Hello
    I have a need for creating a validation on a SQL Query (updateable report)- type region
    1)
    When a row get populated i don't want same values to be populated in the second row.(as shown in first 2 rows)
    Also, I don't want to have a value that falls in range in between 100 and 1000 for the period falls in the previous. (as shown in third row)
    i.e
    Type_id
    Lower_Limit        Upper_Limit           Date_From       Date _To
    1
    100                      1000                   1/1/2013        12/31/2013
    2
    100                      1000                    1/1/2013        12/31/2013
    3
    101                       500                    2/1/2013        10/31/2013
    appreciate any help
    thanks
    kp

    This example could help:
    http://apex.oracle.com/pls/otn/f?p=31517:214
    Denes Kubicek
    http://deneskubicek.blogspot.com/
    http://www.opal-consulting.de/training
    http://apex.oracle.com/pls/otn/f?p=31517:1
    -------------------------------------------------------------------

  • I am not able to see the trace files for SQL query

    Hello, I am using windows vista OS.
    SQL> select * from v$version;
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
    PL/SQL Release 10.2.0.3.0 - Production
    CORE 10.2.0.3.0 Production
    TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
    NLSRTL Version 10.2.0.3.0 - Production
    SQL>
    I enabled the trace as below
    alter system set timed_statistics=TRUE
    alter system set sql_trace=TRUE
    After this, I ran sql query from scott as below.
    select count(*) from emp;
    After this, i checked in user dump directory. The trace files are generating... But this sql code is not appearing in the trace files...
    Am i missing anything... Any help is appreciated.
    Thanks

    This may happen when the trace file is not completely closed.You may try again doing like this,
    alter session set sql_trace=true;
    select * from emp;
    alter session set sql_trace=false;After this,disconnect and exit from your session as well. This should close the file completely and you should be able to see your command in it.
    HTH
    Aman....

  • A challenging dynamic SQL query problem

    hi All,
    I have a very interesting problem at work:
    We have this particular table defined as follows :
    CREATE TABLE sales_data (
    sales_id NUMBER,
    sales_m01 NUMBER,
    sales_m02 NUMBER,
    sales_m03 NUMBER,
    sales_m04 NUMBER,
    sales_m05 NUMBER,
    sales_m06 NUMBER,
    sales_m07 NUMBER,
    sales_m08 NUMBER,
    sales_m09 NUMBER,
    sales_m10 NUMBER,
    sales_m11 NUMBER,
    sales_m12 NUMBER,
    sales_prior_yr NUMBER );
    The columns 'sales_m01 ..... sales_m12' represents aggregated monthly sales, in which 'sales_m01' translates to 'sales for the month of january, january being the first month, 'sales_m02' sales for the month of february, and so on.
    The problem I face is that we have a project which requires that a parameter be passed to a stored procedure which stands for the month number which is then used to build a SQL query with the following required field aggregations, which depends on the parameter passed :
    Sample 1 : parameter input: 4
    Dynamically-built SQL query should be :
    SELECT
    SUM(sales_m04) as CURRENT_SALES,
    SUM(sales_m01+sales_m02+sales_m03+sales_m04) SALES_YTD
    FROM
    sales_data
    WHERE
    sales_id = '0599768';
    Sample 2 : parameter input: 8
    Dynamically-built SQL query should be :
    SELECT
    SUM(sales_m08) as CURRENT_SALES,
    SUM(sales_m01+sales_m02+sales_m03+sales_m04+
    sales_m05+sales_m06+sales_m07+sales_m08) SALES_YTD
    FROM
    sales_data
    WHERE
    sales_id = '0599768';
    So in a sense, the contents of SUM(sales_m01 ....n) would vary depending on the parameter passed, which should be a number between 1 .. 12 which corresponds to a month, which in turn corresponds to an actual field range on the table itself. The resulting dynamic query should only aggregate those columns/fields in the table which falls within the range given by the input parameter and disregards all the remaining columns/fields.
    Any solution is greatly appreciated.
    Thanks.

    Hi another simpler approach is using decode
    try like this
    SQL> CREATE TABLE sales_data (
      2  sales_id NUMBER,
      3  sales_m01 NUMBER,
      4  sales_m02 NUMBER,
      5  sales_m03 NUMBER,
      6  sales_m04 NUMBER,
      7  sales_m05 NUMBER,
      8  sales_m06 NUMBER,
      9  sales_m07 NUMBER,
    10  sales_m08 NUMBER,
    11  sales_m09 NUMBER,
    12  sales_m10 NUMBER,
    13  sales_m11 NUMBER,
    14  sales_m12 NUMBER,
    15  sales_prior_yr NUMBER );
    Table created.
    SQL> select * from sales_data;
      SALES_ID  SALES_M01  SALES_M02  SALES_M03  SALES_M04  SALES_M05  SALES_M06  SALES_M07  SALES_M08  SALES_M09  SALES_M10  SALES_M11  SALES_M12 SALES_PRIOR_YR
             1        124        123        145        146        124        126        178        189        456        235        234        789          19878
             2        124        123        145        146        124        126        178        189        456        235        234        789          19878
             1        100        200        300        400        500        150        250        350        450        550        600        700          10000
             1        101        201        301        401        501        151        251        351        451        551        601        701          10000----now for your requirement. see below query if there is some problem then tell.
    SQL> SELECT sum(sales_m&input_data), DECODE (&input_data,
      2                 1, SUM (sales_m01),
      3                 2, SUM (sales_m01 + sales_m02),
      4                 3, SUM (sales_m01 + sales_m02 + sales_m03),
      5                 4, SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04),
      6                 5, SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04 + sales_m05),
      7                 6, SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04+sales_m05+sales_m06),
      8                 7, SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04+sales_m05+sales_m06+sales_m07),
      9                 8, SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04+sales_m05+sales_m06+sales_m07+sales_m08),
    10                 9, SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04+sales_m05+sales_m06+sales_m07+sales_m08+sales_m09),
    11                 10,SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04+sales_m05+sales_m06+sales_m07+sales_m08+sales_m09+sales_m10),
    12                 11,SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04+sales_m05+sales_m06+sales_m07+sales_m08+sales_m09+sales_m10+sales_m11),
    13                 12,SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04+sales_m05+sales_m06+sales_m07+sales_m08+sales_m09+sales_m10+sales_m11+sales_m12)
    14                ) total
    15    FROM sales_data
    16   WHERE sales_id = 1;
    Enter value for input_data: 08
    Enter value for input_data: 08
    old   1: SELECT sum(sales_m&input_data), DECODE (&input_data,
    new   1: SELECT sum(sales_m08), DECODE (08,
    SUM(SALES_M08)      TOTAL
               890       5663

  • Need Heip For SQL Query

    Hi Friends,
    I am too muchconfused about an sql query. I've created a html page where I select one radio button . The value of this radio button is passed to the query.Now the out put of the query is used in developing the output.
    For illustration I show you the source code.
    Radio Button Selection page.
    <form action="service.html" method="post"> <h3><strong>Services Available</strong></h3>   <p>     <label>       <input type="radio" name="service" value="si"  />       Institutions</label>     <br />     <label>       <input type="radio" name="service" value="sc" />       Corporate_Offices</label>     <br />     <label>       <input type="radio" name="service" value="sr" />       Restaurent_Cafe</label>     <br />     </p>   <label>     <input type="submit" value="Submit" />   </label> </form>
    Now the query page-
    <sql:query var="empList" scope="session"> select <c:out value="${param.service}" /> from map_service where cityname = ? <sql:param value="${param.cityname}" />   </sql:query>
    Now I need to generate output with the given below value in the next page-
    <c:out value="${param.service}" />
    This value show value as si, sc sr only where I need the database table value as per selection.
    I am really confused that what I should write as <c:out value="${...
    My database table has attributes as cityname, si, sc, sr. etc.
    when some one select si from radio button the value of radio button is passed to sql query and the value which is selected on the table after query should be used in developing output result please help.

    Dear Friend Is it not possible for any of you to give me a clue that if I've to get the output of *?* from the query what I can write in syntax.
    <sql:query var="empList" scope="session">
    select ? from map_service
    where cityname = ?
    <sql:param value="${param.service}" />
    <sql:param value="${param.cityname}" />
    </sql:query>
    <c:out value="${empList.param.service}" />Output of the value of ? from the database select ? from map_service Any idea

  • No Data Found for SQL query

    Hello,
    I have a simple SQL query in a report.
    select
          null as "Complete",
             a.col1,
             a.col2,
             a.col3,
             a.col4,
          b.col2
    from      TableA a, TableB b
    where   a.id = b.idThere is a known issue in apex 4.0 that is causing the "When No Data Found" message to work incorrectly so that this is returned:
    report error:
    ORA-01403: no data found
    Is there a way to return a message in a SQL statement? If not, any suggestions on how I can convert this in the existing report to PL/SQL would be appreciated.
    Thanks!
    Matt

    I would like to do something similar to this (below) but the only option that is given other than "SQL Query" is “PL/SQL function body returning a SQL query”
    I'm not sure how to do this as a funciton.
    declare
    vComplete Varchar2;
    vCol1        Varchar2;
    vCol2        Varchar2;
    vCol3        Varchar2;
    vCol4        Varchar2;
    vColB2      Varchar2;
    Begin
    select
             null as "Complete",
             a.col1,
             a.col2,
             a.col3,
             a.col4,
             b.col2
    into vComplete,vCol1,vCol2,vCol3,vCol4,vColB2
    from   TableA a, TableB b
    where   a.id = b.id
    Dbms_output.put_line(vComplete,vCol1,vCol2,vCol3,vCol4,vColB2);
    Exception
          When no_data_found
                Dbms_output.put_line(vComplete,vCol1,vCol2,vCol3,vCol4,vColB2);
          When others
                Dbms_output.put_line(‘exception handling message’);
    End;

  • TR for SQL query

    Hi,
    I have a query which I want to attach it to a tcode and then move to quality as well as production.I am getting the program name for the query from the additional functions options under the Quick View in the menu bar of SQVI.The same I am using to create a tcode.How to create a transport request for the query as well as this program.Can I assign the same program which is AQA0SYSTQV000015ZMI===== to a package?
    Thanks,
    K.Kiran.

    hi Kiran,
    tcode for query, pls. have a look:
    Create a transaction calling transaction START_REPORT with the following parameters/attributes filled :
    D_SREPOVARI-REPORTTYPE= AQ "parameter indicating Abap Query
    D_SREPOVARI-REPORT= 'ZGRP' "Query User group
    D_SREPOVARI-EXTDREPORT= 'ZNAME' "Query name
    to transport queries, pls. use the following program: RSAQR3TR
    you have to follow the following steps:
    1. With the above program, choose Export and add the objects you want to transport. after execution you get a transport request from the system.
    2. Release this transport request
    3. Import this transport request into the target system (like any other transport request)
    4. In the target system run the above program, but choose Import now and add the transport request name
    hope this helps
    ec

  • Help needed for SQL query

    hello ,
    I am a beginner in terms of writing sql queries. I hope some body can help me out.
    I have two tables
    mysql> desc user_group_t;
    ---------------------------------------------------+
    | Field | Type | Null | Key | Default | Extra |
    ---------------------------------------------------+
    | userAccountId | char(8) | | PRI | | |
    | groupId | char(8) | | PRI | | |
    ---------------------------------------------------+
    2 rows in set (0.00 sec)
    mysql> desc group_t;
    ---------------------------------------------------+
    | Field | Type | Null | Key | Default | Extra |
    ---------------------------------------------------+
    | id | char(8) | | PRI | | |
    | name | char(50) | YES | | NULL | |
    | email | char(100) | YES | | NULL | |
    | description | char(254) | YES | | NULL | |
    | parentId | char(8) | YES | | NULL | |
    | creatorId | char(8) | YES | | NULL | |
    | createDate | char(20) | YES | | NULL | |
    | updateDate | char(20) | YES | | NULL | |
    | updatorId | char(8) | YES | | NULL | |
    ---------------------------------------------------+
    9 rows in set (0.00 sec)
    what I want is list of all groups with id,name and #of members(which is the # of rows in the user_group_t for any given id). Importantly I need the groups with 0 members also to be listed. In short my output should contain exactly the same number of rows as in group_t table with an additional column indicating # of members for that group.
    Any help would be greatly appreciated.
    Thanks in Advance.
    -Vasanth

    Thanks Donald,
    Actually I figured it out, with the following query:
    select id,name,sum(if(groupid is not null,1,0)) as members from group_t left join user_group_t on id=groupid group by id;
    I tried your solution, but mysql says there is an error at '+' . Anyway I modified your solution to the one below and it worked.
    select a.id, a.name, count(b.groupid) from group_t a left join user_group_t b on a.id=b.groupid group by a.id, a.name;
    I tried that before but then I used Count(*) instead of count on groupid. Your solution is elagant and I will go with yours.
    Thanks again.
    Vasanth

  • Tweak for sql query - help needed for smalll change

    Hi.
    I am trying to run a script that checks for used space on all tablespaces and returns the results.
    So far so good:
    set lines 200 pages 2000
    col tablespace_name heading 'Tablespace' format a30 truncate
    col total_maxspace_mb heading 'MB|Max Size' format 9G999G999
    col total_allocspace_mb heading 'MB|Allocated' format 9G999G999
    col used_space_mb heading 'MB|Used' format 9G999G999D99
    col free_space_mb heading 'MB|Free Till Max' like used_space_mb
    col free_space_ext_mb heading 'MB|Free Till Ext' like used_space_mb
    col pct_used heading '%|Used' format 999D99
    col pct_free heading '%|Free' like pct_used
    break on report
    compute sum label 'Total Size:' of total_maxspace_mb total_allocspace_mb used_space_mb - free_space_mb (used_space_mb/total_maxspace_mb)*100 on report
    select
    alloc.tablespace_name,
    (alloc.total_allocspace_mb - free.free_space_mb) used_space_mb,
    free.free_space_mb free_space_ext_mb,
    ((alloc.total_allocspace_mb - free.free_space_mb)/alloc.total_maxspace_mb)*100 pct_used,
    ((free.free_space_mb+(alloc.total_maxspace_mb-alloc.total_allocspace_mb))/alloc.total_maxspace_mb)*100 pct_free
    FROM (SELECT tablespace_name,
    ROUND(SUM(CASE WHEN maxbytes = 0 THEN bytes ELSE maxbytes END)/1048576) total_maxspace_mb,
    ROUND(SUM(bytes)/1048576) total_allocspace_mb
    FROM dba_data_files
    WHERE file_id NOT IN (SELECT FILE# FROM v$recover_file)
    GROUP BY tablespace_name) alloc,
    (SELECT tablespace_name,
    SUM(bytes)/1048576 free_space_mb
    FROM dba_free_space
    WHERE file_id NOT IN (SELECT FILE# FROM v$recover_file)
    GROUP BY tablespace_name) free
    WHERE alloc.tablespace_name = free.tablespace_name (+)
    ORDER BY pct_used DESC
    The above returns something like this:
    MB MB % %
    Tablespace Used Free Till Ext Used Free
    APPS_TS_ARCHIVE 1,993.13 54.88 97.32 2.68
    APPS_TS_TX_IDX 14,756.13 1,086.88 91.37 8.63
    APPS_TS_TX_DATA 20,525.75 594.25 80.18 19.82
    APPS_TS_MEDIA 6,092.00 180.00 74.37 25.63
    APPS_TS_INTERFACE 13,177.63 366.38 71.49 28.51
    The above works fine, but I would like to further change the query so that only those tablespaces with free space less than 5% (or used space more than 95%) are returned.
    I have been working on this all morning and wanted to open it up to the masters!
    I have tried using WHERE pct_used > 95 but to no avail.
    Any advice would be appreciated.
    Many thanks.
    10.2.0.4
    Linux Red Hat 4.

    Thanks for that.
    What is confusing is that the below query works for every other (about 10 others) database but not this one (?)
    SQL> set lines 200 pages 2000
    SQL>
    SQL> col tablespace_name heading 'Tablespace' format a30 truncate
    SQL> col total_maxspace_mb heading 'MB|Max Size' format 9G999G999
    SQL> col total_allocspace_mb heading 'MB|Allocated' format 9G999G999
    SQL> col used_space_mb heading 'MB|Used' format 9G999G999D99
    SQL> col free_space_mb heading 'MB|Free Till Max' like used_space_mb
    SQL> col free_space_ext_mb heading 'MB|Free Till Ext' like used_space_mb
    SQL> col pct_used heading '%|Used' format 999D99
    SQL> col pct_free heading '%|Free' like pct_used
    SQL>
    SQL> break on report
    SQL> compute sum label 'Total Size:' of total_maxspace_mb total_allocspace_mb used_space_mb - free_space_mb (used_space_mb/total_maxspace_mb)*100 on report
    SQL>
    SQL> select /*+ALL_ROWS */
    2 alloc.tablespace_name,
    3 alloc.total_maxspace_mb,
    4 alloc.total_allocspace_mb,
    5 (alloc.total_allocspace_mb - free.free_space_mb) used_space_mb,
    6 free.free_space_mb+(alloc.total_maxspace_mb-alloc.total_allocspace_mb) free_space_mb,
    7 free.free_space_mb free_space_ext_mb,
    8 ((alloc.total_allocspace_mb - free.free_space_mb)/alloc.total_maxspace_mb)*100 pct_used,
    9 ((free.free_space_mb+(alloc.total_maxspace_mb-alloc.total_allocspace_mb))/alloc.total_maxspace_mb)*100 pct_free
    10 FROM (SELECT tablespace_name,
    11 ROUND(SUM(CASE WHEN maxbytes = 0 THEN bytes ELSE maxbytes END)/1048576) total_maxspace_mb,
    12 ROUND(SUM(bytes)/1048576) total_allocspace_mb
    13 FROM dba_data_files
    14 WHERE file_id NOT IN (SELECT FILE# FROM v$recover_file)
    15 GROUP BY tablespace_name) alloc,
    16 (SELECT tablespace_name,
    17 SUM(bytes)/1048576 free_space_mb
    18 FROM dba_free_space
    19 WHERE file_id NOT IN (SELECT FILE# FROM v$recover_file)
    20 GROUP BY tablespace_name) free
    21 WHERE alloc.tablespace_name = free.tablespace_name (+)
    22 ORDER BY pct_used DESC
    23 /
    ((alloc.total_allocspace_mb - free.free_space_mb)/alloc.total_maxspace_mb)*100 pct_used,
    ERROR at line 8:
    ORA-01476: divisor is equal to zero

  • Condition variable for sql query

    I have created the follwing query which adds the open sales orders and forecasts to create a projected cashflow.  The query works fine except I would like to be able to enter the forecast code, rather than have it hard coded.  If I enter Where OFCT.code='[%0]', I get no results for the forecast. 
    SELECT     FCT1.ItemCode "Item", FCT1.Date "Date", FCT1.Quantity "Qty", ITM1.Price "Price", (FCT1.Quantity*ITM1.Price)"Ext Price"
    FROM         OFCT INNER JOIN
                          FCT1 ON OFCT.AbsID = FCT1.AbsID INNER JOIN
                          ITM1 ON FCT1.ItemCode = ITM1.ItemCode
    WHERE     (ITM1.PriceList = '2') AND (OFCT.Code ='5/24/2010')
    UNION
    SELECT          RDR1.ItemCode "Item", RDR1.ShipDate "Date", RDR1.Quantity "Qty", RDR1.Price "Price", (RDR1.Quantity*RDR1.Price)"Ext Price"
    FROM          RDR1
    WHERE          RDR1.LineStatus = 'O' and  RDR1.OpenQty >'0'
    ORDER BY DATE
    Edited by: Sandra Clavell on May 27, 2010 3:50 AM
    Edited by: Sandra Clavell on May 27, 2010 3:51 AM

    Try this:
    Declare @fcod char(16)
    set @fcod/* select f.code from ofct f where f.code*/='[%0]'
    SELECT FCT1.ItemCode "Item", FCT1.Date "Date", FCT1.Quantity "Qty",
    ITM1.Price "Price", (FCT1.Quantity*ITM1.Price)"Ext Price"
    FROM OFCT INNER JOIN
    FCT1 ON OFCT.AbsID = FCT1.AbsID INNER JOIN
    ITM1 ON FCT1.ItemCode = ITM1.ItemCode
    WHERE (ITM1.PriceList = '2') --AND (OFCT.Code =@fcod)
    UNION
    SELECT RDR1.ItemCode "Item", RDR1.ShipDate "Date", RDR1.Quantity "Qty",
    RDR1.Price "Price", (RDR1.Quantity*RDR1.Price)"Ext Price"
    FROM RDR1
    WHERE RDR1.LineStatus = 'O' and RDR1.OpenQty >'0'
    ORDER BY DATE

  • A challenge for the Ipod Experts

    My 5th Gen 30gb ipod (18 months old )has worked flawlessly until I
    upgraded to latest software (1.3) last weekend. Since then, my ipod is
    completley knackered and the problems have definetley started since the recent download.Firstly All of the music has been completley erased and when
    connected to computer it refuses to sync anything from the library
    advising ''error 48''. I spoke to someone at Apple help desk last friday
    who emailed me something to fix error 48 but it has not made a bit of
    difference - error 48 still keeps coming up. In addition to this my ipod
    will not let me re-store to factory settings, when I try this a
    different error comes up - saying files in use by another application.Ipod
    will therefore not re-store or re-set. I have tried putting it into disk
    mode, but again it won't let me. I have completley drained the battery
    and re-charged it - still same problem. I have changed the drive letter
    on PC- still makes no difference, I have uninstalled and re-install
    ed itunes several times - still no difference, constantly comes up
    with error 48. As well as this, the ipod is showing up as 25gb of used
    disk space on the orange bar, but there is nothing at all on the ipod so
    how can it be full? Sometimes it shows up on my computer - sometimes it
    doesnt.
    My ipod is out of Warranty now and therefore its apparently down to me
    to have it fixed??!! I am sooooooooo frustrated because I have taken very good
    care of an item that cost me alot of money and which now appears to be
    completley useless.
    There appears to be a lot of posts recently relating to similar issues following download of software, but nothing specific that seems to fix it. I've tried all of the advice given to others and just nothing works, so could one of the experts please try and help me?

    If you get that network message when you try to uninstall iTunes with add/remove programs, you usually need to do an installer clean up with the Microsoft Installer Cleanup Utility.
    Here is a method for full removal of iTunes and related programs with clean up and reinstall:
    Download a fresh copy of iTunes and the stand alone version of Quicktime (the one without iTunes)
    http://www.apple.com/quicktime/download/win.html
    http://www.apple.com/itunes/download/
    Download and install Microsoft Installer cleanup utility, there are instructions on the page as well as the download. Note that what you download is the installer not the program – you have to run it to install the program.
    To run the program – All Programs>>Windows Install Cleanup
    http://support.microsoft.com/kb/290301/
    Now use the following method to remove iTunes and its components:
    XP
    http://support.apple.com/kb/HT1925
    Vista
    http://support.apple.com/kb/HT1923
    *If you hit a problem with one of the uninstalls don't worry*, carry on with the deleting of files and folders as directed in the method.
    When you get to deleting Quicktime files in the system32 folder as advised in the method, you can delete any file or folder with Quicktime in the name.
    Restart your PC.
    Run the Microsoft Installer Cleanup Utility. (Start > All Programs > Windows Install Clean Up)
    Remove any references you find to the programs you removed - strictly speaking you only need to worry about those programs where the uninstall failed.
    If you don’t see an entry for one of the programs that did not uninstall, look out for blank entries or numeric entries that look like version numbers e.g. 7.x for Quicktime or 1.x for Bonjour.
    restart your PC
    Install the stand alone Quicktime and check that it works.
    If it does, install iTunes.

  • Need help for SQL Query

    Hi Friends,
    I am using below query to genearet one report.
    {code}
    distinct a.plan_id,
    (SELECT external_plan_ref
    FROM bk9_t_plan
    WHERE plan_id = a.plan_id)
    "EXT_PAN_REF",
    (SELECT external_plan_ref
    FROM bk9_t_plan
    WHERE plan_id = b.depends_on_ba_plan_id)
    "EXT_PAN_REF1",
    (SELECT external_plan_ref
    FROM bk9_t_plan
    WHERE plan_id = c.drp_plan_id)
    "EXT_PAN_REF2",
    (SELECT external_plan_ref
    FROM bk9_t_plan
    WHERE plan_id = d.srp_plan_id)
    "EXT_PAN_REF3",
    (SELECT external_plan_ref
    FROM bk9_t_plan
    WHERE plan_id = e.DRP_TO_DRP_PLAN_ID)
    "EXT_PAN_REF4",
    (SELECT external_plan_ref
    FROM bk9_t_plan
    WHERE plan_id = f.srp_plan_id)
    "EXT_PAN_REF5"
    FROM BK9_T_BUSINESS_ACTIVITY a,
    BK9_T_BA_DEPENDENCY b,
    BK9_T_BA_DRP_DEP c,
    BK9_T_BA_SRP_DEP d,
    BK9_T_DRP_DEPENDENCY e,
    BK9_T_SYSTEM_DEPENDENCY f
    WHERE     a.plan_id = b.ba_to_ba_plan_id
    AND a.plan_id = c.ba_plan_id
    AND a.plan_id = d.ba_plan_id
    AND c.drp_plan_id = e.DRP_TO_DRP_PLAN_ID
    AND e.DRP_TO_DRP_PLAN_ID = f.drp_plan_id
    {code}
    My output is like this :
    {code}
    22767 19093 19095 19049  19049 19059
    22767 19093 19095 19049  19049 19060
    22767 19093 19095 19062  19062 19060
    22767 19093 19095 19062  19062 19061
    22768 19094 19093 19062  19062 19060
    22768 19094 19093 19062  19062 19061
    {code}
    But What i am looking is like this.
    {code}
    22767 19093 19095 19049  19049 19059
    22767                                               19060
    22767 19093 19095 19062  19062 19060
    22767                                              19061
    22768 19094 19093 19062 19062 19060
    22768                                              19061
    {code}
    Please help.

    Hi,
    Could you please illuminate our minds and spend a bit of time in explaining the logic? That will be much easier without reading the crystal ball which nobody has it here.
    Please read: Re: 2. How do I ask a question on the forums?
    And please provide sample data (create table and insert statement), description of the logic, database version and everything else which is specified in the FAQ
    Regards.
    Al

Maybe you are looking for