How to get multiple columns values from other table - sql-server

I have two tables. One has all the values as an int and the other table has the string value that correspondents for that (int) value.
Table 01
ID
D1
D2
D3
01
12
18
9
02
66
33
14
Table 02
Column A
Column B
12
SomeText
66
Something
33
John
14
Doe
15
Mark
18
Walter
I want a way to create a view that will change the int values (d1/d2/d3) to the values in Column B from table 2. The way that I have tried is this query:
SELECT [id],
b.[Column B] AS [d1],
c.[Column B] AS [d2],
d.[Column B] AS [d3]
FROM [Table01]
LEFT JOIN [Table02] b ON d1 = b.[Column A]
LEFT JOIN [Table02] c ON d1 = c.[Column A]
LEFT JOIN [Table02] d ON d1 = d.[Column A];
This is weak solution and it takes a long time to load. Any other solutions?
ID
D1
D2
D3
01
Text
Mark
john
02
someth
Walter
doe

Related

Filter duplicate values in Google Spreadsheets

I have the following table:
[example]
How can I make a new table where all the duplicates are merged and the values are added together so I have a total per name ?
name value
A 34
B 25
A 18
C 14
B 16
A 9
B 4
C 9
name value
A 61
B 45
C 23
A:
SELECT name, SUM(value)
FROM tableName
GROUP BY name
use:
=QUERY(V:X; "select V,sum(W),sum(X) where V is not null group by V")

SQLITE: Keep all unmatched rows during join

I'm trying to join multiple tables.
Table 1:
ID
TEMP
DESC
NUMB
32
89
Y
6
47
NULL
Y
5
56
43
N
4
34
54
N
3
22
78
NULL
NULL
Table 2
ID
IND
FLAV
32
Y
G
47
N
G
56
Y
R
34
Y
B
22
Y
Y
Table 3:
ID
COLOR
SHAPE
32
RED
SQUARE
47
BLUE
CIRCLE
52
NULL
TRI
22
ORANGE
NULL
I want the resulting table:
ID
TEMP
DESC
NUMB
IND
FLAV
COLOR
SHAPE
32
89
Y
6
Y
G
RED
SQUARE
47
NULL
Y
5
N
G
BLUE
CIRCLE
56
43
N
4
Y
R
NULL
NULL
34
54
N
3
Y
B
NULL
NULL
22
78
NULL
NULL
Y
Y
ORANGE
NULL
52
NULL
NULL
NULL
NULL
NULL
NULL
TRI
The row order of the resulting ID's doesn't matter to me.
I've tried:
SELECT *
FROM Table1
INNER JOIN Table2 USING(ID)
LEFT JOIN Table3 USING(ID)
But it leaves out ID 52. I want to be sure no unmatched ID's from either table are left out.
Is this possible in SQLITE?
For this requirement the correct type of join is FULL OUTER JOIN wich is not supported by SQLite.
A workaround is to use a subquery that returns the distinct ids of all 3 tables and then do LEFT joins to the tables with the USING clause:
SELECT *
FROM (SELECT ID FROM Table1 UNION SELECT ID FROM Table2 UNION SELECT ID FROM Table3) t
LEFT JOIN Table1 USING (id)
LEFT JOIN Table2 USING (id)
LEFT JOIN Table3 USING (id);
See the demo.

How to join tables when there are duplicates in right table

