Merging tables into New table while retaining information - sql

I apologize in advance as the title is not very descriptive.
I have approximately 40+ tables each with the same exact table layout columns, and data save for one column. I would like to take that column which is not the same across all the tables and merge them into one table. Confusing? Let me illustrate..
SRCTbl01:
ID TYPE COLR1 INSTOCK
-----------------------
1 B RED YES
2 B BLUE YES
3 P GREEN NO
4 B BLACK YES
SRCTbl02:
ID TYPE COLR1 INSTOCK
-----------------------
1 B RED YES
2 B BLUE NO
3 P GREEN YES
4 B BLACK YES
SRCTbl03:
ID TYPE COLR1 INSTOCK
-----------------------
1 B RED YES
2 B BLUE NO
3 P GREEN NO
4 B BLACK NO
RESULT: (Type P to be excluded)
ID TYPE COLR1 SRCTbl01 SRCTbl02 SRCTbl03
----------------------------------------------
1 B RED YES YES YES
2 B BLUE YES NO NO
4 B BLACK YES YES NO
And finally after all that I'd like to make the table look something like this:
INSTOCK Table:
Customer RED BLUE BLACK
---------------------------
SRCTbl1 YES YES YES
SRCTbl2 YES NO YES
SRCTbl3 YES NO NO
I'm not entirely sure whether I can directly manipulate the tables into looking like the final iteration so I thought I should ask how to get it to the first Result as to me it seems simpler.
Thank you for the help, I've spent a full 8 hours on this and have yet to find a way to achieve it so I have come here to ask the experts.
EDIT: To clarify, I have had zero success in achieving the results I illustrated above. I have tried using SELECT .. Union SELECT, and FULL JOINS.
Using this code resulted in duplicates ( I was trying to just get the INSTOCK)
SELECT 01.INSTOCK, 02.INSTOCK, 03.INSTOCK
FROM dbo.SRCTbl01 AS 01, dbo.SRCTbl02 AS 02, dbo.SRCTbl03 AS 03
WHERE 01.TYPE='B'
I tried very many but this is probably the closest I got.

you could do something like that, all in one :
You first flatten with union, then take the max 'instock' for each color by customer (MAX will work as 'YES' > 'NO')
select Customer, MAX(RED) as RED, MAX(BLUE) as BLUE, MAX(BLACK) as BLACK
FROM(
SELECT 'SRCTbl01' as Customer,
CASE WHEN COLR1 = 'RED' then INSTOCK ELSE 'NO' END as RED,
CASE WHEN COLR1 = 'BLUE' then INSTOCK ELSE 'NO' END as BLUE,
CASE WHEN COLR1 = 'BLACK' then INSTOCK ELSE 'NO' END as BLACK
FROM SRCTbl01
WHERE Type <> 'P'
UNION
SELECT 'SRCTbl02' as Customer,
CASE WHEN COLR1 = 'RED' then INSTOCK ELSE 'NO' END as RED,
CASE WHEN COLR1 = 'BLUE' then INSTOCK ELSE 'NO' END as BLUE,
CASE WHEN COLR1 = 'BLACK' then INSTOCK ELSE 'NO' END as BLACK
FROM SRCTbl02
WHERE Type <> 'P'
UNION
SELECT 'SRCTbl03' as Customer,
CASE WHEN COLR1 = 'RED' then INSTOCK ELSE 'NO' END as RED,
CASE WHEN COLR1 = 'BLUE' then INSTOCK ELSE 'NO' END as BLUE,
CASE WHEN COLR1 = 'BLACK' then INSTOCK ELSE 'NO' END as BLACK
FROM SRCTbl03
WHERE Type <> 'P'
) as a
GROUP BY Customer
(You just need to add a CREATE TABLE INSTOCK as <codegiven>)
see SqlFiddle

