How to distinctly select a column while selecting other columns - sql

I have a table that looks something like this in hive. What I want to do is run a query such that every 3 hours, I look at unique workerUUIDs and do some manipulation on them. So what I want to do is between now and 3hrs before
Capture all the unique workerUUIDs
Select * from these workerUUIDs
I am using hive to run this query and the table has a few million entries every three- six hours. What is the best way to write this query?
--------------------------------------------
| workerUUID | City | Debt | TestN| LName|
|------------------------------------------|
| 1234 | SF | 100k | 23 | Nil |
|-------------------------------------------
| 6789 | NY | 150k | 34 | Fa |
|------------------------------------------|
| 1234 | SF | 10k | 45 | Na |
--------------------------------------------
| 6789 | NY | 1k | 13 | Nil |
|-------------------------------------------
| 6789 | SF | 150k | 34 | Nil |
|------------------------------------------|
| 8999 | IN | 10k | 45 | Na |
--------------------------------------------
Basically I want to do something like
select City, Debt, TestN where workerUUID = '1234'
select City, Debt, TestN where workerUUID = '6789'
select City, Debt, TestN where workerUUID = '8999'
To clarify further, I want to generate temporary tables like
| workerUUID | City | Debt | TestN|
|------------------------------------
| 1234 | SF | 100k | 23 |
|------------------------------------
| 1234 | SF | 10k | 45 |
|-----------------------------------|
| workerUUID | City | Debt | TestN|
|------------------------------------
| 6789 | NY | 150k | 23 |
|------------------------------------
| 6789 | NY | 1k | 13 |
|------------------------------------
| 6789 | NY | 150k | 34 |
|-----------------------------------
| workerUUID | City | Debt | TestN|
|------------------------------------
| 8999 | IN | 10k | 45 |
etc
for all the unique value of workerUUIDs generated in the 3 hour gap

Related

Display the sale id and sale date of sales made by salesmen working from London

I have three tables:
Salesman Table
+-----+---------+----------+
| SID | SNAME | LOCATION |
+-----+---------+----------+
| 1 | Peter | London |
| 2 | Michael | Paris |
| 3 | John | Mumbai |
| 4 | Harry | Chicago |
| 5 | Kevin | London |
| 6 | Alex | Chicago |
+-----+---------+----------+
Sale Table
+--------+-----+-----------+
| SALEID | SID | SLDATE |
+--------+-----+-----------+
| 1001 | 1 | 01-JAN-14 |
| 1002 | 5 | 02-JAN-14 |
| 1003 | 4 | 01-FEB-14 |
| 1004 | 1 | 01-MAR-14 |
| 1005 | 2 | 01-FEB-14 |
| 1006 | 1 | 01-JUN-15 |
+--------+-----+-----------+
Expected Result
+--------+-----------+
| SALEID | SLDATE |
+--------+-----------+
| 1001 | 01-JAN-14 |
| 1002 | 02-JAN-14 |
| 1004 | 01-MAR-14 |
| 1006 | 01-JUN-15 |
+--------+-----------+
I am using Oracle SQLDeveloper. I run the code below:
SELECT S.SALEID, S.SLDATE
FROM Salesman SA
INNER JOIN Sale S ON SA.SID = S.SID
WHERE SA.LOCATION = 'London';
but I get error:
Error: Your result did not match the Expected result.
If anyone can find the errors please answer.
OKAY, I got it, it was an database error, Refreshed the database and the same Query worked.
The query mentioned is correct tho.

Outer Join multible tables keeping all rows in common colums

I'm quite new to SQL - hope you can help:
I have several tables that all have 3 columns in common: ObjNo, Date(year-month), Product.
Each table has 1 other column, that represents an economic value (sales, count, netsales, plan ..)
I need to join all tables on the 3 common columns giving. The outcome must have one row for each existing combination of the 3 common columns. Not every combination exists in every table.
If I do full outer joins, I get ObjNo, Date, etc. for each table, but only need them once.
How can I achieve this?
+--------------+-------+--------+---------+-----------+
| tblCount | | | | |
+--------------+-------+--------+---------+-----------+
| | ObjNo | Date | Product | count |
| | 1 | 201601 | Snacks | 22 |
| | 2 | 201602 | Coffee | 23 |
| | 4 | 201605 | Tea | 30 |
| | | | | |
| tblSalesPlan | | | | |
| | ObjNo | Date | Product | salesplan |
| | 1 | 201601 | Beer | 2000 |
| | 2 | 201602 | Sancks | 2000 |
| | 5 | 201605 | Tea | 2000 |
| | | | | |
| | | | | |
| tblSales | | | | |
| | ObjNo | Date | Product | Sales |
| | 1 | 201601 | Beer | 1000 |
| | 2 | 201602 | Coffee | 2000 |
| | 3 | 201603 | Tea | 3000 |
+--------------+-------+--------+---------+-----------+
Thx
Devon
It sounds like you're using SELECT * FROM... which is giving you every field from every table. You probably only want to get the values from one table, so you should be explicit about which fields you want to include in the results.
If you're not sure which table is going to have a record for each case (i.e. there is not guaranteed to be a record in any particular table) you can use the COALESCE function to get the first non-null value in each case.
SELECT COALESCE(tbl1.ObjNo, tbl2.ObjNo, tbl3.ObjNo) AS ObjNo, ....
tbl1.Sales, tbl2.Count, tbl3.Netsales

