I have table like that:
id
document_id
category_id
is_main_category
1765
210
181
0
1764
210
179
1
1763
201
179
1
1762
209
181
0
1761
209
179
1
1754
211
182
0
1753
211
180
1
I need to select:
SELECT * FROM `documents` WHERE document_id = 210;
And if the number of rows is less than 2 I would like to insert into table one more row, like that:
INSERT INTO document_category (document_id, category_id, is_main_category)
VALUES (210, 181, 0);
Could you please help me to merge those 2 constructions.
Maybe you can build upon this:
INSERT INTO document_category (document_id, category_id, is_main_category)
SELECT 210, 181, 0
FROM documents
WHERE document_id = 210
GROUP BY document_id
HAVING COUNT(*) < 2;
If i understand you correctly, this should be what you looking for.
INSERT INTO
document_category(document_id, category_id, is_main_category)
SELECT
210, 181, 0
FROM
dual
WHERE
(SELECT COUNT(1) FROM documents WHERE document_id = 210) < 2;
Related
Given this data:
Bolt_Table:
PID
UNIQ ID
GROUP_ID
Distance
PID_24_2225
14
13
1141
PID_5_1444E
3214
13
652
PID_5_14454
3152
13
802
PID_24_2225
15
14
1141
PID_5_14454
3151
14
802
PID_5_1444E
3213
14
652
PID_26_21FC
536
2300
597
PID_5_13388
4121
2300
620
PID_5_13382
4169
2300
802
This is the desired result:
PID
UNIQ_ID
GROUP_ID
Distance
PID_5_1444E
3214
13
652
PID_5_1444E
3213
14
652
PID_5_13388
4121
2300
620
Explanation:
1st Record: #Group ID = 13,
Get the similar PID
PID_5_1444E and PID_5_14454 - compare the corresponding distances minimum of (652 and 802). Since 652 is the least, the corresponding PID: " PID_5_1444E " should be retained, hence record 1 of the desired table
What would be the query for SQL? (Microsoft Access)
I tried using LIKE, MID(String,1,4), GROUP BY & HAVING but nothing seems to work. How should I make the query for this?
The closest one I got is when I force to do the judging under a hard coded GROUP_ID,I would like to do it FOR EACH GROUP_ID
SELECT TOP 1 PERCENT PID, UNIQ_ID, GROUP_ID, Distance
FROM
(
SELECT
a.PID, a.UNIQ_ID, a.GROUP_ID, ID, a.Distance,
(select count(PID) as counter from Bolt_Table where GROUP_ID = a.GROUP_ID and LEFT(PID, 9) = LEFT(a.PID, 9)) as counter from Bolt_Table a WHERE a.GROUP_ID = 13
)
where counter > 1
order by Distance
SELECT b.pid, b.[uniq id], b.group_id, b.distance FROM bolt_table as b INNER JOIN (SELECT group_id, min(distance) as mindist FROM bolt_table GROUP BY group_id) as a on b.group_id = a.group_id AND b.distance = a.mindist
I have receipt data with receipt numbers like this:
receipt_id item_id
123 321
123 322
123 323
124 321
124 322
125 321
125 323
I need to renumber the receipt_id so it looks like this:
receipt_id item_id
1 321
1 322
1 323
2 321
2 322
3 321
3 323
row_over or row_over partition by does not seem to get me what I want. I can do this by selecting distinct ids, renumber them, and then join them back. But there has to be a simpler, more correct way, or?
I think you can use DENSE_RANK() for this:
WITH T(receipt_id, item_id) AS(
VALUES
(123, 321),
(123, 322),
(123, 323),
(124, 321),
(124, 322),
(125, 321),
(125, 323)
)
SELECT DENSE_RANK() OVER(ORDER BY receipt_id) AS receipt_id, item_id
FROM T
I have a similar problem like this thread but is bit different what it required
https://community.oracle.com/thread/4132183
I have the following table:
Table1:
ID empID employeeactive dtefrom dateto mgrid
1 123 1 1/10/2016 113
2 213 0 1/20/2015 1/20/2016 323
3 213 1 1/20/2016 423
4 312 0 1/05/2016 1/30/2017 523
5 512 1 1/30/2017 623
6 812 1 2/30/2017 6543
Table2:
empID emplyactive supid
123 1 -
213 1 -
312 1 -
512 0 -
612 1 -
712 1 -
812 1 872
912 0 222
I have this table instead of - i want to replace with mgrid in table 1.. and table2 have extra data which is not in table1 so i have to ignore the extra data if supid '-' and also want to have emplyactive =1 but some of the emplyactive=1 table 1 has multiple mgr id ...
so I tried this one
select empid , decode(supid,'-',mgrid,supid) from table2,table1 where
empid(+) = empid and emplyactive =1 and employeeactive=1
so I am getthing how to solve this please help me out thank you .. if some thing like and exists will work thanks in advance.
This is what I am trying to insert in a package body oracle.
This is how the output looks like:
empID emplyactive supid
123 1 113
213 1 423
812 1 872
select a.empid, a.emplyactive, max(a.supid) supid
from (
select * from #table2
union
select empid, employeeactive, mgrid from #table1)a
left join #table1 b on a.empid=b.empid and employeeactive=1
join #table2 c on a.empid=c.empid and c.emplyactive=1
where a.emplyactive=1
and a.supid<>0
group by a.empid, a.emplyactive
I have 3 tables in a Postgres 9.5 DB like below,
threshold
id threshold_amount
----------------------
111 100
112 200
113 80
customers - each customer has a threshold_id of threshold table
id customer_name threshold_id
--------------------------------
313 abc 111
314 xyz 112
315 pqr 113
charges - per customer there is charges so this table has customer_id
id customer_id amount post_date
------------------------------------
211 313 50 4/1/2017
212 313 50 4/30/2017
213 313 50 5/15/2017
214 314 100 3/1/2017
215 314 50 3/21/2017
216 314 50 4/21/2017
217 314 100 5/1/2017
218 315 80 5/5/2017
I want to query it and return the specific post_date with sum( amount ) == threshold_amount by ascending order of charges.id column,
The resultset look like below,
customer_id post_date
-----------------------
313 4/30/2017
314 4/21/2017
315 5/5/2017
I've tried sum( amount ) with group by customer_id and call the one separate the stored procedure from select clause and pass the amount, post_date and threshold_amount then created one temp table and insert post_date into it if the above condition get match and then again access that temp table but it seems something not valid so I want to know if some other solution or Can I do it in query?
Thanks
Your question is asking about an exact match for the threshold. This is basically a cumulative sum:
select cct.*
from (select ch.customer_id, ch.amount,
sum(ch.amount) over (partition by ch.customer_id order by post_date) as running_amount,
t.threshold_amount
from charges ch join
customers c
on ch.customer_id = c.id join
threshholds t
on c.threshold_id = t.id
) cct
where running_amount = threshold_amount;
try this:
select
c.customer_id,
c.post_date
from charges c
join customers cu on cu.id = c.customer_id
join threshold t on t.id = cu.threshold_id
where (select sum(cc.amount) from charges cc where cc.id <= c.id
and cc.customer_id = c.customer_id) = t.threshold_amount
How can I write a query in SQL Server such as rank() but a bit different calculate.
For example rank is:
rankNumber uniqeId
1 160
2 159
3 158
4 157
5 156
5 156
7 152
8 151
8 151
10 150
I need the result like these:
rankNumber uniqeId
1 160
2 159
3 158
4 157
5 156
5 156
6 152
7 151
7 151
8 150
How can I do this? Is there such a function in SQL Server?
SELECT DENSE_RANK() OVER (ORDER BY TotCnt DESC) AS TopCustomers, CustomerID, TotCnt
FROM (SELECT CustomerID, COUNT(*) AS TotCnt
FROM Orders Group BY CustomerID) AS Cust
OUTPUT
To expand on the DENSE_RANK comment, the full query is short and sweet:
SELECT
DENSE_RANK() OVER (ORDER BY uniqueId DESC) AS rankNumber,
uniqueId
FROM myTable
ORDER BY rankNumber
There's a SQL Fiddle here