Query for two different scenarios - sql

I need to write a query which acheives the following requirements
1.) Need to get one row for the combination of "PrimaryID1" and "PrimaryID2", there are more than one field for this combination.
2.) There are two sub conditions here we need to take in to account.
2.a) For fields with "FlagField = 1"
In some combination of "PrimaryID1" and "PrimaryID2" we have "FlagField = 1" in this case only that particular field needs to be taken
2.b) For fields with "FlagField IS NULL"
If "FlagField IS NULL" then the requirement is like we need to pull the record for min of "PrimaryID3"(its a diff. field)
Here is how the data looks like
-----------------------------------------------------------------------------------------------
PrimaryID1 PrimaryID2 PrimaryID3 FlagField DataField1 DataField2
-----------------------------------------------------------------------------------------------
PPID11 PPID21 1 0 DF111 DF211
PPID11 PPID21 2 1 DF112 DF212
PPID11 PPID21 3 0 DF113 DF213
PPID12 PPID22 1 NULL DF121 DF221
PPID12 PPID22 2 NULL DF122 DF222
PPID12 PPID22 3 NULL DF123 DF223
Sample result
-----------------------------------------------------------------------------------------------
PrimaryID1 PrimaryID2 PrimaryID3 FlagField DataField1 DataField2
-----------------------------------------------------------------------------------------------
PPID11 PPID21 2 1 DF112 DF212
PPID12 PPID22 1 NULL DF121 DF221

You can use ROW_NUMBER to identify the row with the lowest PrimaryID3. Wrapping that in a case statement will allow you to 'conditionally combine' it with the FlagField. You then just need to filter on the result of the expression.
WITH cte AS
(
SELECT *, r = CASE WHEN FlagField IS NOT NULL THEN FlagField ELSE ROW_NUMBER() OVER(PARTITION BY PrimaryID1, PrimaryID2 ORDER BY CAST(REPLACE(PrimaryID3, 'PPID', '') AS INT)) END
FROM tbl
)
SELECT *
FROM cte
WHERE r = 1

Related

How add more rows when find string in column Oracle

Would it be possible to add more rows base on Keyword string in SQL ?
table A
PID PromotionName
1 OUT_EC_D10_V500K_FamilyCare_PROCO
2 OUT_EC_D5_V50K_Lunchbox_PROCO
3 OUT_EC_D5_V50K_PROCO
table B
promotion_code itm_name quantity
Lunchbox Item name 1 1
FamilyCare Item name 2 1
FamilyCare Item name 3 1
BUY1FREE6 Item name 4 1
HiSummer Item name 5 1
FamilyCare Item name 6 1
Example:
SELECT * FROM A where pid = '1';
Output of the SQL should be -
PID PromotionName Itm_name quantity
1 OUT_EC_D10_V500K_FamilyCare_PROCO
2 FamilyCare Item name 2 1
3 FamilyCare Item name 3 1
4 FamilyCare Item name 6 1
How to find string with keyword 'FamilyCare' in PromotionName of table A base on promotion_code of table B? If it exist it will add more rows in output
Any help with the SQL?
Here is how you can achieve this:
SELECT PID,PromotionName, '' as Itm_name, NULL as quantity
FROM A
WHERE pid = '1'
UNION
SELECT PID, PROMOTION_NAME, Itm_name, quantity
FROM
(SELECT * FROM A inner join B on a.promotionName LIKE '%'||b.promotion_name||'%')
WHERE pid='1'
You have to update your pid in both the places (before and after UNION).
Notice that tables were joined using LIKE operator with % before and after the word. Hence this joins if a part of a string is present in another column.
db<>fiddle link here
An option would be starting to construct a subquery factoring along with joining tables through a.promotionName LIKE '%'||b.promotion_code||'%' condition while filtering by b.promotion_code = 'FamilyCare', then add another query to combine the result sets by UNION ALL, and then enumerate with an id column by ROW_NUMBER() analytic function such as
WITH ab AS
(
SELECT a.*, b.*
FROM a
JOIN b
ON a.promotionName LIKE '%'||b.promotion_code||'%'
WHERE b.promotion_code = 'FamilyCare'
), ab2 AS
(
SELECT promotion_code, itm_name, quantity
FROM ab
UNION ALL
SELECT DISTINCT promotionName, NULL, NULL
FROM ab
)
SELECT ROW_NUMBER() OVER (ORDER BY itm_name NULLS FIRST) AS pid,
a.*
FROM ab2 a
if there's mismatch for the topmost query, then no row will be returned. eg. that query will check for the existence for the literal you provide
Demo

SQL to Merge Multiple Child Records into a single row with new columns

I have a table called OrderDetail. I also have a child table called PriceCategories.
Each OrderDetail can have multiple PriceCategories (eg A = $1, B = 2$, C = 3$, etc...)
I want to somehow allow a single resulting record when I am doing a SQL Query on OrderDetail joined to PriceCategories, but returning each record in PriceCategories as columns in a single row.
For example using a normal Join I would end up with.
OrderID | PriceCat | Amount
1 A 1
1 B 2
1 C 3
What I want to end up with is something like;
OrderId | CatAAmount | CatBAmount | CatCAmount
1 1 2 3
Is there anyway within a SQL statement that can achieve this?
Use a case statement -
SELECT order_id,
CASE
WHEN pricecat = 'A' THEN
amount
END AS cataamount,
CASE
WHEN pricecat = 'B' THEN
amount
END AS catbamount,
CASE
WHEN pricecat = 'C' THEN
amount
END AS catcamount
FROM table
Using pivot.
SELECT OrderID, [A] CatAAmount,[B] CatBAmount, [C] CatCAmount
FROM
(SELECT OrderID, PriceCat, Amount
FROM test) p
PIVOT
(
max (Amount)
FOR PriceCat IN
( [A],[B],[C])
) AS pvt;
If you have more categories you can simply add them in the 'FOR' and 'SELECT' clause.
Please refer http://sqlfiddle.com/#!18/e19ac/13/0

