SQL to derive package and Item details - hive

We have a requirement, where we get package details having list of items in one table. and in other table we have these items.
Sample Data
Package Details.
PackName|ItemName|ItemQuantity
1|AA|10
1|AB|20
1|AC|30
2|BC|40
2|BD|50
2|BE|60
2|BF|70
Item Received
ItemName|ItemQuantity
AA|10
AB|20
AC|30
AA|10
AB|20
AC|30
BC|40
BD|50
BE|60
BF|70
AA|10
AB|20
BE|60
BF|70
Desired Output
Output-1
ItemName|ItemQuantity|Is Package
AA|10|1
AB|20|1
AC|30|1
AA|10|1
AB|20|1
AC|30|1
BC|40|1
BD|50|1
BE|60|1
BF|70|1
AA|10|0
AB|20|0
BE|60|0
BF|70|0
Output-2
PackName|TotalPacks
1|2
2|1
Output1 --> if we receive all the items in a pack then we need to derive IsPackage as 1 or else 0
Output2 --> We need to update total Packs which were shipped, in our case since we have received 2 packs of A and 1 B.

Related

How to get the sum of a column based on another column (SQL Server 2014)

SELECT
HCT_USER_CONTRACT_NUMBER AS ContractNumber,
SUM(CASE
WHEN HIT_CURRENT_STOCK_NUMBER = '_DELIVERY'
THEN HIT_CHARGE_AMOUNT
ELSE 0
END) AS [DeliveryCharge]
FROM
TH_HIRE_CONTRACTS C
LEFT JOIN
TH_HIRE_ITEMS I on C.HCT_CONTRACT_NUMBER = I.HIT_CONTRACT_NUMBER
GROUP BY
HCT_USER_CONTRACT_NUMBER
I am including the above in the select statement but I get an error
Error converting data type varchar to real
In the hire_items table there is multiple lines for each corresponding record in the hire_contracts table, and I am trying to return a single line for each hire contract with the sum of the HIT_CHARGE_AMOUNT where the HIT_CURRENT_STOCK_NUMBER= = '_DELIVERY'. Unfortunately the DB design has a line for each hire item on the contract and in the same table a line for delivery and a line for collection for each hire contract and I just want to get the delivery charges per contract number.

Dynamic SQL query to loop through records and find correct category

I'm trying to write a 2 step process to determine material types for different components.
Step 1: this includes results from a different query containing RecordID and Component Name :
Sample records :
RecordID
Component
Material
BR39590
00000000000000564792
000000000002073757
BR39590
00000000000000567649
000000000002073757
BR39591
00000000000000567650
000000000002073758
Above RecordId's will contain several component numbers and corresponding Materials
Loop through the Material derived from above query result and join to a different table called 'Material' to determine Material type, if Material type belongs to 'A' or 'B' the process should exit out and insert the records into a new table. If Material type does not belong to 'A' or B then Query should go back to step 1 and fetch the next component to look for 'A' or 'B' or blank resultset.
Material
Category
000000000002073757
A
000000000002073758
B
Above 2 steps are repeated for all RecordID's
Final Result:
RecordId
Material
Component
Category
BR39590
000000000002073757
00000000000000564792
A
BR39590
000000000002073757
00000000000000567649
A
BR39591
000000000002073758
00000000000000567650
B
I did not understand what you mean, With dynamic SQL or coding in SQL environment, your wishes can be met, but there is a much, much easier way.
with a left join you can find the end result.
select
c.RecordID,
c.Material,
c.Component,
m.Category
from components c left join material m on c.Material = m.Material

Retrieve data from HugeClob in oracle

I have one Table, in this table stored data as xml on HugeClob. In this xml I have several data. I want retrieve this data with SQL query. I add 3 xml example.
// First
<xml-fragment>
<sdr:OCAC>
<op:operation>Sale</op:operation>
<op:ID>100</op:ID>
<op:payload>
<op:Tea>
</op:Tea>
</op:payload>
</sdr:OCAC>
</xml-fragment>
// Second
<xml-fragment>
<sdr:OCAC>
<op:operation>Sale</op:operation>
<op:ID>101</op:ID>
<op:payload>
<op:Cofe>
</op:Cofe>
</op:payload>
</sdr:OCAC>
</xml-fragment>
// Third
<xml-fragment>
<sdr:OCAC>
<op:operation>Sale</op:operation>
<op:ID>102</op:ID>
<op:payload>
<op:Juice>
</op:Juice>
</op:payload>
</sdr:OCAC>
</xml-fragment>
I want shown query result as attached image. I'm so sorry for my english.
Tea's price is 1.50
Cofe's price is 2.50
Juice's price is 3.00

VS SR Report joining data from multiple Data sets?

