How to Join to first row qlikview
I have the follwoing script
sales :
Load *
from test.qvd ;
LEFT JOIN(sales )
IntervalMatch (datecol)
LOAD DISTINCT [DATE DEBUT]
,[DATE FIN]
resident OC
;
LEFT JOIN (sales )
LOAD *
RESIDENT OC;
I got duplicates rows . so how to Join to first row in qlikview ?
I assuming residents have multiple sales. You can always GROUP BY whatever is duplicating and resolve the issue.
Related
I have three tables: The [inventory snapshot] table shows the name of the product and how many we have on site; the [inbound loads] table shows how many are coming in; the [outbound routes] table lists how many are going out.
I was getting unique values when I only had the first and second table (showing on-hand and 'arriving') but when I added in the third, I began getting multiple records instead of summed records.
Purpose of query
I work in a warehouse and I'm trying to isolate pick slots that are 1) low on inventory; 2)have no more product coming in; 3) I'd like to know if there are open orders to ship out any of the remaining product.
SELECT DISTINCT [inventory snapshot].locn_brcd,
[inventory snapshot].description,
[inventory snapshot].item_name,
Sum([inventory snapshot].on_hand_qty) AS SumOfON_HAND_QTY,
Sum([outbound routes].quantity) AS SumOfQuantity
FROM [outbound routes]
RIGHT JOIN ([inbound loads]
RIGHT JOIN [inventory snapshot]
ON [inbound loads].[wrin number] =
[inventory snapshot].item_name)
ON [outbound routes].[wrin number] =
[inventory snapshot].item_name
GROUP BY [inventory snapshot].locn_brcd,
[inventory snapshot].description,
[inventory snapshot].item_name,
[inbound loads].[quantity to receive]
HAVING ( ( ( Sum([inventory snapshot].on_hand_qty) ) < 10 )
AND ( ( [inbound loads].[quantity to receive] ) IS NULL ) );
Since you resolved your issue, consider further adjustment of grouping columns. Right now, the GROUP BY clause contains one more field than SELECT clause, namely: [inbound loads].[quantity to receive].
Typically, in aggregate SQL queries like this one, numeric values like quantity are not grouped but run as aggregated columns. You may have added it to GROUP BY in order to use it in HAVING. However, non-aggregates can be handled in WHERE to filter before aggregation. (Oddly, MS Access tends to move such level filtering in HAVING using Query Design.)
Consider below refactored SQL which uses table alias to avoid repetition of long table names. Of course, remove your needless location grouping.
SELECT s.locn_brcd
, s.description
, s.item_name
, SUM(s.on_hand_qty) AS SumOfON_HAND_QTY
, SUM(r.quantity) AS SumOfQuantity
FROM [outbound routes] r
RIGHT JOIN ([inbound loads] l
RIGHT JOIN [inventory snapshot] s
ON l.[wrin number] = s.item_name)
ON r.[wrin number] = s.item_name
WHERE l.[quantity to receive] IS NULL
GROUP BY s.locn_brcd
, s.description
, s.item_name
HAVING SUM(s.on_hand_qty) < 10 ;
By the way, the SQL industry tends to use LEFT JOIN rather than RIGHT JOIN for readability which you may not have control as this query may be output from Query Design. Try equivalent version without nesting JOINs where parentheses is required in Access SQL:
FROM ([inventory snapshot] s
LEFT JOIN ([inbound loads] l
ON l.[wrin number] = s.item_name)
LEFT JOIN [outbound routes] r
ON r.[wrin number] = s.item_name
I have two queries which I want to join together. Unionoption will not work as I want the values to be displayed in different fields. The 1st query is
SELECT Outage.DISTRICT, Count(Outage.DISTRICT) AS CountOfDISTRICT
FROM Outage LEFT JOIN [Site ID gets generated] ON Outage.[Site ID]=[Site ID gets generated].[Site ID]
GROUP BY Outage.DISTRICT;
The 2nd query is
SELECT Outage.DISTRICT, Count(Outage.[Site ID]) AS [CountOfSite ID]
FROM Outage
GROUP BY Outage.DISTRICT;
I tried the below code but it is giving syntax error (missing operator) in the 1st query
SELECT Outage.DISTRICT,
(SELECT Count(Outage.[Site ID]) AS [CountOfSite ID] FROM Outage),
(SELECT Count(Outage.DISTRICT) AS CountOfDISTRICT FROM Outage LEFT JOIN [Site ID gets generated] ON Outage.[Site ID]=[Site ID gets generated].[Site ID])
GROUP BY Outage.DISTRICT;
It would be very helpful if answer is also given with an explanation, as I am quite new to access and trying to learn it from scratch.
You can join the two queries together to get their columns side-by-side:
select
sq1.DISTRICT
, sq1.CountOfDISTRICT
, sq2.[CountOfSite ID]
from (
... your first query here ...
) as sq1
inner join (
... your second query here ...
) as sq2
on sq1.DISTRICT = sq2.DISTRICT
The thing to remember is that unions are for joining tables/queries vertically, while joins are for joining them horizontally.
I have three tables TeamTerritoryMapping, UpdQuotaTbl and tblInvoiceFile
When I join the first two tables I am getting the correct result
select
TTM.Team, Sum(Upd.Quota) as Quota
from
TeamTerritoryMapping TTM
inner join
UpdQuotaTbl upd on upd.ITM = TTm.Territory
where
upd.Month = 'july'
group by
TTM.Team
but when I join the third table for another column revenue from tblinvoicefile, some of the rows are getting duplicated and the end result is becoming higher. Below is the query which I am using to join 3 tables
select
TTM.Team,
Sum(upd.Quota) as quota,
sum(inv.[End MS Sales Revenue]) as Revenue
from
UpdQuotaTbl Upd
inner join
TeamTerritoryMapping TTM on TTM.Territory = upd.ITM
inner join
tblInvoiceFile inv on inv.[Inv Territory] = TTM.Territory
where
upd.Month = 'july'
and inv.[End Fiscal Month] = 'July, 2013'
So how can I eradicate the duplicate values in third table, I am getting the correct value when I join two tables ie TeamTerritoryMapping,UpdQuotaTbl and also tables TeamTerritoryMapping.
It looks like tblInvoiceFile has multiple entires for [Invenio Territory] which is causing this issue. If your intention is to bring in and sum all the [End MS Sales Revenue] for that table you can try something like this
SELECT TTM.Team,Sum(upd.Quota) as quota,sum(inv.[End MS Sales Revenue]) as Revenue
FROM UpdQuotaTbl Upd
INNER JOIN TeamTerritoryMapping TTM ON TTM.Territory = upd.ITM
INNER JOIN
(SELECT [Invenio Territory], SUM([End MS Sales Revenue]) AS [End MS Sales Revenue]
FROM tblInvoiceFile
WHERE [End Fiscal Month] = 'July, 2013'
GROUP BY [Invenio Territory]) AS inv
ON inv.[Invenio Territory] = TTM.Territory
WHERE upd.Month = 'july'
My initial response would be : The reason you're getting some results multiple times is because the ON used to join tblInvoiceFile is not 'unique' enough. If you look at the primary key (or a unique?) of said table you'll probably notice that it involves more than just the Inv Territory field.
To solve it you can do two different things.
Expand the ON clause to include more fields that make the connection unique
Keep the query as it is now but aggregate the results so the invoice information gets SUMmed
The problem is, it seems like you're already SUM()-ing the information (although as noted by Win, you're missing the GROUP BY), so I'm not quite sure why this would not work.
UPDATE: Only now realise that your problem is not doubled records, but doubled values in the first SUM(). Best way to solve this is by PRE-aggregating the values as shown by Abhi.
I am having difficulty getting all the rows to show from the 'Customer' table, as they are not all listed in the 'SalesOrder' table I have joined.
How do I get all the rows to show from the Customer table even if they have a 0 value? Thank you for any suggestions :)
SELECT Customer.CID, Customer.Name,
COUNT (SalesOrder.CID) AS NbrOrders,
SUM (FullPrice) AS [Total Value of Items]
FROM (Customer
INNER JOIN SalesOrder
ON Customer.CID=SalesOrder.CID)
GROUP BY Customer.CID, Customer.Name;
use LEFT JOIN instead of INNER JOIN
SELECT Customer.CID, Customer.Name,
COUNT (SalesOrder.CID) AS NbrOrders,
SUM (FullPrice) AS [Total Value of Items]
FROM Customer
LEFT JOIN SalesOrder
ON Customer.CID=SalesOrder.CID
GROUP BY Customer.CID, Customer.Name;
Use a left join. This will show values from second table which dont have corresponding data in first table.
i have to update total_orders of customers column to be equal to the total number of all the orders placed by the customer(in cust_order)
here is what i have tried
update (select *
from atish_customer a
inner join
(
select cust_nbr,count(cust_nbr) as count_orders
from atish_cust_order
group by cust_nbr
)c
on c.cust_nbr=a.cust_nbr)
set tot_orders=count_orders;
But this is the error i get
ORA-01779: cannot modify a column which maps to a non key-preserved table
How about this:
UPDATE customer SET total_orders = (
SELECT COUNT(*) FROM cust_order
WHERE cust_order.cust_nbr = customer.cust_nbr
)
[I'm not sure where your atish_customer and atish_customer_order comes into play...they're not shown in your diagram]
Explanation: Basically the inner select just counts the number of orders from the cust_order table for each cust_nbr. By joining the outer customer.cust_nbr to the inner cust_order.cust_nbr, each [outer] row will be updated with the correct total. This is called a correlated subquery (see here for a short tutorial).