I'm stuck trying to solve a problem using SQL (MySQL 5.6). Need to get the address which has ID 2 or, if not exists ID 2, the lower ID.
For example
|-----------------|---------------|
| CostumerID | AddressID |
|-----------------|---------------|
| 1 | 4 |
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
| 2 | 4 |
| 3 | 4 |
| 4 | 3 |
| 4 | 4 |
| 5 | 2 |
| 6 | 4 |
| 7 | 2 |
| 7 | 4 |
| 8 | 3 |
| 9 | 1 |
| 9 | 3 |
| 9 | 4 |
| 9 | 2 |
|-----------------|---------------|
If a costumerID have an AddressID 2, must get that. If not, must get the minimum AddressID.
The output must be like:
|-----------------|---------------|
| CostumerID | AddressID |
|-----------------|---------------|
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
| 4 | 3 |
| 5 | 2 |
| 6 | 4 |
| 7 | 2 |
| 8 | 3 |
| 9 | 2 |
|-----------------|---------------|
So far I've tried this:
SELECT distinct CostumerID,
if (AddressID= 2, AddressID,
(select min(b.AddressID) from Addresses b where b.AddressID= a.AddressID)) as tipus
FROM from Addresses a
but get duplicates at CostumerID.
Use aggregation with CASE logic:
SELECT
CostumerID,
CASE WHEN COUNT(CASE WHEN AddressID = 2 THEN 1 END) > 0
THEN 2 ELSE MIN(AddressID) END AS AddressID
FROM yourTable
GROUP BY
CostumerID;
SELECT CASE
WHEN EXISTS(SELECT *
FROM tbl_name
WHERE AddressID = 2)
THEN (SELECT *
FROM tbl_name
WHERE AddressID > 2 )
ELSE 'Default Value'
END
Related
Given the sample table of:
+----+----------+---------+------+
| id | userName | storeId | cost |
+----+----------+---------+------+
| 1 | foo | 1 | 10 |
| 2 | bar | 1 | 10 |
| 3 | baz | 5 | 5 |
| 4 | baz | 3 | 20 |
| 5 | qux | 1 | 5 |
| 6 | qux | 4 | 20 |
| 7 | qux | 15 | 30 |
| 8 | qux | 17 | 40 |
| 9 | qux | 3 | 5 |
| 10 | quux | 6 | 20 |
+----+----------+---------+------+
I would like to work out how many people purchased at each store and how many did not. I want the report to display the results grouped by store.
I know the statement select storeId, count(distinct username) as total from purchases group by storeId provides me with how many people purchased in each store, but I want to subtract the result of the query select count(distinct userName) from purchases; in another column. I would expect the sample output to display as follows.
+---------+-----------+--------------+
| storeId | purchased | notPurchased |
+---------+-----------+--------------+
| 1 | 3 | 2 |
| 3 | 2 | 3 |
| 4 | 1 | 4 |
| 5 | 1 | 4 |
| 6 | 1 | 4 |
| 15 | 1 | 4 |
| 17 | 1 | 4 |
+---------+-----------+--------------+
You can use NOT condition with IN() function
As long a subse3lect gives back only only one,
you can use following
SELECT
storeId,
COUNT(DISTINCT username) AS total,
((SELECT
COUNT(DISTINCT userName)
FROM
purchases) - COUNT(DISTINCT username)) notPurchased
FROM
purchases
GROUP BY storeId
storeId | total | notPurchased
------: | ----: | -----------:
1 | 3 | 2
3 | 2 | 3
4 | 1 | 4
5 | 1 | 4
6 | 1 | 4
15 | 1 | 4
17 | 1 | 4
db<>fiddle here
I'm working on a query where I need to count distinct CarId row when the column LocationId is not null and get all CarId if its null or 0 but the query that I tried distincts all the CarId even if its null
#LocId int
Select Count(distinct a.CarId) from VehicleDetails a
inner join VehicleDocuments b on a.DocId=b.DocId
left join VehicleShipmentDetails dpg on dpg.VehicleShipmentId= b.VehicleShipmentId
where b.LogicalDelete=0 and a.LogicalDelete=0
and (dpg.LocationId= #LocId or dpg.LocationId= 0 or dpg.LocationId is null)
| ID | CarId | LocationId | DateCreated |
|------+----------------+-----------------+---------------|
| 1 | 1 | 5 | 02/03/2019 |
| 2 | 2 | null | 01/14/2019 |
| 3 | 2 | 0 | 02/03/2019 |
| 4 | 2 | 5 | 12/30/2018 |
| 5 | 4 | 3 | 01/10/2019 |
| 6 | 3 | 5 | 02/14/2019 |
| 7 | 2 | 5 | 03/13/2019 |
Desired output:
| ID | CarId | LocationId | DateCreated |
+------+----------------+-----------------+---------------+
| 1 | 1 | 5 | 02/03/2019 |
| 2 | 2 | null | 01/14/2019 |
| 3 | 2 | 0 | 02/03/2019 |
| 4 | 2 | 5 | 03/13/2019 |
| 5 | 4 | 3 | 01/10/2019 |
| 6 | 3 | 5 | 02/14/2019 |
Current Output
| ID | CarId | LocationId | DateCreated |
+------+----------------+-----------------+---------------+
| 1 | 1 | 5 | 02/03/2019 |
| 2 | 2 | 5 | 01/14/2019 |
| 3 | 4 | 3 | 01/10/2019 |
| 4 | 3 | 5 | 02/14/2019 |
Im getting a count of 4 but i needed to have 6 as the Count
EDIT: My goal is to remove the row to Distinct CarId if the value of the LocationId is Null or 0 but on my Current code, It distincts all CarId that is null,0 and equals to #LocId
You can query something like this, replace your_table by your actual set of data.
SELECT ID, CardId, LocationId, DateCreated
FROM your_table as T
WHERE NOT EXISTS (SELECT *
FROM your_table as T1
WHERE T.ID > T1.ID AND T.CarID = T1.CarID)
In SQL, you can use the statement CASE to manage conditions (just like the "if then else" in other programming languages). In your case this function could help because you have two differents cases to handle.
I need to create a view that stores the top 200 rows for each userId from another table that has userId as one of its columns.
Found a way to do this using user-defined variables in SELECT, but then MySQL does not allow views with variables in the SELECT.
This is a part of the SELECT statement to be used in the view:
select *,#num:= if(#userId = userId, #num + 1, 1) as row_number,
#userId := userId as dummy from (SELECT #userId:=0, #num:=0) as init
Is it possible to replace #userId and #num with functions instead? Similar examples would of great help!!
Consider the following...
SELECT * FROM results;
+----+------------+--------+-------+-------+
| id | discipline | member | event | value |
+----+------------+--------+-------+-------+
| 1 | 1 | 2 | 4 | 10 |
| 2 | 1 | 1 | 4 | 8 |
| 3 | 1 | 2 | 5 | 9 |
| 4 | 2 | 3 | 5 | 9 |
| 5 | 1 | 2 | 6 | 11 |
| 6 | 1 | 2 | 7 | 11 |
| 7 | 1 | 2 | 1 | 11 |
| 8 | 1 | 2 | 3 | 7 |
| 9 | 1 | 1 | 8 | 8 |
+----+------------+--------+-------+-------+
Say I want to get the top 3 'value' results for each member. In the event of a tie, I'll take the lower 'event' first...
SELECT x.*
, COUNT(*) rank
FROM results x
JOIN results y
ON y.member = x.member
AND (y.value > x.value OR (y.value = x.value AND y.event <= x.event))
GROUP
BY x.member
, x.value
, x.event
HAVING COUNT(*) <=3;
+----+------------+--------+-------+-------+---------+
| id | discipline | member | event | value | rank |
+----+------------+--------+-------+-------+---------+
| 2 | 1 | 1 | 4 | 8 | 1 |
| 9 | 1 | 1 | 8 | 8 | 2 |
| 7 | 1 | 2 | 1 | 11 | 1 |
| 5 | 1 | 2 | 6 | 11 | 2 |
| 6 | 1 | 2 | 7 | 11 | 3 |
| 4 | 2 | 3 | 5 | 9 | 1 |
+----+------------+--------+-------+-------+---------+
Assume following table:
+----+-----------+
| id | session |
+----+-----------+
| 1 | abcd1234 |
| 2 | abcd1234 |
| 3 | abcd1234 |
| 4 | qwert5678 |
| 5 | qwert5678 |
| 6 | abcd1234 |
| 7 | abcd1234 |
| 8 | qwert5678 |
| 9 | abcd1234 |
| 10 | qwert5678 |
| 11 | qwert5678 |
| 12 | qwert5678 |
+----+-----------+
Suppose we want to get the first id of a given session, then set every instance of that session to the id for all sessions, such that the table becomes:
+----+-----------+
| id | session |
+----+-----------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 4 |
| 5 | 4 |
| 6 | 1 |
| 7 | 1 |
| 8 | 4 |
| 9 | 1 |
| 10 | 4 |
| 11 | 4 |
| 12 | 4 |
+----+-----------+
We have a table with approximately 45M records, and are essentially changing every instance of column b to the value of min(column a) when grouped by column b.
Is there a way to do this in a single query? We have attempted several.
update example e
set session =
(select id from
(select id,min(session)
from example as first_id
group by session
) as this_id
);
...which errors out: "Subquery returns more than 1 row".
update example e
join
(select id
from
(select id,min(session)
from example as first_id
group by session
) as this_id
) as etable
set session = first_id;
...which errors out: "Unknown column 'first_id' in 'field list'". Also used 'this_id' to the same effect.
And other queries. Is this possible in a single query? Are we thinking about this incorrectly?
Query:
SQLFIDDLEExample
UPDATE example
SET session =(SELECT MIN(e2.ID)
FROM (SELECT *
FROM example) e2
WHERE e2.session = example.session)
Result:
| ID | SESSION |
----------------
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 4 |
| 5 | 4 |
| 6 | 1 |
| 7 | 1 |
| 8 | 4 |
| 9 | 1 |
| 10 | 4 |
| 11 | 4 |
| 12 | 4 |
I want to be able to merge rows and find out how many are the same excluding the ID. For example, if I was to have this table:
+---------+-----------+-----------+
| ID | Col 1 | Col 2 |
+---------+-----------+ ----------+
| 1 | 1 | 5 |
| 2 | 1 | 5 |
| 3 | 4 | 9 |
| 4 | 3 | 9 |
| 5 | 1 | 5 |
| 6 | 1 | 5 |
| 7 | 1 | 5 |
| 8 | 4 | 9 |
+---------+-----------+-----------+
It would become:
+---------+-----------+-----------+---------+
| ID | Col 1 | Col 2 | Count |
+---------+-----------+ ----------+---------+
| 1 | 1 | 5 | 5 |
| 2 | 4 | 9 | 2 |
| 3 | 3 | 9 | 1 |
+---------+-----------+-----------+---------+
What would the query for this be?
beside the ID column in your result, to me it looks like you need:
SELECT Col1, Col2, Count(Col1)
FROM myTable
GROUP BY Col1, Col2
select col1,col2,count(*) as `count`
from table
group by col1,col2