Join SQL Query Problem? - sql

First Select Statement:
SELECT
dbo.FG_FILLIN.PartNumber,
dbo.DropshipPackinglist.Shiplist_Qty,
dbo.DropshipPackinglist.Quantity
FROM dbo.FG_FILLIN INNER JOIN
dbo.DropshipPackinglist ON
dbo.FG_FILLIN.PartNumber = dbo.DropshipPackinglist.PartNumber
WHERE (dbo.FG_FILLIN.Batch = 'CIP_HK_6')
GROUP BY
dbo.FG_FILLIN.Batch,
dbo.FG_FILLIN.PartNumber,
dbo.FG_FILLIN.ItemNumber,
dbo.DropshipPackinglist.Shiplist_Qty,
dbo.DropshipPackinglist.Quantity
Result :
PartNumber Shiplist_Qty Quantity
P02-070161-00111-C100 6 3
P02-070161-10111-C100 6 3
2nd :
SELECT PartNumber,COUNT(Batch) AS Created
FROM dbo.FG_FILLIN
WHERE Batch='CIP_HK_6'
GROUP BY Batch,PartNumber
Result :
PartNumber Created
P02-070161-00111-C100 3
P02-070161-10111-C100 1
Joining this two Query I cant show this one: RESULT NEEDED
PartNumber Shiplist_Qty Quantity Created
P02-070161-00111-C100 6 3 3
P02-070161-10111-C100 6 3 1
It always show : when i Add Count(dbo.FG_FILLIN.Batch) as Created
PartNumber Shiplist_Qty Quantity Created
P02-070161-00111-C100 6 3 6
P02-070161-10111-C100 6 3 2
Any Advice.? Thanks In Regards!

Using a subselect:
SELECT f.PartNumber,
dpl.Shiplist_Qty,
dpl.Quantity,
(SELECT COUNT(Batch) AS Created
FROM dbo.FG_FILLIN x
WHERE x.batch = f.batch
AND x.partnumber = f.partnumber
GROUP BY Batch, PartNumber) AS created
FROM dbo.FG_FILLIN f
JOIN dbo.DropshipPackinglist dpl ON dpl.partnumber = f.partnumber
WHERE f.Batch = 'CIP_HK_6'
GROUP BY f.Batch, f.PartNumber, f.ItemNumber, dpl.Shiplist_Qty, dpl.Quantity
Using a JOIN to a derived table/inline view
SELECT f.PartNumber,
dpl.Shiplist_Qty,
dpl.Quantity,
x.created
FROM dbo.FG_FILLIN f
JOIN dbo.DropshipPackinglist dpl ON dpl.partnumber = f.partnumber
LEFT JOIN (SELECT t.partnumber,
t.batch,
COUNT(Batch) AS Created
FROM dbo.FG_FILLIN t
GROUP BY Batch, PartNumber) x ON x.partnumber = f.partnumber
AND x.batch = f.batch
WHERE f.Batch = 'CIP_HK_6'
GROUP BY f.Batch, f.PartNumber, f.ItemNumber, dpl.Shiplist_Qty, dpl.Quantity
Change "LEFT JOIN" to "JOIN" if you only want to see parts that have created values.

Related

How to get Odoo Inventory adjustment value through SQL

