Trying to combine 2 columns into one distinct result - sql

I'm trying to concatenate 2 rows, from different tables into one result. this is the code I currently have...
SELECT CONCAT(C.Correct_Answer, I.Incorrect_Answers) AS Answers
FROM Cor_A AS C, Inc_A AS I
and this is what it returns
Answers
----------------------------
45
410
450
4Green
4Red
4Potato
4Yellow
40
42
This is, however, not what I want.
How do I get them to return in separate records?
4
5
10
50
Green
Red
Potato
Yellow
(What should be "Blue")
any help is appreciated guys, as you can tell, I'm pretty new...

This will eventually return all the records in both fields but if a record is common it return only one record
SELECT Correct_Answer as Answer
FROM Cor_A
UNION
SELECT Incorrect_Answers
FROM Inc_A;

Related

SQL to return records that do not have a complete set according to a second table

I have two tables. I want to find the erroneous records in the first table based on the fact that they aren't complete set as determined by the second table. eg:
custID service transID
1 20 1
1 20 2
1 50 2
2 49 1
2 138 1
3 80 1
3 140 1
comboID combinations
1 Y00020Y00050
2 Y00049Y00138
3 Y00020Y00049
4 Y00020Y00080Y00140
So in this example I would want a query to return the first row of the first table because it does not have a matching 49 or 50 or (80 and 140), and the last two rows as well (because there is no 20). The second transaction is fine, and the second customer is fine.
I couldn't figure this out with a query, so I wound up writing a program that loads the services per customer and transid into an array, iterates over them, and ensures that there is at least one matching combination record where all the services in the combination are present in the initially loaded array. Even that came off as hamfisted, but it was less of a nightmare than the awkward outer joining of multiple joins I was trying to accomplish with SQL.
Taking a step back, I think I need to restructure the combinations table into something more accommodating, but I still can't think of what the approach would be.
I do not have DB2 so I have tested on Oracle. However listagg function should be there as well. The table service is the first table and comb the second one. I assume the service numbers to be sorted as in the combinations column.
select service.*
from service
join
(
select S.custid, S.transid
from
(
select custid, transid, listagg(concat('Y000',service)) within group(order by service) as agg
from service
group by custid, transid
) S
where not exists
(
select *
from comb
where S.agg = comb.combinations
)
) NOT_F on NOT_F.custid = service.custid and NOT_F.transid = service.transid
I dare to say that your database design does not conform to the first normal form since the combinations column is not atomic. Think about it.

Creating calculated result set based on looping through 2 columns and multiplying

I have 2 columns in a sql database that are integers (in separate tables), i'll simplify my problem to explain. I have to replicate what has been done in excel in SQL and don't know how to do it. In basic terms I have to loop through all of one table multiplying all by the first record in the 2nd table, then the 2nd row and so on. Building up a table. I don't know how to do this does anyone have any ideas? Example below.
Column A:| Column B:
12 | 36
24 | 89
26
The result output should a table like:
A | B
432 | 1068
864 | 2136
936 | 2314
So 36 has been multiplied by 12, 24 then 26 to create the first column and then 89 multiplied by 12, 24 then 26 to create the 2nd column and so on
The reality columns have 400+ rows to multiply by so the result is a huge table, how can i do this looping through and adding columns to build a calculated table. Hope that makes sense, any help would be greatly appreciated.
Since I dont know how your database looks like here is a generic code that could work for you.
select tableA.colA, tableB.colb, (tableA.colA*tableB.colB) as colC
from tableA
join tableB (on ... if there is something to join on).
This will give you three columns though, the two original columns and the third multiplied column. You could ofcourse remove colA and colB oif you dont need them, they are just there for reference.
this code should work in most sql-languages.
Many thanks for your help, I sorted out the problem with a cross join

Access SQL query select with a specific pattern

I want to select each 5 rows to be unique and the select pattern applies for the rest of the result (i.e if the result contains 10 records I am expecting to have 2 set of 5 unique rows)
Example:
What I have:
1
1
5
3
4
5
2
4
2
3
Result I want to achieve:
1
2
3
4
5
1
2
3
4
5
I have tried and searched a lot but couldn't find anything close to what I want to achieve.
Assuming that you can somehow order the rows within the sets of 5:
SELECT t.Row % 5, t.Row FROM #T t
ORDER BY t.Row , t.Row % 5
We could probably get closer to the truth with more details about what your data looks like and what it is you're trying to actually do.
This will work with the sample of data you provided
SELECT DISTINCT(thevalue) FROM theresults
UNION ALL
SELECT DISTINCT(thevalue) FROM theresults
But it's unclear to me if it's really what you need.
For instance :
if your table/results returns 12 rows, do you still want 2x5 rows or do you want 2x6 rows ?
do you have always in your table/results the same rows in double ?
There's a lot more questions to rise and no hint about them in what you asked.

Multicriteria Insert/Update