I have three tables. Table Cust has a custID field, plus various other values (name, address etc)
Table List has a single column ID. Each ID is a custID in the Cust table
Edit: the purpose of this is to filter the records, restricting thge results to ones where the CustID appears in the list table.
All three tables are indexed.
Table Trans has a TransactionID field, a Cust field that holds a customer ID, And other transaction fields
Edit: I should have mentioned that in some cases there will be no transaction record. In this case I want one row of Customer info with the transaction fields null or blank.
I want a query to return cust and transaction ID for each ID in the list table. If there is more than one matching row in the transaction table, I want each included along 3with the matching cust info. So if the tables look like This:
Cust
ID Name
01 John
02 Mary
03 Mike
04 Jane
05 Sue
06 Frank
List
ID
01
03
05
06
Transact
TransID CustId Msg
21 01 There
22 01 is
23 02 a
24 03 tide
25 04 in
26 04 the
27 05 affairs
28 05 of
29 05 men
I want the result set to be:
CustID Name TransID Msg
01 John 21 There
01 John 22 is
03 Mike 24 tide
05 Sue 27 affairs
05 Sue 28 of
05 Sue 29 men
06 Frank -- --
(Where -- represents NULL or BLANK)
Obviously the actual tables are much larger (millions of rows), but that shows the pattern, one row for every item in table Transactions that matches any of the items in the List table, with matching fields from the Cust table. if there is no matching Transaction, one row of customer info from each ID in the List table. CustID is unique in the Cust and List tables, but not in the transaction table.
This needs to work on any version of SQL server from 2005 onward, if that matters.
Any suggestions?
Unless I'm missing something, this is all you need to do:
Select T.CustID, C.Name, T.TransID, T.Msg
From Transact T
Join Cust C On C.Id = T.CustId
Join List L On L.Id = C.Id
Order By T.CustID, T.TransID
;with cust (id, name) as
(
select 1, 'John' union all
select 2, 'Mary' union all
select 3, 'Mike' union all
select 4, 'Jane' union all
select 5, 'Sue'
), list (id) as
(
select 1 union all
select 3 union all
select 5
), transact (TransId, CustId, Msg) as
(
select 21, 1, 'There '
union all select 22, 1, 'is'
union all select 23, 2, 'a'
union all select 24, 3, 'tide'
union all select 25, 4, 'in'
union all select 26, 4, 'the'
union all select 27, 5, 'affairs'
union all select 28, 5, 'of'
union all select 29, 5, 'men'
)
select
CustId = c.id,
Name = c.Name,
TransId = t.TransId,
Msg = t.Msg
from cust c
inner join list l
on c.id = l.id
inner join transact t
on l.id = t.custid
yields:
CustId Name TransId Msg
----------- ---- ----------- -------
1 John 21 There
1 John 22 is
3 Mike 24 tide
5 Sue 27 affairs
5 Sue 28 of
5 Sue 29 men

How to sort a table by multiple columns using SQL Server?

I have an unsorted Table A.
C1 C2 C3 C4
----------------------
15 2 7 5
12 5 4 8
19 1 3 12
21 9 1 18
4 11 12 22
I want to sort this table in such a way that values with higher c1, higher c4, higher c2 and lower c3 are on top.
Also, c1 > c2 > c4 > c3 in terms of priority.
I am not sure how to go about this and would really appreciate any help from experts here.
Should I assign a fraction value to each column like c1 = .35 , c2 = .3, c4 = .2, and c3 = .15 and multiply the value for each column by its fraction value and add up all the values for each row?
Sounds like you need:
ORDER BY C1 DESC, C2 DESC, C4 DESC, C3 ASC
You mean simple order by? Then just add an order byclause:
ORDER BY C1 DESC, C4 DESC, C2 DESC, C3 ASC
DESC = descending, from highest to lowest
.ASC = ascending, from lowest to highest

Looking for missing rows T-SQL

I am having an issue and I have been working on it for the past three hours or so and have not found a solution. Running on a SQL Server platform. I have a single table that looks like this.
PT ITM VAL
-- --- ---
01 01 A
01 02 B
01 03 C
02 01 A
02 03 C
03 01 A
03 02 B
I am trying to find which PTs are missing the item numbers.
In the case above PT02 and PT03 are missing two item. This is the base what where I started, but I am not sure if I am even on the right track.
select t.PT,t.ITM
FROM MYTABLE t
GROUP BY t.PT,t.ITM
HAVING COUNT(*) > 1
Thanks
jlimited
If you're expecting 3 ITM per PT, the query would be
select PT
FROM MYTABLE
GROUP BY PT
HAVING COUNT(ITM) < 3
for other conditions a more complicated query is required.
Here was the solution that worked. I had to select a VAL that was populated, to find the values that were not.
select stg.PT,COUNT(stg.ITM) AS ITM_CNT
FROM MYTABLE stg
WHERE stg.ITM IS NOT NULL
AND stg.VAL IN (11)
GROUP BY stg.PT
HAVING COUNT(stg.ITM) > 1
EXCEPT
select stg.PT,COUNT(stg.ITM) AS ITM_CNT
FROM MYTABLE stg
WHERE stg.ITM IS NOT NULL
AND stg.VLA IN (4,5)
GROUP BY stg.PT
HAVING COUNT(stg.ITM) > 1

Resources