I have a table like below
The main idea is to get the amount of each channel for each orderID.
If the channel is repeating for Id, it should take the amount only once and rest would be null.
The result should look like below
I want to do the same logic for country and source as well. If I do the pivot it should only give unique amount value for each ID on Channel, Source and Country basis and repeated value should be NULL.I have a very big data set and tough to do it in Excel
How can I do this in SQL Server? Is there any window function? Can anyone please help me?
Thanks in advance
Related
I have a problem with groping a set of records by a common value. A common value isn't one single value thought therefore I'm not sure how to approach it
This is a set / table:
This is what I'd need to achieve:
Is it too tricky or it could be achieved (SQL 2012). If it could please point me into a direction coz I'm hit a bit wall :D Thanks!
Use disctinct form when you get group data from specific column
SELECT DISTINCT SSC FROM Table_name;
I've 250+ columns in customer table. As per my process, there should be only one row per customer however I've found few customers who are having more than one entry in the table
After running distinct on entire table for that customer it still returns two rows for me. I suspect one of column may be suffixed with space / junk from source tables resulting two rows of same information.
select distinct * from ( select * from customer_table where custoemr = '123' ) a;
Above query returns two rows. If you see with naked eye to results there is not difference in any of column.
I can identify which column is causing duplicates if I run query every time for each column with distinct but thinking that would be very manual task for 250+ columns.
This sounds like very dumb question but kind of stuck here. Please suggest if you have any better way to identify this, thank you.
Solving this one-time issue with sql is too much effort. Simply copy-paste to excel, transpose data into columns and use some simple function like "if a==b then 1 else 0".
I have a very large database and I need to extract information from 3 columns:
I am trying to determine how many unique customers the user is processing.
Username. This is unique name.
CustomerNumber. A customer number will appear on many lines, as they could have ordered many products and each product is a line.
Date Range. I need to be able to define a date range.
The code I am tried and searched is counting the customer numbers, but not just the distinct customer number.
I have not tried the date range as yet.
I have attached 2 images to show an example of the database and the end result. We used a pivot table to produce this result, but the data changes all the time and we dont want to create a pivot table the whole time.
Image of Sample Data in Excel:
Image of Required Final Result
SELECT `'All data$'`.Username, Count(`'All data$'`.CustomerNumber)
FROM `C:\Users\rhynto\Desktop\Darren Qwix\QWIX_PICKED.xlsx`.`'All data$'` `'All data$'`
GROUP BY `'All data$'`.Username
I will appreciate any advice on this please.
I am trying to extract the following columns from a sql table called Vouchers:
Date,
VoucherID,
ValueIssued,
ValueRedeemed,
Valueexpired
That bit is straight forwards and returns the below data:
However I would like to show them
by day,
by voucherID
and then sum up the values for each individual voucherID to produce a view similar to the below:
Can anyone point me in the correct direction in regards to what sql code will do this?
I am using SSMS 2014
It's pretty straight forward
SELECT PosDate, VoucherId, SUM(ValueIssued), SUM(ValueRedeemed)
FROM Vouchers
GROUP BY PosDate, VoucherId
Note that column DateExpired isn't correct in this context. It should be either grouped by or removed entirely (as I did)
I have a requirement to display row values as column names in a data grid view. I want to get the store names into columns using sql select statement. (Please refer the attached image). I want user to enter some values under each column. So STORE 1, STORE 2, STORE 3 should displays as columns in datagrid view. Does anyone can help me to get this work?
while googling i found this can be done using PIVOT in SQL. But in this table i don't have any aggregate columns. Any help pls?
the result should be somthing like
You may know that your data only contains a single row for each pivoting column, but SQL Server has to construct a plan that could accommodate multiple rows.
So, use PIVOT and just use an aggregate that, if passed a single value, will return that same value. MIN() and MAX() fit that description (as does SUM if you're working with numeric data)
You may use specific function of dynamic pivot and pass your query with item count column.
You can use below link which provided you function and can easily show you expected output.
http://forums.asp.net/t/1772644.aspx/1
Procedure name:
[dbo].[dynamic_pivot]