Using UNION and PIVOT, something like this should do what you want (PIVOT should work on SQL Server 2005 and newer):
SELECT Customer, [Red], [Blue], [Black]
FROM (
SELECT 'SRCTbl01' AS Customer, COLR1, INSTOCK FROM SRCTbl01 WHERE Type <> 'P'
UNION
SELECT 'SRCTbl02' AS Customer, COLR1, INSTOCK FROM SRCTbl02 WHERE Type <> 'P'
UNION
SELECT 'SRCTbl03' AS Customer, COLR1, INSTOCK FROM SRCTbl03 WHERE Type <> 'P'
/* UNION ... */
) AS SRC
PIVOT (
MAX(INSTOCK)
FOR COLR1 IN ([Red], [Blue], [Black])
) AS pvt

Related

How can I return a column that says "No" for values missing from the result?

Sorry for the poor phrasing in the title; I can't figure out how to explain my question succinctly.
I have a list of fruit (apple, banana, pear) and want to create a table which looks like:
fruit eaten
apple yes
banana yes
pear no
from a table that looks like
fruit quantity
apple 120
banana 30
(Note that the original table does not include pears). So far I've figured out how to get to:
fruit eaten
apple yes
banana yes
by the query:
select
fruit,
case when quantity > 0 then "yes" else no end as eaten
from original_table
But I can't figure out how to get those pesky pears to be included.
If you have a fruit table you could do something like this:
SELECT
f.fruit
,CASE WHEN ot.quantity > 0 THEN 'yes' ELSE 'no' END as Eaten
FROM
fruit f
LEFT JOIN original_table ot
ON f.fruit = ot.fruit
If you do not have a fruit table then you need to make one. I am not too familiar with hive but this technique should work in lots of different platforms:
SELECT
f.fruit
,CASE WHEN ot.quantity > 0 THEN 'yes' ELSE 'no' END as Eaten
FROM
(
SELECT 'apple' as fruit
UNION ALL
SELECT 'banana'
UNION ALL
SELECT 'pear'
) f
LEFT JOIN original_table ot
ON f.fruit = ot.fruit

How to SELECT COUNT multiple values in one column

rather a newbie at SQL, so please be gentle....as I think this is a basic one.
I'm trying to write a query with multiple (13) counts, based off Column1. The 1st Count is the over-all total. And then the 12 others are filtered by Color. I can get my results by doing multiple Counts all in one query, but this gives me 13 rows of data. The goal here is to get everything on just one row. So, almost like each count would be its own column. Here is an example of the data model
Database = CARS, Table = TYPES, Column1 = LICENSE, Column2 = COLOR
SELECT COUNT (LICENSE) AS 'Total ALL Cars'
FROM CARS.TYPES WITH (NOLOCK)
SELECT COUNT (LICENSE) AS 'Total RED Cars'
FROM CARS.TYPES WITH (NOLOCK)
WHERE COLOR = 'RED'
And on & on & on for each remaining color. This works, but again, I'm trying to streamline it all into one row of data, IF possible. Thank you in advance
You simply need to include color in select statement and group by it to count cars of each color.
SELECT Color, Count(*)
FROM CARS.TYPES WITH(NOLOCK)
GROUP BY Color
or
SELECT COUNT(CASE WHEN Color = 'RED' THEN 1
ELSE NULL
END) AS RedCars
,COUNT(CASE WHEN Color = 'BLUE' THEN 1
ELSE NULL
END) AS BlueCars
,COUNT(*) AS AllCars
FROM CARS.TYPES WITH ( NOLOCK )
You can do this with a conditional SUM():
SELECT SUM(CASE WHEN Color = 'Red' THEN 1 END) AS 'Total Red Cars'
,SUM(CASE WHEN Color = 'Blue' THEN 1 END) AS 'Total Blue Cars'
FROM CARS.TYPES
If using MySQL you can simplify further:
SELECT SUM(Color = 'Red') AS 'Total Red Cars'
,SUM(Color = 'Blue') AS 'Total Blue Cars'
FROM CARS.TYPES
Or with PIVOT
SELECT RED + BLUE + GREEN AS total,
RED,
BLUE,
GREEN
FROM CARS.TYPES PIVOT (COUNT (LICENSE) FOR COLOR IN ([RED], [BLUE], [GREEN])) P
SELECT SUM(Al) AllCount, SUM(Red) RedCount, SUM(Green) GreenCount, ...
(
SELECT 1 AS Al
, CASE WHEN Color = 'Red' THEN 1 ELSE 0 END AS Red
, CASE WHEN Color = 'Green' THEN 1 ELSE 0 END AS Green
...
FROM CARS.Types
)

