sql compact 3.5, select top n rows from each group - sql

I am writing a query to select the top 1 record from each group. Keep in mind that I working on sql compact 3.5 and thus can not use the rank function. I'm pretty sure my query is incorrect but I'm not sure how to select top from each group. Any one got any ideas?
Here is the query I was trying to get working
/*
* added fH.InvoiceNumber to my query to get result further below.
/
select tH., t.CustomerNumber, c.CustomerName, fH.Status, fH.InvoiceNumber
from tenderHeader tH
join task t ON tH.TaskActivityID = t.ActivityID
join finalizeTicketHeader fH ON tH.FinalizeTicketTaskActivityID = fH.TaskActivityID
join customer c ON t.CustomerNumber = c.CustomerNumber
where fH.Status <> '3' AND t.TripID = '08ea6982-6efd-46fa-9753-0fd8b076f24c';
Here is what my tables look like:
customer table:
|------------------------------------------------|
| CustomerNumber | CustomerName | Address1 | ... |
|------------------------------------------------|
| 0012084737 | Customer A | 150 Rd A | ... |
|------------------------------------------------|
| 0012301891 | Customer B | 152 Rd A | ... |
|------------------------------------------------|
task table
|-----------------------------------------------------------------|
| ActivityID | TripID | TaskTypeName | Status | CustomerNumber |
|-----------------------------------------------------------------|
| 4967f6cc | 08ea6982 | Payment | 2 | 0012084737 |
|-----------------------------------------------------------------|
| e96469a1 | 08ea6982 | Payment | 2 | 0012301891 |
|-----------------------------------------------------------------|
finalizeTicketHeader table
|---------------------------------------------------|
| TaskActivityID | InvoiceNumber | Amount | Status |
|---------------------------------------------------|
| 916082c8 | 1000 | 563.32 | 3 |
|---------------------------------------------------|
| 916082c8 | 1001 | -343.68 | 0 |
|---------------------------------------------------|
| 4b38bf60 | 1002 | 152.29 | 0 |
|---------------------------------------------------|
| 4b38bf60 | 1003 | -35.80 | 0 |
|---------------------------------------------------|
tenderHeader table
|-------------------------------------------------------------------------------------|
| TaskActivityID | InvoiceNumber | PastDue | TodaysDue | FinalizeTicketTaskActivityID |
|-------------------------------------------------------------------------------------|
| 4967f6cc | 1234567891 | 23.55 | 219.64 | 916082c8 |
|-------------------------------------------------------------------------------------|
| e96469a1 | 1234567893 | 0.00 | 116.49 | 4b38bf60 |
|-------------------------------------------------------------------------------------|
the problem I was having was getting duplicates.
like so:
|------------------------------------------------------------------------------------------------------------------------------------|
| TaskActivityID | InvoiceNumber | PastDue | TodaysDue | FinalizeTicketTaskActivityID | CustomerNumber | CustomerName | InvoiceNumber |
|------------------------------------------------------------------------------------------------------------------------------------|
| 4967f6cc | 1234567891 | 23.55 | 219.64 | 916082c8 | 0012084737 | Customer A | 1001 |
|------------------------------------------------------------------------------------------------------------------------------------|
| e96469a1 | 1234567893 | 0.00 | 116.49 | 4b38bf60 | 0012301891 | Customer B | 1002 |
|------------------------------------------------------------------------------------------------------------------------------------|
| e96469a1 | 1234567893 | 0.00 | 116.49 | 4b38bf60 | 0012301891 | Customer B | 1003 |
|------------------------------------------------------------------------------------------------------------------------------------|
I've rewritten the query like so, but I need to get specific columns from the sub query.
select tH.* from tenderHeader th
inner join task t on tH.TaskActivityID = t.ActivityID
inner join (
select k.TaskActivityID from finalizeTicketHeader k group by k.TaskActivityID
) as fH on tH.FinalizeTicketTaskActivityID = fH.TaskActivityID
inner join customer c on t.CustomerNumber = c.CustomerNumber
I need to get the status from fH. Any ideas of how to do that?
select tH.*, fH.Status from tenderHeader th
inner join task t on tH.TaskActivityID = t.ActivityID
inner join finalizeTicketHeader fH on tH.FinalizeTicketTaskActivityID = tH.TaskActivityID
inner join customer c on t.CustomerNumber = c.CustomerNumber
where tH.FinalizeTicketTaskActivityID = (
select top (1) k.TaskActivityID from finalizeTicketHeader k
);
but it seems that sql compact 3.5 does not support scalar values with subquery in where cause.

Here is an example that demonstrat a way of selecting the top 1 from each group
id|time
--------
2 | 1:10
2 | 0:45
2 | 1:45
2 | 1:30
1 | 1:00
1 | 1:10
the table is called table_1; we group by id and assume that time should be desc ordered
select table_1.* from table_1
inner join (
select id, max(time) as max_time from table_1
group by id
) as t
on t.max_time = table_1.time and table_1.id = t.id
order by table_1.id
the result we get is
id|time
--------
1 | 1:10
2 | 1:45

Related

Left Outer Join with subqueries IN/EXIST at Hive

All, so I am trying to run the query.
The query consist of 7 tables and I want to all table get left joined based on A.conn_keyy and the others with clause 'ON'
My confusion comes when I want to join CPLCUR based on A , not works.
(CPLCUR.conn_keyy in ( a.conn_keyy = b.conn_keyy )
It appears error :
both left and right aliases encountered in join 'conn_key'
set hive.support.quoted.identifiers=none;
select
coalesce(a.conn_keyy, b.conn_keyy,CPLCUR.conn_keyy) as rrconn_keyy,
b.rfbbn, b.LINES_ID,b.TYPE,CPLCUR.*
FROM
(tablee.aa)A
LEFT OUTER JOIN
(tablee.bb) B
ON (A.conn_keyy = B.conn_keyy)
LEFT OUTER JOIN (SELECT `(c21)?+.+` FROM tablee.cc ) CPLCUR
ON (CPLCUR.conn_keyy in ( a.conn_keyy = b.conn_keyy )
AND CPLCUR.cllt = REGEXP_EXTRACT(B.rfbbn,'^(?:[^*]*\\*){2}([^*]*)',1))
LEFT OUTER JOIN (SELECT DISTINCT * FROM tablee.dd) CPLBAL
ON CPLBAL.conn_keyy = A.conn_keyy
AND CPLBAL.SEQUENCE = CPLCUR.SEQUENCE
AND CPLBAL.dtdt = '1999'
LEFT OUTER JOIN
(tablee.REP)REP
ON REP.relino = B.lnido
LEFT OUTER JOIN tablee.P PRD
ON PRD.PRODUCT_CODE = REGEXP_EXTRACT(A.conn_keyy,'[.]([^.]+)',1)
AND PRD.dtdt = '1999'
WHERE B.lnido LIKE 'PLCONS1%'
) rrvv;
What is best practice to get this?
The desired results:
+-----------+---------+--------+----------+-------------+-------+-----+-----+
| conn_keyy | b.rfbbn | b.LINES| b.TYPE | CPLCUR | CPLBAL| REP | PRD |
+-----------+---------+--------+----------+-------------+-------+-----+-----+
| 111 | aaa | PCOS1% | bbsr | 2019-02-21 | | | |
| 200 | | PCOS1% | ny | X | | | |
| 222 | bbb | PCOS1% | pp | Y | | | |
| 300 | rrr | PCOS1% | atl | 2019-03-18 | | | |
| 333 | ccc | PCOS1% | dd | Z | | | |
| 400 | vvv | PCOS1% | tt | 2019-03-18 | | | |
+-----------+---------+--------+----------+-------------+-------+-----+-----+

select count of sold products with 2 attributes on different rows

I am trying to generate a report of every product sold of SKUABC in size 34 with inseam 33 (it is available in 33 and 31 inseam).
Table - orders_products
Table - Orders:
+-----------+------------------------+--+
| Orders_id | date_purchased | |
+-----------+------------------------+--+
| 46198 | 2020-10-18 19:43:25 | |
| 46199 | 2020-10-19 19:43:25 | |
| 46200 | 2020-10-22 19:43:25 | |
+-----------+------------------------+--+
Table - orders_products
+--------------------+-----------+-------------+----------------+--+
| orders_products_id | Orders_id | products_id | products_mode | QTY
+--------------------+-----------+-------------+----------------+--+
| 42154907 | 46198 | 878 | SKUABC |1 |
| 42154908 | 46198 | 878 | SKUABC |1 |
| 42154909 | 46198 | 282 | DIFFSKU |1 |
+--------------------+-----------+-------------+----------------+--+
Table - Orders_products_attributes (showing order_id 46198 only):
+------------------------------+-----------+--------------------+-----------------+-----------------------+--+
| orders_products_attribute_id | orders_id | orders_products_id | Product options | Product_options_value | |
+------------------------------+-----------+--------------------+-----------------+-----------------------+--+
| 167618 | 46198 | 42155189 | Color | Green | |
| 167619 | 46198 | 42155189 | Inseam | 33 | |
| 167620 | 46198 | 42155189 | Size | 34 | |
+------------------------------+-----------+--------------------+-----------------+-----------------------+--+
my sql so far:
SELECT distinct o.orders_id, op.products_model, opa.products_options_values, sum(op.products_quantity)
FROM orders o
LEFT JOIN orders_products op
ON o.orders_id = op.orders_id
LEFT JOIN orders_products_attributes opa
on op.orders_id = opa.orders_id
WHERE op.products_model in ('SKUABC')
and opa.`products_options_values` in ('36')
and o.date_purchased > '2020-10-13'
If I add in :
and opa.`products_options_values` in ('31')
it returns no results, the reason being because the inseam and size rows are separate. and the problem with the above code is that it is combining any orders/ordered products where the inseam is both 33 or 31 but I want it to be separate.
My desired out would be
+--------+------------+------------+-------------------+
| model | attribute1 | attribute2 | quantity sold sum |
+--------+------------+------------+-------------------+
| ABCSKU | 34 | 33 | 120 |
+--------+------------+------------+-------------------+
Here is a fun solution: select two products_options_values and label those in different name then everything will be easy
SELECT distinct o.orders_id, op.products_model, opa.products_options_values
AS Inseem,opa2.products_options_values AS Size, sum(op.products_quantity)
FROM orders o
LEFT JOIN orders_products op
ON o.orders_id = op.orders_id
LEFT JOIN orders_products_attributes opa
ON op.orders_id = opa.orders_id
LEFT JOIN orders_products_attributes opa2
ON op.orders_id = opa.orders_id
--your condition below
then just use opa for inseem and opa2 for size.It is stupid but work.You can even make the rows data null by adding some condition with Product option column for easier insert later.

SQL Group By and Join based on a weird client table

I have 3 tables that I want to join together and group it to get client membership info. My code works for grouping the base table together but it breaks at the join part and I can't figure out why.
BASE TABLE : sales_detail
+-------+-----------+-----------+-----------------------------------------+
| order_date | transaction_id| product_cost | payment_type | country
+-------+-----------+-----------+------------------------------------------+
| 10/1 | 12345 | 20 | mastercard | usa
| 10/1 | 12345 | 50 | mastercard | usa
| 10/5 | 82456 | 50 | mastercard | usa
| 10/9 | 64789 | 30 | visa | canada
| 10/15 | 08546 | 20 | mastercard | usa
| 10/15 | 08546 | 90 | mastercard | usa
| 10/17 | 65898 | 50 | mastercard | usa
+-------+-----------+-----------+-------------------------------------+
table : client_information
+-------+-----------+-----------+-------------------+
| other_id | client_Type | item
+-------+-----------+-----------+----------+
| 112341 | new | hola |
| 112341 | old | mango |
| 145634 | old | pine |
| 879547 | old | vip |
| 745688 | new | unio |
| 745688 | old | dog |
| 147899 | new | cat |
| 124589 | new | amigo |
+-------+-----------+-----------+-----------+
table : connector
+-------+-----------+-----------+-------------------+
| transaction_ID | other_id | item
+-------+-----------+-----------+----------+
| 12345 | 112341 | hola |
| 82456 | 145634 | pine |
| 08157 | 879547 | unio |
| 08546 | 745688 | dog |
| 65898 | 147899 | cat |
| 06587 | 124589 | amigo |
+-------+-----------+-----------+-----------+
**I want the output to look something like this: **
IDEAL OUTPUT
+-------+-----------+-----------+--------------------------------+
| order_date | transaction_ID | product_cost | client_Type|
+-------+-----------+-----------+--------------------------------+
| 10/1 | 12345 | 70 | new |
| 10/5 | 82456 | 70 | old |
| 10/15 | 08546 | 110 | old |
| 10/17 | 65898 | 50 | new |
+-------+-----------+-----------+----------------------------------+
**i am trying to join my base table to the connector table by transaction ID to get other_id and items to match to client_type **
This is the code i used but it failed to compile after adding in left joins :
select t1.transaction_id, sum(t1.product_cost), t1.order_date, t3.client_type
from sales_detail t1
left join (select DISTINCT transaction_ID, other_id, fruits from connector) t2
ON t1.transaction_ID=t2.transaction_ID
left join (select DISTINCT order_id, client_type, fruits from client information) t3
ON t2.other_id=t3.other_id and t2.item=t3.item
where t1.payment_type='mastercard' and t1.order_Date between '2020-10-01' and'2020-10-31'
and country != 'canada'
GROUP BY t1.transaction_id, t1.order_date, t3.client_type;
Thanks in advance! I am a beginner so still learning the ins and outs of sql! (am using hive)
I think that's joins and aggregation. For more efficiency, you can pre-aggregate in a subquery, then join:
select sd.*, ci.client_type
from (
select order_date, transaction_id, sum(product_cost) product_cost
from sales_detail
where
payment_type = 'mastercard'
and order_date >= '2020-10-01'
and order_date < '2020-11-01'
and country <> 'canada'
group by order_date, transaction_id
) sd
inner join connector c on c.transaction_id = sd.transaction_id
inner join client_information ci on ci.other_id = c.other_id
Note that I rewrote the filter on order_date to use half-open intervals rather than between. This properly handles the case when your dates have a time portion.
From what I have understood, your code works although not as you would like using an INNER JOIN and it fails to add a LEFT JOIN. I think what happens is a failure due to the NULL elements. to add a NULL element and not get an error, you have to use some function that changes the NULL value to 0 .
One such function is the ISNULL(yourColumn, 0) function of T-SQL.The documentation.
I can see that in result table you only need clients who used mastercard, so you should use inner join there so only those client who used mastercard will be considered. While the remaining query is okay i guess, but main problem was the join on client information.
I think on the answer with GMB you also need to join on item column otherwise you will get multiple rows output.
select sd.*, ci.client_type
from (
select order_date, transaction_id, sum(product_cost) product_cost
from sales_detail
group by order_date, transaction_id
) sd
inner join connector c on c.transaction_id = sd.transaction_id
inner join client_information ci on ci.other_id = c.other_id and ci.item = c.item
Just modify with your filters and you should be sorted.

Left join and Group how to join the tables

What I need is to join the tables and pull the name of the series and its due seasons
tbl_seriados
serie_id | serie_slug | serie_nome
1 | slug-01 | slug 01
2 | slug-02 | slug 02
3 | slug-03 | slug 03
tbl_temporadas
temp_id | serie_id | serie_slug | temp_nome
1 | 2 | slug-02 | xxx 1
2 | 2 | slug-02 | xxx 2
3 | 3 | slug-03 | xxx 1
in that my select is looping for each result, how do I use GROUP BY?
SELECT * FROM tbl_seriados LEFT JOIN tbl_temporadas
ON tbl_seriados.serie_id = tbl_temporadas.serie_id
I want the following result
my url (will be dynamic)
www.mysite.com/slug-02
________________________
|serie_nome: slug 02 |
________________________
|temp_nome: xxx 1 |
|temp_nome: xxx 2 |
Result:
Here you don't have to use GROUP BY, you should use WHERE:
for www.mysite.com/slug-02:
SELECT temp_nome FROM tbl_seriados LEFT JOIN tbl_temporadas
ON tbl_seriados.serie_id = tbl_temporadas.serie_id
WHERE serie_slug='slug-02';
results:
temp_nome
____________
|xxx 1 |
|xxx 2 |

SQL Server - Master / Detail - get extract exact detail item match

I need help to extract the exact item match from two table. I want from Detail Table all the order that exists in Temp Detail and Master and Temp Master itemQty are the same.
example: in this case I need the sum of master.Amount for all the order which ItemQty is 3 and the detail table has the exact same value of temp Detail.
Master
OrderId| Amount | ItemQty |Status
------------------------------------------
125 | 36.75 | 3 | A |
------------------------------------------
128 | 11.95 | 5 | A |
------------------------------------------
Detail contain lot of order detail
------------------------------------------
OrderId| ItemId | Qty | Price |
------------------------------------------
125 | 12345 | 1 | 11.00 |
------------------------------------------
125 | 23456 | 1 | 12.75 |
------------------------------------------
125 | 34567 | 1 | 13.00 |
------------------------------------------
Temp Master
------------------------------------------
|OrderId| Amount | ItemQty |Status
------------------------------------------
|9999 | 36.75 | 3 | A |
------------------------------------------
Temp Detail
------------------------------------------
|OrderId | ItemId | Qty | Price |
------------------------------------------
| 9999 | 12345 | 1 | 11.00 |
------------------------------------------
| 9999 | 23456 | 1 | 12.75 |
------------------------------------------
| 9999 | 34567 | 1 | 13.00 |
------------------------------------------
thank you in advance
You can try with something like this
select m.OrderId, sum(m.amount)
from master m
join detail d
on m.OrderId = d.OrderId
join tempDetail td
on m.OrderId = td.OrderId
where m.ItemQty = 3
group by m.OrderId
having sum(d.Qty * d.Price) = sum(td.Qty * td.Price)
My best guess:
select Masterr.OrderId,
sum(Masterr.Amount)
from Masterr
where Masterr.ItemQty = 3
and exists(select *
from Detail
join TempMaster on TempMaster.ItemQty = Masterr.ItemQty
join TempDetail on TempDetail.OrderId = TempMaster.OrderId
where Detail.OrderId = Masterr.OrderId
and TempDetail.ItemId = Detail.ItemId
and TempDetail.Qty = Detail.Qty
and TempDetail.Price = Detail.Price)
group by Masterr.OrderId;