How to select distinct combinations in T-SQL - tsql

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?

Related

Tableau Target vs actual - data drop issue

I am having a serious issue in tableau when comparing the current month value to the current month target. I am currently using data blending for this purpose. But this issue wont solve even if I use tableau relationship joining. I have 3 join clauses. Such as branch ID, Staff ID and Month/year. Pls see the below
Ex - january Target - 95,000
Actual - 126,000
But when 3 joining clauses created, its dropping data to 111,900.
the reason is in january, even the allocated branch ID and month is matching, the other staff id clauses are dropping off. That means even though the table A has all branches , staff ID's and date key, the txn table is having only one staff ID maching for January. If the 3 matches are not satisfying , data is dropping itself. How to solve this isseue? I need to show the total value of 126,000 infront of 95,000. not 111,900.
Hope anyone can help.
Many thanks
This got achieved by a table unioning.
Seems like this could have been solved by changing the join type from an inner join to a right join.

KDB - Filter List Column Based on Another Column

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

How to use a (repeating) aggregate function value with other columns from the table I use the aggregate function on

Problem: I have to count the number of times a certain user has a certificate and then return the users name, his number of certificates and the difference between the maximum number of certificates across all users and this specific users number of certificates. I succeeded in the first part (getting the number of certificates) which I'll denote as $query$ (because I have a feeling my problem has something to do with aliasing).
So $query$ looks like this:
User |N_Certificates
Geoff 4
Ann 2
Lisa 0
And my end result should look like this:
User |N_Certificates |Difference
Geoff 4 0
Ann 2 2
Lisa 0 4
I tried this query:
SELECT Sub.name, Sub.N_Certificates,
MAX(Sub.N_Certificates)- Sub.Certificates AS Difference FROM ($_query_$) AS SUB
but it returned a error (because I was trying to use an aggregate function in combination with a column I was not grouping by) or a wrong result (notably, difference=0 for all columns).
I tried a contraption with INNER JOIN on another version of sub (same $query$ code with another alias) but it also didn't work (same reason). I could ofcourse hard code the max but I don't think that's a good solution. My about screen tells me I'm using version 1.18 of pg_Admin.
You can't do it in this way, SQL syntax doesn't allow this.
The easiest way is to use a subquery:
SELECT Sub.name, Sub.N_Certificates,
(SELECT MAX(Sub.N_Certificates) FROM ($_query_$))
-
Sub.Certificates AS Difference
FROM ($_query_$) AS SUB
You can also use a common table expression:
WITH some_alias AS(
SELECT * FROM ($_query_$)
)
SELECT name, N_Certificates.
(SELECT MAX(N_Certificates)FROM some_alias)
-
Certificates AS Difference
FROM some_alias
And you can use a windows function: http://www.postgresql.org/docs/9.1/static/tutorial-window.html
SELECT Sub.name, Sub.N_Certificates,
MAX(Sub.N_Certificates) OVER ()
-
Sub.Certificates AS Difference
FROM ($_query_$) AS SUB

Postgres - Order by 2 fields contemporary

I've got a problem with an order by.
I have this table
|desc|phone|calls|group|priority|
|cccc|12347|700|13247|0|
|aaaa|12345|900|12345|0|
|bbbb|12346|500|12345|1|
I need to order this table for calls respecting the group, so my result should be
|desc|phone|calls|group|priority|
|aaaa|12345|900|12345|0|
|bbbb|12346|500|12345|1|
|cccc|12347|700|13247|0|
because bbbb is with the same group of aaaa.
How can I do this?
Thanks
EDIT:
Hi all,
sorry if my question is unclear, next time I'll be more specific.
Yes, I need to order this table for calls respecting the group, like if bbbb doesn't exists
Since your question is a bit unclear, An order by is enough
select * from sample order by "group"
sqlfiddle-demo

SQL Server 2008: Pivot column with no aggregate function workaround

Yes I know, this question has been asked MANY times but after reading all the posts I found that there wasn't an answer that fits my need. So, Heres my question. I would like to take a column of values and pivot them into rows of 6 columns.
I want to take this...... And turn it into this.......................
G Letter Date Code Ammount Name Account
081278 G 081278 12 00123535 John Doe 123456
12
00123535
John Doe
123456
I have 110000 values in this one column in one table called TempTable. I need all the values displayed because each row is an entity to itself. For instance, There is one unique entry for all of the Letter, Date, Code, Ammount, Name, and Account columns. I understand that the aggregate function is required but is there a workaround that will allow me to get this desired result?
Just use a MAX aggregate
If one row = one column (per group of 6 rows) then MAX of a single value = that row value.
However, the data you've posted in insufficient. I don't see anything to:
associate the 6 rows per group
distinguish whether a row is "Letter" or "Name"
There is no implicit row order or number to rely upon to generate the groups
Unfortunately, the max columns in a SQL 2008 select statement is 4,096 as per MSDN Max Capacity.
Instead of using a pivot, you might consider dynamic SQL to get what you want to do.
Declare #SQLColumns nvarchar(max),#SQL nvarchar(max)
select #SQLColumns=(select '''+ColName+'''',' from TableName for XML Path(''))
set #SQLColumns=left(#SQLColumns,len(#SQLColumns)-1)
set #SQL='Select '+#SQLColumns
exec sp_ExecuteSQL #SQL,N''