Wondering if anyone can help me, I am currently learning, SRSS Reports and have been tasked with a Pay & Display vehicle registration report, for staff and student carparks, to show if they are registered for P&D. I have the report laid out as a 3 table column; Name, Vehicle Number and Vehicle Registration. I have from the below code set out in my datasets the Vehicle Number and Vehicle Registrations pulling through, with a parameter to filter out if is staff or student pay and display. However I am having difficulty with pulling the Staff and Student names into one name column as they are through two different datasets. Can anyone be of help at all please? TIA
**-- Pay & Display Carpark Details Dataset**
SELECT * FROM Carpark_Vehicles
WHERE VehicleCustNo IN (SELECT CustID FROM [dbo].[Carpark_Customer]
WHERE CarParkID IN (SELECT CarParkID FROM [dbo].[Carpark_CarPark]
WHERE CTypeID IN ('PD', 'SPD')))
--and CTypeID IN (#Carpark_type))
**--Vehicle Details Dataset**
SELECT
Carpark_Vehicles.VehicleID
,Carpark_Vehicles.RegNo
,VehicleCustNo
FROM
Carpark_Vehicles
WHERE CarPark_Vehicles.RegNo = #RegNo
**--Staff details dataset**
SELECT
StaffDetails_StaffandLeaverDetails.StaffNumber
,StaffDetails_StaffandLeaverDetails.LastName
,StaffDetails_StaffandLeaverDetails.Name1
FROM
StaffDetails_StaffandLeaverDetails
**--Student details dataset**
SELECT
Student_CurrentStudentDetails.StudentNumber
,Student_CurrentStudentDetails.StudentID
,Student_CurrentStudentDetails.LastName
,Student_CurrentStudentDetails.FirstName
FROM
Student_CurrentStudentDetails
**--Carpark permit type (Pay & Display) parameter**
SELECT DISTINCT
CTypeID, CASE CTypeID WHEN 'abc' THEN 'Students' WHEN 'abc' THEN 'Staff' END AS Label
FROM
Carpark_Customer
WHERE CTypeID IN ('abc', 'Abc')
For your req I just took some sample data. This entire example will give you Idea how to achieve your final result.
Note: this is just showing you way how to achieve it, you have to modify as per your req.
Student and Staff Info from 2 different Tables
Registration Info from another separate Table
Your Final Result when Student as parameter is selected
Your Final Result when Staff as parameter is selected
Now how did I achieved this?
My Design for Final Result
Row visibilty expression for your final result Table in SSRS
Expressiion as below
=IIF(Parameters!ReportParameter1.Value=1,
IIF(Isnothing(Lookup(Fields!PersonId.Value,Fields!PersonId.Value,Fields!Name.Value, "DSStudentInfo")),true,false),
IIF(Isnothing(Lookup(Fields!PersonId.Value,Fields!PersonId.Value,Fields!Name.Value, "DSStaffInfo")),true,
false)
)
first Name column visibility
second column name visibility
First Name data value expression
=Lookup(Fields!PersonId.Value,Fields!PersonId.Value,Fields!Name.Value, "DSStudentInfo")
second name data value expression
=Lookup(Fields!PersonId.Value,Fields!PersonId.Value,Fields!Name.Value, "DSStaffInfo")

Firebird SQL. How to allow for BOM components in SOH when calculating production requirements

I am trying to write a query to find the purchasing/manufacturing requirements, based on our sales orders and taking into account our stock on hand and work in progress.
I am using a Firebird 2.5 server
My approach has been to begin with the sales orders, then
use a recursive query to identify all the items that are required.
Then to subtract those requirements from the work in progress and stock on hand to give available and free quantities.
The first part works well, but the second part fails to take into account the child components present in the stock on hand.
For example Item A includes 3 x Item B
I have sales for 5 x Item A and 2 x Item B
The query will correctly tell me that I need 5 X Item A and 17 x Item B
But if I have 2 x Item A in stock It will tell me I need 3 x Item A and 17 x Item B , not allowing for the 6 x Item B already present in the 2 x Item A
If I try to subtract the Stock On Hand, inside the recursive part of the query, it may subtract the same items several times.
I think I somehow need to use two recursive queries, but I'm not sure how.
WITH RECURSIVE RPL (ITEMCODE, LINECODE, ITEMNAME, PERQTY) AS
(
SELECT MAX(SALESHEADER.ORDERNUMBER), SALESLINES.LINECODE, MAX(SALESLINES.LINEDESCRIPTION), SUM(SALESLINES.ORDERQTY)
FROM SALESHEADER, SALESLINES
WHERE ORDERSTATUS = 'Open'
AND ORDERDESCRIPTION NOT SIMILAR TO '%F/C%'
AND SALESLINES.ORDERNUMBER = SALESHEADER.ORDERNUMBER
GROUP BY SALESLINES.LINECODE
UNION ALL
SELECT CHILD.ITEMCODE, CHILD.LINECODE, CHILD.LINEDESCRIPTION, CHILD.PERQTY*PARENT.PERQTY
FROM RPL PARENT, BOMLINES CHILD
WHERE PARENT.LINECODE = CHILD.ITEMCODE
AND CHILD.CODETYPE = 'Item Code'
)
SELECT RPL.LINECODE, MAX(RPL.ITEMNAME) AS DESCRIPTION , MAX(ITEMMASTER.ONHANDQTY) AS ONHAND, MAX(ITEMMASTER.SUPPLYQTY) AS SUPPLY,
SUM(RPL.PERQTY) AS DEMAND,
(MAX(ITEMMASTER.SUPPLYQTY) + MAX(ITEMMASTER.ONHANDQTY)) - SUM(RPL.PERQTY) AS AVAILABLE,
MAX(ITEMMASTER.ONHANDQTY) - SUM(RPL.PERQTY) AS FREE
FROM RPL JOIN ITEMMASTER ON ITEMMASTER.ITEMCODE = RPL.LINECODE
GROUP BY RPL.LINECODE;
Thanks in advance for any assistance.