SQL display summation of data in row

I have a table like this
No.
--
b
r
g
g
r
b
r
g
I want resultset like below
Type of color | Ocurrence
Blue 2
green 3
red 3
TOTAL 8
Please help
Sounds like CASE and GROUP BY would be what you need;
SELECT
CASE WHEN color = 'r' THEN 'red'
WHEN color = 'g' THEN 'green'
WHEN color = 'b' THEN 'blue'
END "Type of color", COUNT(color) "Occurrence"
FROM Table1
GROUP BY color
ORDER BY color;
An SQLfiddle to test with.
To get a total, one (not necessarily the simplest) way is to just UNION with the total;
WITH cte AS (
SELECT
CASE WHEN color = 'r' THEN 'red'
WHEN color = 'g' THEN 'green'
WHEN color = 'b' THEN 'blue'
END "Type of color", COUNT(color) "Occurrence"
FROM Table1
GROUP BY color
UNION
SELECT 'TOTAL',COUNT(*)
FROM Table1
)
SELECT * FROM cte
ORDER BY CASE WHEN "Type of color" = 'TOTAL' THEN 1 END;
Another SQLfiddle.
Joachim's answer is fine, except there is an easier way to get the total using rollup:
SELECT
CASE WHEN color = 'r' THEN 'red'
WHEN color = 'g' THEN 'green'
WHEN color = 'b' THEN 'blue'
when color is NULL then 'Total'
END "Type of color", COUNT(*) "Occurrence"
FROM Table1
GROUP BY color with rollup
ORDER BY (case when color is null then 1 else 0 end), color

Select records based on column priority

First of all, the title of this question is horrible, but I didn't find a better way to describe my issue.
There's probably a very easy way to do this, but I couldn't figure it out. This is very similar to this question, but I'm running on sqlite3 (iOS) so I suspect my options are much more limited.
I have a table with product records. All records have an ID (note: I'm not talking about the row ID, but rather an identification number unique to each product). Some products may have two entries in the table (both with the same ID). The only difference would be in a special column (let's say column COLOUR can be either RED or GREEN).
What I want to do is create a list of unique products based on the value of COLOUR, with priority to GREEN if both GREEN and RED records exist for the same product.
In short, if I have the following case:
id PRODUCT ID COLOUR
1 1001 GREEN
2 1002 GREEN
3 1002 RED
4 1003 RED
I would like my SELECT to return the rows 1, 2 and 4. How can I achieve this?
My current approach is to have separate tables, and do the join manually, but obviously this is a very bad idea..
Note: I've tried to use the same approach from here:
SELECT *
FROM xx
WHERE f_COLOUR = "GREEN"
UNION ALL
SELECT *
FROM xx
WHERE id not in (SELECT distinct id
FROM xx
WHERE f_COLOUR = "GREEN");
But the result I'm getting is rows 1,2,3,4 instead of just 1,2,4. What am I missing?
Edit: One more question please: how can this be made to work with a subset of records, ie. if instead of the entire table I wanted to filter some records?
For example, if I had something like SELECT * FROM table WHERE productID LIKE "1%" ... how can I retrieve each unique product, but still respecting the colour priority (GREEN>RED)?
Your query is nearly correct. Just use PRODUCTID and not ID.
SELECT *
FROM xx
WHERE f_COLOUR = "GREEN"
UNION
SELECT *
FROM xx
WHERE PRODUCTID not in
(SELECT PRODUCTID
FROM xx
WHERE f_COLOUR = "GREEN");
SQLFiddle Demo
Try this
SELECT *
FROM xx
WHERE COLOUR = 'GREEN'
UNION
SELECT *
FROM xx WHERE P_Id not in
(SELECT P_Id
FROM Persons
WHERE COLOUR = 'GREEN');
See ALSO SQL FIDDLE DEMO
I just want to offer that you can do this with a group by:
select (case when sum(case when colour = 'Green' then 1 else 0 end) > 0
then max(case when colour = 'Green' then id end)
else max(case when colour = 'Red' then id end)
end) as id,
product_id
(case when sum(case when colour = 'Green' then 1 else 0 end) > 0 then 'Green'
else 'Red'
end) as colour
from t
group by product_id
You can have it like this
WITH PriorityTable
AS
(
SELECT T.*,
ROW_NUMBER() OVER (PARTITION BY T.ID
ORDER BY PT.ColorPriority ) PriorityColumn
FROM XX AS T
INNER JOIN (
SELECT 'RED' AS f_COLOUR , 1 AS ColorPriority
UNION
SELECT 'GREEN' AS f_COLOUR , 2 AS ColorPriority
) AS PT
ON T.f_COLOUR = PT.f_COLOUR
)
SELECT * FROM PriorityTable
WHERE PriorityColumn = 1