I am working on a custom stock valuation module and in one model I am trying to get adjustment value for a lot - product - warehouse wise of the previous day.
QUERY 1
SELECT COUNT(*)
FROM
(
SELECT stock_inventory.date AS stock_adjustment_date,
stock_move_line.lot_id,
stock_move_line.product_id,
SUM(stock_move_line.qty_done) total_stock_adjustment
FROM stock_move_line
LEFT JOIN stock_move ON stock_move_line.move_id = stock_move.id
LEFT JOIN stock_inventory ON stock_move.inventory_id = stock_inventory.id
WHERE stock_move.inventory_id IS NOT NULL
AND stock_move_line.location_id = 5
AND stock_move_line.location_dest_id = 13
AND stock_move_line.lot_id IS NOT NULL
GROUP BY stock_move_line.lot_id, stock_move_line.product_id, stock_inventory.date
ORDER BY total_stock_adjustment DESC
)
testTable;
QUERY 2
SELECT COUNT(*)
FROM
(
SELECT stock_inventory.date AS stock_adjustment_date,
stock_move_line.lot_id,
stock_move_line.product_id,
SUM(stock_move_line.qty_done) total_stock_adjustment
FROM stock_move_line
LEFT JOIN stock_move ON stock_move_line.move_id = stock_move.id
LEFT JOIN stock_inventory ON stock_move.inventory_id = stock_inventory.id
WHERE stock_move.inventory_id IS NOT NULL
AND stock_move_line.location_id = 13
AND stock_move_line.location_dest_id = 5
AND stock_move_line.lot_id IS NOT NULL
GROUP BY stock_move_line.lot_id, stock_move_line.product_id, stock_inventory.date
ORDER BY total_stock_adjustment DESC
)
testTable;
Why these both queries returning same count 14,849 ?
13 is the warehouse ID and 5 is the virtual location used for adjustment. What I am doing wrong here?

I have a SUM function in my SELECT but don't want it to show

I want to see the companies which deliver the most sold items in my DB.
Currently i have this Code:
SELECT L."Firma", SUM("B"."Anzahl")
FROM "Bestelldetails" B, "Artikel" A, "Lieferanten" L
WHERE L."Lieferanten-Nr" = A."Lieferanten-Nr"
AND A."Artikel-Nr" = B."Artikel-Nr"
GROUP BY L."Firma"
ORDER BY 2 DESC
The Output that i get:
Firma
2
Company 1
2756
Company 2
2377
Company 3
2063
...many more...
..XXX..
The Output that i want:
Firma
Company 1
Company 2
Company 3
...many more...
Code with output as image
But i don't want the row with the number 2 to show. I just want the Company-Names to show. How would i do so?
Simply ORDER BY the SUM():
SELECT L."Firma"
FROM "Bestelldetails" B
JOIN "Artikel" A
ON A."Artikel-Nr" = B."Artikel-Nr"
JOIN "Lieferanten" L
ON L."Lieferanten-Nr" = A."Lieferanten-Nr"
GROUP BY L."Firma"
ORDER BY SUM("B"."Anzahl") DESC
(Now using proper, explicit JOIN syntax.)
select Firma from
(
SELECT L."Firma", SUM("B"."Anzahl")
FROM "Bestelldetails" B, "Artikel" A, "Lieferanten" L
WHERE L."Lieferanten-Nr" = A."Lieferanten-Nr"
AND A."Artikel-Nr" = B."Artikel-Nr"
GROUP BY L."Firma"
ORDER BY 2 DESC
);

SQL : combine 2 rows into 1

This is my database
I'm trying to display "hello" and "wo" in the same column
My SQL statement:
SELECT
d.CampaignId, d.ClientID,
citn.ImagePath AS Thumbnail, cidi.ImagePath AS DetailImage
FROM
MasterData.CampaignImage AS d
INNER JOIN
MasterData.CampaignImage AS citn ON d.CampaignId = citn.CampaignId
AND d.ClientID = citn.ClientID
AND citn.ImageTypeId = 1
INNER JOIN
MasterData.CampaignImage AS cidi ON d.CampaignId = cidi.CampaignId
AND d.ClientID = cidi.ClientID
AND cidi.ImageTypeId = 2
The output:
But now I have 2 rows in my output, how can I combine these into just one row?
Simply do SELECT DISTINCT to skip duplicate rows.

Nesting Queries to get multiple column results