SQL Server 2008 displaying records horizontally

I have the tables and data created in SqlFiddle( http://sqlfiddle.com/#!3/72398/3)
I have 3 tables with data
dbo.Contracts:
C_ID,
C_NAME,
C_VOLUME
dbo.Players:
P_ID,
P_NAME
dbo.ContractPlayers:
C_ID,
P_ID,
Share
I am able to do simple joins which displays the data vertically. but i need the data horizontally
Like this:
C_ID | C_NAME | C_Volume | P_NAME1 | SHARE1 | P_NAME2 | SHARE2 | P_NAME3 | SHARE3 |
1 | Agriculture | 40000 | Johndeer | 3000 | Statefarm | 4500 | Vortex | 3200 |
2 | Chemicals | 50000 | Johndeer | 1231 | Statefarm | 2345 | Vortex | 2311 |
3 | Autos | 35000 | Johndeer | 1212 | Statefarm | 1111 | Vortex | 4534 |
I am assuming this is possible. Please help Here is the sqlFiddle( http://sqlfiddle.com/#!3/72398/3)

Is it possible to see the 'null' in the table in sql instead of blank

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | |
| 7 | Muffy | 24 | Indore | |
+----+----------+-----+-----------+----------+
how to print the null instead of blank space in the above table in id 6,7 for salary column while inserting the values.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | null |
| 7 | Muffy | 24 | Indore | null |
+----+----------+-----+-----------+----------+
You can use ISNULL() or IFNULL() in your SELECT depending on the RDBMS. Your query would look something like this:
SELECT ID, NAME, AGE, ADDRESS, IFNULL(SALARY, "null") FROM YOURTABLE
Oracle equivalent of neelsg's answer,
SELECT id, name, age, address, NVL(salary, "null") FROM yourtable

SQL Join with Group By

Ok, so i'm trying to write a complex query (at least complex to me) and need some pro help. This is my database setup:
Table: MakeList
| MakeListId | Make |
| 1 | Acura |
| 2 | Chevy |
| 3 | Pontiac |
| 4 | Scion |
| 5 | Toyota |
Table: CustomerMake
| CustomerMakeId | CustomerId | _Descriptor |
| 1 | 123 | Acura |
| 2 | 124 | Chevy |
| 3 | 125 | Pontiac |
| 4 | 126 | Scion |
| 5 | 127 | Toyota |
| 6 | 128 | Acura |
| 7 | 129 | Chevy |
| 8 | 130 | Pontiac |
| 9 | 131 | Scion |
| 10 | 132 | Toyota |
Table: Customer
| CustomerId | StatusId |
| 123 | 1 |
| 124 | 1 |
| 125 | 1 |
| 126 | 2 |
| 127 | 1 |
| 128 | 1 |
| 129 | 2 |
| 130 | 1 |
| 131 | 1 |
| 132 | 1 |
What i am trying to end up with is this...
Desired Result Set:
| Make | CustomerId|
| Acura | 123 |
| Chevy | 124 |
| Pontiac | 125 |
| Scion | 131 |
| Toyota | 127 |
I am wanting a list of unique Makes with one active (StatusId = 1) CustomerId to go with it. I'm assuming i'll have to do some GROUP BYs and JOINS but i haven't been able to figure it out. Any help would be greatly appreciated. Let me know if i haven't given enough info for my question. Thanks!
UPDATE: The script doesn't have to be performant - it will be used one time for testing purposes.
Something like this:
select cm._Descriptor,
min(cu.customerid)
from CustomerMake cm
join Customer cu on cuo.CustomerId = cm.CustomerId and cu.StatusId = 1
group by cm._Descriptor
I left out the MakeList table as it seems unnecessary because you are storing the full make name as _Descriptorin the CustomerMake table anyway (so the question is what is the MakeList table for? Why don't you store a FK to it in the CustomerMake table?)
You want to
(a) join the customer and customermake tables
(b) filter on customer.statusid
(c) group by customermake._descriptor
Depending on your RDBMS, you may need to explicitly apply a group function to customer.customerid to include it in the select list. Since you don't care which particular customerid is displayed, you could use MIN or MAX to just pick an essentially arbitrary value.