SQL Join Tables

Table one contains
ID|Name
1 Mary
2 John
Table two contains
ID|Color
1 Red
2 Blue
2 Green
2 Black
I want to end up with is
ID|Name|Red|Blue|Green|Black
1 Mary Y Y
2 John Y Y Y
Thanks for any help.
Thanks for the responses. I'm going to re-post this with some additional info about exactly what I'm trying to do that may complicate this. Can someone close this?
If you use T-SQL you can use PIVOT (http://msdn.microsoft.com/en-us/library/ms177410.aspx)
Here is query I used:
declare #tbl_names table(id int, name varchar(100))
declare #tbl_colors table(id int, color varchar(100))
insert into #tbl_names
select 1, 'Mary'
union
select 2, 'John'
insert into #tbl_colors
select 1, 'Red'
union
select 1, 'Blue'
union
select 2, 'Green'
union
select 2, 'Blue'
union
select 2, 'Black'
select name,
case when [Red] is not null then 'Y' else '' end as Red,
case when [Blue] is not null then 'Y' else '' end as Blue,
case when [Green] is not null then 'Y' else '' end as Green,
case when [Black] is not null then 'Y' else '' end as Black
from
(
select n.id, name, color from #tbl_names n
inner join #tbl_colors c on n.id = c.id
) as subq
pivot
(
min(id)
FOR color IN ([Red], [Blue], [Green], [Black])
) as pvt
And here is output:
John Y Y Y
Mary Y Y
I can use a CASE statement with a subquery to input the Y values.
select ID, Name,
case
when exists (select * from Colors C where C.ID = N.ID and Color = 'Red') then
'Y'
else
NULL
end
,
case
when exists (select * from Colors C where C.ID = N.ID and Color = 'Blue') then
'Y'
else
NULL
end
,
case
when exists (select * from Colors C where C.ID = N.ID and Color = 'Green') then
'Y'
else
NULL
end
,
case
when exists (select * from Colors C where C.ID = N.ID and Color = 'Black') then
'Y'
else
NULL
end
from Names N
I think you're going to have to end up with something like this :
SELECT t1.ID,
t1.Name,
CASE
WHEN red.ID IS NULL THEN ''
ELSE 'Y'
END As Red,
CASE
WHEN blue.ID IS NULL THEN ''
ELSE 'Y'
END As Blue
FROM Table1 t1
LEFT JOIN Table2 Red
ON t1.ID = Red.ID AND Red.Color = 'Red'
LEFT JOIN Table2 Blue
ON t1.ID = Blue.ID AND Blue.Color = 'Blue'
MS Sql does not support PIVOT queries like MS Access.
As other commenters have pointed out, you don't display exactly how you are linking people and colors. If you are using a linking table (person_id, color_id) then there is no way to solve this problem in standard SQL since it requires a pivot or cross-tabulation, which is not part of standard SQL.
If you are willing to add the condition that the number of colors is limited and known and design time, you could come up with a solution using one join for each color and CASE or IF functions in the SQL. But that would not be elegant and, furthermore, I wouldn't trust that condition to stay true for very long.
If you are able to come up with a different way of storing the color linking information you might have more options for producing the output you want, but a different storage technique implies some degree of denormalization of the database which could well cause other difficulties.
Otherwise, you will have to do this in a stored procedure or application code.
Contrary to what some other posters have said; I see no need for a third table. If colors are a well known enumeration in you application then you don't need a "Color" table.
What you are looking for is a PIVOT like this one.