if count value of the column is greater than 1, I want to print the count of the column else I want to print value in the field

I am writing a query which fetches details from different tables. In one column I want to print count value of a column. If the count value of the column is greater than 1, I want to print the count of the column else I want to print value in the field.
I want to build a query which will give me count of user_id from table 1 & 2. if the count user_id is greater than 1, then print count (user_id) else print value of user_id
Table:1
| user_id |
| John |
| Bob |
| Kris |
| Tom |
Table:2
| user_id |
| Rob |
query result should list count of table1 as it greater than 1. Table2 should list Rob as it is lesser than 2
You want to select user IDs (names actually) from a table. If it's just one row then show that name, otherwise show the number of entries instead. So, just use a CASE expression to check whether count is 1 or greater than 1.
You probably need CAST or CONVERT to turn the count number into a string, so the CASE expression always returns the same type (this is how CASE works).
select
case when count(*) > 1
then cast(count(*) as varchar(100))
else max(user_id)
end as name_or_count
from mytable
Window Functions come to mind but since your user_ids are not numbers, you'll run into an issue where you can't have two different data types in the same column. See how this works for you. Make sure to cast the varchar numbers back to integer if this script is part of a larger process.
with cte as
(select 'John' as user_id union all
select 'Bob' as user_id union all
select 'Kris' as user_id union all
select 'Tom' as user_id)
select distinct case when count(*) over() > 1
then cast(count(*) over() as varchar) else user_id end
from cte
with cte as
(select 'Rob' as user_id)
select distinct case when count(*) over() > 1
then cast(count(*) over() as varchar) else user_id end
from cte

SQL aggregate rows with same id , specific value in secondary column

I'm looking to filter out rows in the database (PostgreSQL) if one of the values in the status column occurs. The idea is to sum the amount column if the unique reference only has a status equals to 1. The query should not SELECT the reference at all if it has also a status of 2 or any other status for that matter. status refers to the state of the transaction.
Current data table:
reference | amount | status
1 100 1
2 120 1
2 -120 2
3 200 1
3 -200 2
4 450 1
Result:
amount | status
550 1
I've simplified the data example but I think it gives a good idea of what I'm looking for.
I'm unsuccessful in selecting only references that only have status 1.
I've tried sub-queries, using the HAVING clause and other methods without success.
Thanks
Here's a way using not exists to sum all rows where the status is 1 and other rows with the same reference and a non 1 status do not exist.
select sum(amount) from mytable t1
where status = 1
and not exists (
select 1 from mytable t2
where t2.reference = t1.reference
and t2.status <> 1
)
SELECT SUM(amount)
FROM table
WHERE reference NOT IN (
SELECT reference
FROM table
WHERE status<>1
)
The subquery SELECTs all references that must be excluded, then the main query sums everything except them
select sum (amount) as amount
from (
select sum(amount) as amount
from t
group by reference
having not bool_or(status <> 1)
) s;
amount
--------
550
You could use windowed functions to count occurences of status different than 1 per each group:
SELECT SUM(amount) AS amount
FROM (SELECT *,COUNT(*) FILTER(WHERE status<>1) OVER(PARTITION BY reference) cnt
FROM tc) AS sub
WHERE cnt = 0;
Rextester Demo

SQL Query Help: Returning distinct values from Count subquery

I've been stuck for quite a while now trying to get this query to work.
Here's the setup:
I have a [Notes] table that contains a nonunique (Number) column and a nonunique (Result) column. I'm looking to create a SELECT statement that will display each distinct (Number) value where the count of the {(Number), (Result)} tuple where Result = 'NA' is > 25.
Number | Result
100 | 'NA'
100 | 'TT'
101 | 'NA'
102 | 'AM'
100 | 'TT'
200 | 'NA'
200 | 'NA'
201 | 'NA'
Basically, have an autodialer that calls a number and returns a code depending on the results of the call. We want to ignore numbers that have had an 'NA'(no answer) code returned more than 25 times.
My basic attempts so far have been similar to:
SELECT DISTINCT n1.Number
FROM Notes n1
WHERE (SELECT COUNT(*) FROM Notes n2
WHERE n1.Number = n2.Number and n1.Result = 'NA') > 25
I know this query isn't correct, but in general I'm not sure how to relate the DISTINCT n1.Number from the initial select to the Number used in the subquery COUNT. Most examples I see aren't actually doing this by adding a condition to the COUNT returned. I haven't had to touch too much SQL in the past half decade, so I'm quite rusty.
you can do it like this :
SELECT Number
FROM Notes
WHERE Result = 'NA'
GROUP BY Number
HAVING COUNT(Result) > 25
Try this:
SELECT Number
FROM (
SELECT Number, Count(Result) as CountNA
FROM Notes
WHERE Result = 'NA'
GROUP BY Number
)
WHERE CountNA > 25
EDIT: depending on SQL product, you may need to give the derived table a table correlation name e.g.
SELECT DT1.Number
FROM (
SELECT Number, Count(Result) as CountNA
FROM Notes
WHERE Result = 'NA'
GROUP
BY Number
) AS DT1 (Number, CountNA)
WHERE DT1.CountNA > 25;