Have two queries , one collects moves in based on property and unit type the other would collect based on Move Outs for the same data. when ran separately they yield the correct information (move outs are 6 and move ins are 11) Have tried nesting in select and from statements but not getting what i need. When nested within the select am getting the correct move outs per unit type, but each line for move ins is total move ins. I recall that the nesting here would only return one value but know there is a way to return the value for each row. Any assistance is appreciated.
SELECT
p.scode as PropNumber,
p.saddr1 propname,
ut.scode as UnitType,
COUNT(t.hmyperson) as Moveouts,
(
SELECT COUNT(t.hmyperson) as MoveIns
FROM
tenant t
JOIN unit u ON t.hunit = u.hmy
JOIN property p ON p.hmy = u.hproperty
JOIN unittype ut ON ut.hmy = u.HUNITTYPE
WHERE
t.dtmovein >= getdate() - 14
AND p.scode IN ('gsaff')
) mi
FROM
Property p
JOIN unit u ON u.hproperty = p.hmy
JOIN tenant t ON t.hunit = u.hmy
JOIN unittype ut ON ut.hmy = u.HUNITTYPE
WHERE
p.scode IN ('gsaff')
AND t.DTMOVEOUT >= getdate()- 14
GROUP BY
ut.scode,
p.scode,
p.saddr1
With this data is coming out like :
PropNumber Propname UnitType MoveOuts MoveIns
1 x tc2 1 11
1 x tc3 2 11
1 x tc4 1 11
1 x tc5 1 11
1 x tc6 1 11 <pre>
Move in column should display as
2
5
1
0
3
You need to correlate the subquery according to the record being processed in the outer query. This also requires that you use different table aliases in the subquery than in the outer query.
It is hard to tell without seeing sample data, however I would expect that you need to correlate with all non-aggregated columns in the outer query.
Try changing :
(
SELECT COUNT(t.hmyperson) as MoveIns
FROM
tenant t
JOIN unit u ON t.hunit = u.hmy
JOIN property p ON p.hmy = u.hproperty
JOIN unittype ut ON ut.hmy = u.HUNITTYPE
WHERE
t.dtmovein >= getdate() - 14
AND p.scode IN ('gsaff')
) mi
To :
(
SELECT COUNT(t.hmyperson) as MoveIns
FROM
tenant t1
JOIN unit u1 ON t1.hunit = u1.hmy
JOIN property p1 ON p1.hmy = u1.hproperty
JOIN unittype ut1 ON ut1.hmy = u1.HUNITTYPE
WHERE
t1.dtmovein >= getdate() - 14
AND p1.scode IN ('gsaff')
AND p1.scode = p.scode
AND p1.saddr1 = p.saddr1
AND ut1.scode = ut.scode
) mi

Adding column contents in a SQL UNION query

So far I have this query
SELECT
COUNT(f.code_id) as item_count,
f.code_desc
FROM
foo f
INNER JOIN foohistory fh ON f.history_id = fh.history_id
WHERE
MONTH(fh.create_dt) = 6
AND YEAR(fh.create_dr) = 2010
GROUP BY
f.code_desc
UNION ALL
SELECT
COUNT(b.code_id) as item_count,
b.code_desc
FROM
bar b
INNER JOIN barhistory bh ON b.history_id = bh.history_id
WHERE
MONTH(bh.create_dt) = 6
AND YEAR(bh.create_dr) = 2010
GROUP BY
b.code_desc
My goal is to UNION these two queries add SUM the 'item_count' columns foreach code_desc. Is this possible?
Without more information about the codes, like if it's possible that codes are mutually exclusive between the two tables, use:
SELECT x.code_desc,
SUM(x.item_count)
FROM (SELECT f.code_desc,
COUNT(f.code_id) as item_count
FROM foo f
JOIN foohistory fh ON f.history_id = fh.history_id
WHERE MONTH(fh.create_dt) = 6
AND YEAR(fh.create_dr) = 2010
GROUP BY f.code_desc
UNION ALL
SELECT b.code_desc,
COUNT(b.code_id) as item_count
FROM bar b
JOIN barhistory bh ON b.history_id = bh.history_id
WHERE MONTH(bh.create_dt) = 6
AND YEAR(bh.create_dr) = 2010
GROUP BY b.code_desc) x
GROUP BY x.code_desc
Yeah, doing something like this
SELECT Sum(unionedTable.item_count)
FROM
(
//your query
) as unionedTable