query solution sql [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i struggling with query:
select bl.REF_CORP_ID as [Dept id] , bl.REF_CORE_BUDGET_ID, rc.Col_1 , bl.SubmittedDate as [Created Date time] , bl.APPROVED_BY , bl.APPROVED_DATETIME , bl.APPROVAL_FROM
from TABLE1 bl right join TABLE2 rc on bl.REF_CORP_ID =rc.ref_corp_id AND bl.REF_CORE_BUDGET_ID =1
group by bl.REF_CORP_ID , REF_CORE_BUDGET_ID , Col_1 , SubmittedDate , APPROVED_BY , APPROVED_DATETIME , APPROVAL_FROM
result in following manner:
1 1 IT 2013-07-10 19:29:40.700
1 1 IT 2013-07-10 19:29:40.700
2 1 Sales NULL
2 1 Sales NULL
4 1 HR 2013-07-10 19:32:21.720
4 1 HR 2013-07-10 19:32:21.720
6 1 management 2013-07-10 20:24:29.890
but this is a i want:
1 1 IT 2013-07-10 19:29:40.700
2 1 Sales NULL
4 1 HR 2013-07-10 19:32:21.720
6 1 management 2013-07-10 20:24:29.890
i have two table in which i applied inner join but in one table have same record and second table have only department info with dept name

The crude solution to this issue is to add the keyword DISTINCT:
SELECT DISTINCT a,b,c,d
And this will remove rows which are exact duplicates. However, duplicate rows is almost always the sign of a logical error elsewhere in the query - it's better to fix that than to just bodge things with DISTINCT.

Related

SQL Query to output single column with below values in each row - 1 2 2 3 3 3 4 4 4 4 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Need to write SQL query for getting below output with the given input table:
Input Table:
Col1
1
2
3
Output:
Col1
1
2
2
3
3
3
WITH CTE(NN)AS
(
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5
)
SELECT C.NN
FROM CTE AS C
CROSS JOIN CTE C2
WHERE C2.NN<=C.NN
CTE is your input table

How to Update tables based on self join [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Update table based on self join on customer_po = ID
Table XYZ
ID Invoice Date Delivery_date Customer_po
123 01-01-2018 null null
125 10-01-2018 null 123
I want Output record like below in Oracle SQL
ID Invoice Date Delivery_date Customer_po
123 01-01-2018 01-01-2018 null
125 10-01-2018 01-01-2018 123
You need an update statement like this.
UPDATE xyz
SET Delivery_date =
(SELECT MAX (invoice_date)
FROM xyz
WHERE Customer_po = 123);
Note that I have used MAX to avoid the errors due to multiple values for Customer_po = 123

Postgres : get min and max rows count in many to many relation table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have mapping table for RFQ(request for quotation) and Vendor's bid amount.
rfq_vendor_mapping :
id rfq_id(FK) vendor_id(FK) amount
---------------------------------------
1 1 1 100
2 1 2 50
3 2 1 200
4 2 3 300
5 2 2 40
6 3 4 70
7 3 1 90
8 3 2 250
9 4 3 30
10 5 1 500
In above table, I want analysis for how many times vendor has submitted minimum and maximum bid for each RFQ.
Expected Output :
vendor_id min_bid_count max_bid_count
-----------------------------------------
1 1 2
2 2 1
3 1 2
4 1 0
http://sqlfiddle.com/#!15/60198/1
Compare the vendor's amount with min and max from a window function and run a conditional count on outer query level:
SELECT vendor_id
, count(min_bid OR NULL) AS min_bid_count
, count(max_bid OR NULL) AS max_bid_count
FROM (
SELECT vendor_id
, amount = min(amount) OVER w AS min_bid
, amount = max(amount) OVER w AS max_bid
FROM rfq_vendor_mapping
WINDOW w AS (PARTITION BY rfq_id)
) sub
GROUP BY 1
ORDER BY 1;
SQL Fiddle.

HP vertica SQL query to create new column [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
This is the table I have and I have 5 distinct programs. when a user like a program it reads as follows:
User Program
----------------
A 1
A 4
B 2
B 4
B 5
However I want to write a query that will allow me to also see the 5 distinct programs for each of my user and create a new column that will take two value (binary) 1 if the user liked a specific program and if not 0. Any help will be appreciated. Thanks
User Program NewColumn
A 1 1
A 2 0
A 3 0
A 4 1
A 5 0
B 1 0
B 2 1
B 3 0
B 4 1
B 5 1
You can do this with a cross join and left join:
select u.user, p.program,
(case when t.user is not null then 1 else 0 end) as NewCol
from (select distinct user from table) u cross join
(select distinct program from table) p left join
table t
on u.user = t.user and p.program = t.program;
Note: You may already have tables with the users and the programs. If so, use those instead of the subqueries.

How to get the cumulative column from my sales table? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have the following scenario
pid & month form a composite primary key .
pid month amount
1 1 10
2 2 15
1 2 20
1 3 10
3 3 4
2 3 6
Now the column to be generated with the table will be like this
pid month amount sum
1 1 10 10
2 2 15 15
1 2 20 30
1 3 10 40
3 3 4 4
2 3 6 21
What should be the query ?
This query will do the trick :
SELECT t1.*, sum(t2.amount)
FROM Table1 t1
INNER JOIN Table1 t2 ON t1.pid = t2.pid AND t1.month >= t2.month
GROUP BY t1.pid, t1.month, t1.amount
See SQLFIDDLE : http://www.sqlfiddle.com/#!3/db350/7/0
If using SQL Server 2012:
SELECT *,SUM(amount) OVER(PARTITION BY pid ORDER BY month ROWS UNBOUNDED PRECEDING)'Total'
FROM YourTable
You did not specify what version of SQL Server you are using but you should be able to use the following to get the running total in any version:
select t1.pid,
t1.month,
t1.amount,
(select sum(t2.amount)
from yourtable t2
where t1.pid = t2.pid
and t2.month <= t1.month) total
from yourtable t1;
See SQL Fiddle with Demo