I am working in SQL Server 2012. I know I can use there 'where' clause in a query to do a multi-variable search on a single column, eg.:
select * from cars where manufacturer in ('Ford', 'Toyota')
would return all rows that have Fort or Toyota as the manufacturer. However, what if I needed to be more specific, and I wanted to see cars made from Ford in 1995, and cars made in Toyota in 1996? Doing a query like this:
select *
from cars
where manufacturer in ('Ford', 'Toyota')
and year in ('1995', 1996)
would bring me back the data I needed, but also cars Toyota made in 1995, and cars Ford made in 1996, which I do not want in my dataset. I've seen some suggestions to do a query like this:
select *
from cars
where (manufacturer, year) in (('Ford', '1995'), ('Toyota', 1996))
However, this does not compile... I'm thinking this is a MySQL query. I would also like to be able to take the parameters for the where query from other tables, and not have to type in 'Ford', 'Toyota', '1995' every time I want to search
Maybe this is what you are looking for?
select * from cars
where (manufacturer = 'Ford' and year = '1995')
or (manufacturer = 'Toyota' and year = '1996')
Just right the filter as you typed it in the question:
WHERE
(manufacturer='Ford' and year=1995) or
(manufacturer='Toyota' and year=1996)
There's no need to overcomplicate the WHERE statement. In fact, where (manufacturer, year) in (('Ford', '1995'), ('Toyota', 1996)) isn't standared SQL
Related
I have a table A which contains list of counties for a given year along with other records.
Columns are 'Year' and 'CountyName'.
For 2020, there are 6434 'Counties' listed and for 2019, 6433 'Counties' are listed (in the same table).
How do I find the CountyName that is missing in 2019?
I can ofcourse run a simple query to get these records individually and then find out the missing county but I am trying to check if it can be done at the query level.
Thank you.
You can easily achieve it with the usage of the not in function or the exist one.
Something like:
Select countryname from table where year =2019 and countryname not in (Select countryname from table where year =2020)
I'm using SQL in Devexpress dashboard designer. I want to select distinct combinations of two parameters.
Perhaps Devexpress uses Transact-SQL but at the same time GROUP BY clause never works for me.
At the same time DISTINCT BY somehowe doesn't work as well.
Example:
There are two IDs 11 and 22
And there are two values of Date for 11, as an example: 21.01.2000 and 22.01.2000. And there's one for 22 as an example: 23.05.2008
Problem here is that I can't coose DISTINCT by date because there are many other IDs which have the same dates.
So I expect to have one distinct combination of ID and Date.
Does anyone faced with the same problem, can you advice any solution / code example?
Using select distinct will filter duplicates if you leave unique row properties out of the selected fields.
so:
Mike Smit
Mike Smit
Will be reduced to
Mike Smit
But if you're also asking for a PK like a Id field you get the following because id makes both rows distinct
1 Mike Smit
2 Mike Smit
Does this help?
I'm struggling with eliminating data from my query. I have attached a picture with my data results (data itself is too large and has customer info so I can't include). I have two tables that I'm joining by SKU to show when we enter a SKU into the system and when we sell it. We reuse SKUs based on vendors which isn't the best practice but is currently a necessity. What I'd like to do is eliminate the InvoiceDates where InvoiceDate < TransferDate. So in the InvoiceDate column it would only show the highlighted yellow dates for the first few rows.
Please let me know if you have any questions and thanks for the help!
This would work:
q) update InvoiceDate:{x where x >= y}'[InvoiceDate;TransferDate] from tbl
Explanation:
Above query uses 'each-both(') function to iterate over InvoiceDate and TransferDate values pair wise(indirectly row wise), pass each pair to lambda function as 'x' and 'y' and then select 'x'(InvoiceDate) which are >= 'y'(TransferDate)
You question is cut off, but I'm guessing you want to filter on whether a particular date is in your invoiceDate lists. You can do this as follows:
q)select from tbl where in[2019.01.01;] each invoiceDate
If this isn't what you are looking for, please clarify above with an example
I have a question here. we have a customer list and product list and sale table. We want to show each customer to buy each product's total sale.
so I use the query like following:
select ...
from ...
where customer="" and product="".
the query is standard simple one. but the table/dashboard is 20*10. It means for each customer and product pair i have to run a query. i have to run query 200 times. which is super slow.
how to improve this? thanks
right now the dashboard give me 20 customer and 10 product and then i go to database for 200 times. it is from a customer list to pick first 20 and another 20 like this way. the product is the same way to choose.
You can use group by ... Something like
Select customer, product, sum(sales) -- or whatever you need
From ...
Group by customer, product
The server would do the aggregation, and the query is much faster than doing 200 queries
Background: I have a payroll system where leave is paid only if it falls in range of the invoice being paid. So if the invoice covers the last 2 weeks then only leave in the last 2 weeks is to paid.
I want to write a sql query to select the leave.
Assume a table called DailyLeaveLedger which has among others a LeaveDate and Paid flag.
Assume a table called Invoice that was a WeekEnding field and a NumberWeeksCovered field.
Now assume week ending date 15/05/09 and NumberWeeksCovered = 2 and a LeaveDate of 11/05/09.
This is an example of how I want it written. The actual query is quite complex but I want the LeaveDate check to be a In subquery.
SELECT *
FROM DailyLeaveLedger
WHERE Paid = 0 AND
LeaveDate IN (SELECT etc...What should this be to do this)
Not sure if its possible the way I mention?
Malcolm
So LeaveDate should be between (WeekEnding-NoOfWeeksCovered) and (WeekEnding) for some Invoice?
If I've understood it right, you might be able to use an EXISTS() subquery, something like this:
SELECT *
FROM DailyLeaveLedger dl
WHERE Paid = 0 AND
EXISTS (SELECT *
FROM Invoice i
WHERE DateAdd(week,-i.NumberOfWeeksCovered,i.WeekEnding) < dl.LeaveDate
AND i.WeekEnding > dl.LeaveDate
/* and an extra clause in here to make sure
the invoice is for the same person as the dailyleaveledger row */
)