I'm trying to create a query that will insert new records to a table or update already existing records, but I'm getting stuck on the filtering and grouping for the criteria I want.
I have two tables: tbl_PartInfo, and dbo_CUST_BOOK_LINE.
I'm want to select from dbo_CUST_BOOK_LINE based upon the combination of CUST_ORDER_ID, CUST_ORDER_LINE_NO, and REVISION_ID. Each customer order can have multiple lines, and each line can have multiple revision. I'm trying to select the unique combinations of each order and it's connected lines, but take the connected information for the row with the highest value in the revision column.
I want to insert/update from dbo_CUST_BOOK_LINE the following columns:
CUST_ORDER_ID
PART_ID
USER_ORDER_QTY
UNIT_PRICE
I want to insert/update them into tbl_PartInfo as the following columns respectively:
JobID
DrawingNumber
Quantity
UnitPrice
So if I have the following rows in dbo_CUST_BOOK_LINE (PART_ID omitted for example)
CUST_ORDER_ID CUST_ORDER_LINE_NO REVISION_ID USER_ORDER_QTY UNIT_PRICE
SCabc 1 1 0 100
SCabc 1 2 4 150
SCabc 1 3 4 125
SCabc 2 3 2 200
SCxyz 1 1 0 0
SCxyz 1 2 3 50
It would return
CUST_ORDER_ID CUST_ORDER_LINE_NO (REVISION_ID) USER_ORDER_QTY UNIT_PRICE
SCabc 1 3 4 125
SCabc 2 3 2 200
SCxyz 1 2 3 50
but with PART_ID included and without REVISION_ID
So far, my code is just for the inset portion as I was trying to get the correct records selected, but I keep getting duplicates of CUST_ORDER_ID and CUST_ORDER_LINE_NO.
INSERT INTO tbl_PartInfo ( JobID, DrawingNumber, Quantity, UnitPrice, ProductFamily, ProductCategory )
SELECT dbo_CUST_BOOK_LINE.CUST_ORDER_ID, dbo_CUST_BOOK_LINE.PART_ID, dbo_CUST_BOOK_LINE.USER_ORDER_QTY, dbo_CUST_BOOK_LINE.UNIT_PRICE, dbo_CUST_BOOK_LINE.CUST_ORDER_LINE_NO, Max(dbo_CUST_BOOK_LINE.REVISION_ID) AS MaxOfREVISION_ID
FROM dbo_CUST_BOOK_LINE, tbl_PartInfo
GROUP BY dbo_CUST_BOOK_LINE.CUST_ORDER_ID, dbo_CUST_BOOK_LINE.PART_ID, dbo_CUST_BOOK_LINE.USER_ORDER_QTY, dbo_CUST_BOOK_LINE.UNIT_PRICE, dbo_CUST_BOOK_LINE.CUST_ORDER_LINE_NO;
This has been far more complicated that anything I've done so far, so any help would be greatly appreciated. Sorry about the long column names, I didn't get to choose them.
I did some research and think I found a way to make it work, but I'm still testing it. Right now I'm using three queries, but it should be easily simplified into two when complete.
The first is an append query that takes the two columns I want to get distinct combo's from and selects them and using "group by," while also selecting max of the revision column. It appends them to another table that I'm using called tbl_TempDrop. This table is only being used right now to reduce the number of results before the next part.
The second is an update query that updates tbl_TempDrop to include all the other columns I wanted by setting the criteria equal to the three selected columns from the first query. This took an EXTREMELY long time to complete when I had 700,000 records to work with, hence the use of the tbl_TempDrop.
The third query is a basic append query that appends the rows of tbl_TempDrop to the end destination, tbl_PartInfo.
All that's left is to run all three in a row.
I didn't want to include the full details of any tables or queries yet until I ensure that it works as desired, and because some of the names are vague since I will be using this method for multiple query searches.
This website helped me a little to make sure I had the basic idea down. http://www.techonthenet.com/access/queries/max_query2_2007.php
Let me know if you see any flaws with the ideology!

how to do a self-join

I have the following table with 3 columns, this is just a sample (not a real one)
Number Decription Value
1 Green 100
1 Yellow 101
1 Blue 102
2 Chair 200
2 Table 101
2 Green 150
3 Car 200
3 plane 205
3 green 105
My first query is to find any record or row which contain value "101" No problemo in this scenario I find 2 records [1, Yellow,101] and [2, table, 101] and all the records with number 3 and the rest are ignored just perfect. Now I need to select other records based on the RESULT of the FIRST query, the number 1 and 2 are the true results. So from column [number] in this case I found [1 and 2], I want to search and add the value of any description = [green]. Still ignoring [3] which has NO [101] value.
The ideal result I want to display is
[1, yellow, 101] and Green is 100
[2, table, 101] and Green is 150
I have got a headache to get it work so far NO good result. If anyone has a any idea how to make the script for this case please let me know. I hope the questions is clear.
P.S the content of the table is fake just to get an impression what is about and fyi it's SQL + PHP.
Try this:
select
concat('[', concat_ws(', ', s1.number, s1.description, s1.value), '] and ', s2.description, ' is ', s2.value)
from
sample s1
inner join
sample s2 on s2.number = s1.number
where
s1.value = 101 and
s2.description = 'Green'
SQL Fiddle Demo
Try this. I think this is what you are trying to do.
edit. Are you trying to only get the "Green"?
SELECT * from table WHERE table.Number IN (
SELECT Number FROM table WHERE table.Value = 101
) AND table.Description = "Green";
I have seen that thx a alot but it shows blanco fields :(.. however I have done many tests this morning after so much coffee I think what I am trying to do doesn't make sens. Because if I find the record [1, yellow, 101] based on the condition value "101" then it is impossible to add the content-field [Green] as I wanted, because number[1] matches both [101] and [Green].
I tried "OR" but it finds also the record [3 green, 105] which is not good because 3 has no [101] data. I know this is crazy puzzle. So now I want to add another Table which will have a UNIQUE number 1,2 and 3 and that number should correspond to the 1st column of the table sample only sample has duplicate of 1's 2' and 3's etc. If I do that would be possible to have a workable condition, where I can display data in one row or list as the following ==> 1, yellow, 101, and Green, 100. I hate to give up :(