I have orders table like this:
OID CID ODATE
1 1 01/01/21
2 2 01/02/21
3 2 20/01/21
4. 3. 20/01/21
5. 4. 20/01/21
I want to see all the orders of cid 2 first then all the others
thanks ...
You may order using a CASE expression:
SELECT *
FROM yourTable
ORDER BY CASE CID WHEN 2 THEN 1 ELSE 2 END;
You may also add more sorting levels after the above CASE expression.
Related
Initially we have such a table
contact_id
group_id
1
1
2
1
2
3
3
1
3
3
3
2
1
2
After that I make a query to search for groups containing the values of contacts 1 and 3
SELECT `group_id` ,COUNT(DISTINCT(`contact_id`)) AS `variants`
FROM `TaskTeam_member`
WHERE `contact_id`='1' OR `contact_id`='3'
GROUP BY `group_id`
HAVING `variants`='2'
it turns out that such a table (correct)
contact_id
variants
1
2
2
2
And now I need to add in addition to searching for values 1 and 3 in the group to check the total number of elements in it (I need 2), that is, if presumably there are elements 1 and 3 in group 1, but the total number of elements is 3 and not 2 as in the example above, then this group should not be output
the result should be like this
contact_id
variants
2
2
help me complete my request!
If you want to check for other contact_ids you need to include all records, but only count the ones you want:
SELECT `group_id`
FROM `TaskTeam_member`
GROUP BY `group_id`
HAVING COUNT(DISTINCT contact_id)=2 AND COUNT(DISTINCT CASE contact_id WHEN 1 THEN 1 WHEN 3 THEN 3 END)=2
I'm trying to create an sql (mariadb) request that select multiples columns but need two columns to be a unique pair but making sure the pair selected has its created_at value the least than the other duplicata pairs.
Here is what my table approximately looks like :
id
from_user_id
to_user_id
created_at
1
1
2
1000000005
2
2
1
1000000002
3
2
3
1000000008
4
5
6
999999999
5
6
5
100000006
I made this table precise to explain the request I want.
So I want to select the distinct pair (from_user_id, to_user_id) implying that the couple (1,2) which could also be (2,1) should be unique. The second rule is it should pick the couple with the minimum created_at value.
So the result table I want is :
id
from_user_id
to_user_id
created_at
2
2
1
1000000002
3
2
3
1000000008
4
5
6
999999999
2,1,1000000002 because the created_at is lesser than the other same couple case (1,2,1000000005).
In this case if I want only the values above created_at:999999999 to be selected I just have to add one condition.
I really hope my question is clear. I'm struggling to make distinct pairs work with other columns.
Thanks in advance for your answers.
WITH
cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY GREATEST(from_user_id,to_user_id),
LEAST(from_user_id,to_user_id)
ORDER BY created_at) rn
FROM table
)
SELECT *
FROM cte
WHERE rn = 1
Lets say this is the table I am talking about
id pId fId
1 1 1
2 2 1
3 2 2
4 3 2
I need to get a list of pId's who have a match to ALL of the given indices in a list of fId's.
What I mean is ->
Consider the list of fId's to be:
(1,2)
Then the result should be
2
Because only pId 2 has a match to all given entries in the list of fId's (which would be 1 and 2).
I couldn't find any way to do it so far - any help is highly appreciated :-)
Aggregate on pid column and use a having clause.
select pid
from tablename
group by pid
having sum(case when fid in (1,2) then 1 else 0 end) >= 2
I have the following table:
Line Date Time Order Qty
3 1140531 24737 S215037 1
3 1140531 24737 S215037 1
2 1140531 14955 S215058 1
2 1140531 14955 S215058 1
2 1140531 24055 S215059 1
2 1140531 24055 S215060 1
3 1140530 25319 S215099 1
3 1140530 25319 S215099 1
I need to be able to display the Line, work order, sum of qty (I need to show the order that was completed first on desc based on date and time). My end result should look like this:
Line Order Sum of Qty
2 S215058 2
2 S215059 1
2 S215060 1
3 S215099 2
3 S215037 2
Attempt:
I have a query that updates the data into a table where it's ordered by Line, Date, Time
then I apply the following query:
SELECT Line, Order, Sum(Qty)
From MyTable
Group by Line, Order
When I group the items, it groups my Orders based on alphabetical order and not the time. Please help!
I feel some contradiction between your description and expected result as you mention the word desc. Anyway the following query will give you the end result.
SELECT `Line`, `Order`, Sum(Qty)
From MyTable
Group by `Line`, `Order`
order by `Line`, `Date`, `Time`, `Order`;
SQL Fiddle here: http://www.sqlfiddle.com/#!2/456bf/8
I am able to fetch the results using the temp tables or looping over this table but I want to translate the following table into the required result in one query with great performance.
Order Table
OrdNbr LineNbr ItemName Qty
1 1 Pen 1
1 2 Pencil 2
1 3 Scale 2
2 5 Bottle 2
3 3 Pen 10
3 1 Pencil 5
Required Result:
OrdNbr OrdNbrFirstLineNbr ItemName Qty AllLineNumbers
1 1-1 Pen 1 1,2,3
2 2-5 Bottle 2 5
3 3-1 Pencil 5 1,3
OrdNbr and LineNbr are primary key for order table. I want to fetch only first record for the same OrdNbr.
Logic to get the result:
Find the distinct order number and get the lowest line number for the individual order number. Now display the order number, lowest line number and details for that lowest line number in that order. I want two extra derived fields OrdNbrFirstLineNbr and AllLineNumbers.
select OrdNbr,
LineNbr as OrdNbrFirstLineNbr,
ItemName,
Qty
from (
select *,
row_number() over (partition by OrdNbr order by LineNbr) as rn
from "Order" -- need to quote this, because order is a reserved word
) t
where rn = 1
Try this:
SELECT OrdNbr, LineNbr, ItemName, Qty
FROM Order
GROUP BY OrdNbr
ORDER BY LineNbr;