-- List all titles that have been sold along with the artist, order date and ship date
SELECT title, artist, order_date, ship_date
FROM items,orders,orderline
WHERE orders.order_id = orderline.order_id
AND items.item_id = orderline.item_id;
I tried my own query up I get results below
Under the Sun, Donald Arley, 11/15/2013, 11/20/2013
Under the Sun, Donald Arley, 12/20/2013, 12/22/2013
Under the Sun, Donald Arley, 1/18/2014, 1/23/2014
Dark Lady, Keith Morris, 1/31/2014, 2/4/2014
Dark Lady, Keith Morris, 3/10/2014, 3/15/2014
Dark Lady, Keith Morris, 3/14/2014, 3/19/2014
Dark Lady, Keith Morris, 11/15/2013, 11/20/2013
Happy Days, Andrea Reid, 2/27/2014, 3/2/2014
Happy Days, Andrea Reid, 10/30/2013, 11/3/2013
Happy Days, Andrea Reid, 12/18/2013, 12/22/2013
The Hunt, Walter Alford, 1/31/2014, 2/4/2014
The Hunt, Walter Alford, 3/10/2014, 3/15/2014
etc...............
This looks like a generic homework question.
I suggest you familiarize yourself with this page and site http://use-the-index-luke.com/sql/join.
Solution:
Change your statement to:
SELECT
items.title, items.artist, orders.order_date, orders.ship_date
FROM
items
JOIN
orderline ON orderline.item_id = items.item_id
JOIN
orders ON orders.order_id = orderline.order_id
The resulting table (CSV) looks like this:
NAME ,TITLE ,YEAR ,QNTY ,CLUB ,PRICE ,LOWEST_CLUB ,LOWEST
Andy Aardverk ,Avarice is Good ,1998,1,Basic ,218.95, CARP ,215.95
Andy Aardverk ,Avarice is Good ,1998,1,Basic ,218.95, YRB Bronze ,215.95
Andy Aardverk ,Yon-juu Hachi ,1948,1,Basic ,44.95, CARP ,41.95
Boswell Biddles ,Not That London! ,2003,1,Basic ,12.5, CAA ,10
Boswell Biddles ,Not That London! ,2003,1,Basic ,12.5, Readers Digest ,10
Cary Cizek ,Ringo to Nashi ,1997,1,Basic ,32.95, YRB Gold ,29.95
Cary Cizek ,Ringo to Nashi ,1997,1,Basic ,32.95, York Club ,29.95
Cary Cizek ,Toronto Underground ,2001,1,YRB Gold ,14.45, York Club ,12.95
Egbert Engles ,Capricia's Conundrum ,1993,1,CARP ,13.45, Guelph Club ,12.95
Egbert Engles ,Tande mou nai ,2002,1,Basic ,112.95, Oprah ,104.95
Egbert Engles ,Tande mou nai ,2002,1,Basic ,112.95, YRB Silver ,104.95
Ekksdwl Qjksynn ,I don't think so ,2001,1,YRB Gold ,12.5, CAA ,11.5
George Wolf ,Math is fun! ,1995,1,YRB Silver ,13.5, CAA ,12
Jack Daniels ,Eigen Eigen ,1980,1,York Club ,57.95, Oprah ,56.95
Jack Daniels ,Okay Why Not? ,2001,1,York Club ,18.45, Oprah ,17.45
Jackie Johassen ,Getting into Snork U. ,2004,1,YRB Silver ,21.95, Waterloo Club ,20.45
Jackie Johassen ,Not That London! ,2003,1,Basic ,12.5, CAA ,10
Klive Kittlehart ,Will Snoopy find Lucy? ,1990,1,YRB Bronze ,14.95, YRB Gold ,12.95
Lux Luthor ,Is Math is fun? ,1996,1,Basic ,72.95, Oprah ,69.95
Lux Luthor ,Tropical Windsor ,2004,1,Basic ,18.95, Oprah ,17.95
Nigel Nerd ,Are my feet too big? ,1993,1,Basic ,13.95, CAA ,11.45
Nigel Nerd ,Dogs are not Cats ,1995,1,Basic ,35.95, UofT Club ,32.95
Phil Regis ,Databases made Real Hard ,2002,1,Basic ,39.95, Oprah ,35.95
Pretence Parker ,Tchuss ,2002,1,Basic ,24.95, Guelph Club ,21.95
Qfwfq ,The Earth is not Enough ,2003,1,YRB Gold ,37.37, Oprah ,36.37
Qfwfq ,Under Your Bed ,2004,1,Oprah ,14.85, CAA ,13.85
Suzy Sedwick ,Are my feet too big? ,1993,1,YRB Silver ,12.95, Oprah ,11.45
Tracy Turnip ,Will Snoopy find Lucy? ,1990,1,Basic ,15.95, Readers Digest ,13.95
Tracy Turnip ,Will Snoopy find Lucy? ,1990,1,Basic ,15.95, YRB Silver ,13.95
Tracy Turnip ,Yon-juu Hachi ,1948,1,Readers Digest ,41, York Club ,40.95
Valerie Vixen ,Base de Donne ,2003,1,YRB Bronze ,23.95, Readers Digest ,20.95
Xia Xu ,Where art thou Bertha? ,2003,1,Basic ,30.95, CAA ,26.95
Yves Yonge ,Radiator Barbecuing ,2002,2,Basic ,14.2, Waterloo Club ,12.2
Zebulon Zilio ,Transmorgifacation ,2004,1,Basic ,288.73, CAA ,278.73
34 record(s) selected.,,,,,,,
I want to be able to only show one of the lowest options. For example 'Andy Aardverk' has purchased 'Avarice is Good' and could have bought it from 'CARP' or 'YRB Bronze' for a lower price. I only want one to show so it could be 'CARP' or 'YRB Bronze' but not both.
I tried to use 'group by' on 'name, title, year, qnty, club, price' but was given this error:
'SQL0119N An expression starting with "LOWEST_CLUB" specified in a SELECT
clause, HAVING clause, or ORDER BY clause is not specified in the GROUP BY
clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a
column function and no GROUP BY clause is specified. SQLSTATE=42803'
It would have been easier with your actual query, but I'll give it a go anyways.
You can solve this problem by using a CTE like this:
;WITH CTE AS (
SELECT
ROW_NUMBER() OVER(PARTITION BY [NAME], [TITLE] ORDER BY LOWEST_CLUB ASC, some_fallback_if_two_prices_are_the_same ASC) AS RowNumber,
[NAME],
[TITLE],
col1,
col2,
lowest_price,
some_fallback_if_two_prices_are_the_same
FROM [Table]
)
SELECT * --or rewrite your columns if you want to avoid the RowNumber
FROM CTE
WHERE RowNumber = 1;
That SELECT inside the CTE should be your current query + the ROW_NUMBER() line.
Seeing as I don't have the query, I can't give you a final result. You'll have to fiddle with it until it works for you.
select C.customerId,(C.lastName+', '+C.firstName) as CustomerName, C.companyName,
D.companyName+' ('+D.lastName+','+D.firstName+')'
as "Parent CompanyName(Last, First)",S.siteId, S.nickName as siteName,
dbo.GetSiteTelemetryBoxList(s.siteId) as "DeviceId's",
dbo.GetSiteTelemetryBoxSKUList(S.siteId,0) as SKU
from Site S
INNER JOIN Customer C ON S.customerId = C.customerId
INNER JOIN Customer D ON D.customerId = C.parentCustomerId
where S.createDate between DATEADD(DAY, -65, GETUTCDATE()) and GETUTCDATE()
order by C.customerId, S.siteId
The above query returns values that look like this:
CID CustomerName companyName Parent CompanyName(Last, First) SiteName DeviceId SKU
888296 DeYoung, Scott DeYoung Farms Mercier Valley Irrigation (Mercier,Ralph) H E east 200241 NETB12WR
890980 Rust, Marcus NULL Chester Inc. (Young,Scott) Byroad east 346370 NETB12WR
890980 Rust, Marcus NULL Chester Inc. (Young,Scott) Byroad west 345431 NETB12WR
891094 Pirani, Mark A Pirani Farm AMX Irrigation (Burroughs,Michael) hwy 64 south 333721 UNKNOWN
891094 Pirani, Mark A Pirani Farm AMX Irrigation (Burroughs,Michael) HWY 64 North 250162 NETB12WR
891094 Pirani, Mark A Pirani Farm AMX Irrigation (Burroughs,Michael) HWY 64 West 250164 NETB12WR
891094 Pirani, Mark A Pirani Farm AMX Irrigation (Burroughs,Michael) HWY 64 East 250157 NETB12WR
891430 Gammil, Bob Gammil FArms AMX Irrigation (Burroughs,Michael) angel 333677 UNKNOWN
891430 Gammil, Bob Gammil FArms AMX Irrigation (Burroughs,Michael) cemetery 333564 UNKNOWN
The problem I face now is that if a customerId/Name is repeating in the result set. The SiteName, deviceId, SKU should be concatenated to represent the data as one value.
For example, Mark Pirani row would look like
CID CustomerName ... SiteName DeviceId's ...
891904 Pirani, Mark ... hwy 64 south, HWY 64 North, HWY 64 West, HWY 64 East 333721,250162,250164,250157 ...
You can convert the rows with something like this to transform the rows into a concatenated string:
select
distinct
stuff((
select ',' + u.username
from users u
where u.username = username
order by u.username
for xml path('')
),1,1,'') as userlist
from users
group by username
I believe this is more of a SQL query issue than a C# code issue, or more appropriately I believe it more efficient to solve this problem at the query level rather than the code level. Off the top of my head you can use SELECT DISTINCT or GROUP BY clauses.
Here is another StackOverflow question addressing this issue - How do I (or can I) SELECT DISTINCT on multiple columns?
I did some digging and found a few ways to implement it. Basically, the simple solution for this is using mysql's group_concat function. These links discuss how the group_concat can be implemented for SQL server. You can choose one based on your requirements.
Simulating group_concat MySQL function in Microsoft SQL Server 2005? -- This thread discusses a few ways to implement it.
Flatten association table to multi-value column? -- This thread discusses the CLR implemenation of it.
http://groupconcat.codeplex.com/ -- This was just perfect for me. Exactly what I was looking for. The project basically creates four aggregate functions that collectively offer similar functionality to the MySQL GROUP_CONCAT function.
I've been handed a task where I've been asked to make use of an SSIS 2008 package to create a CSV then FTP that CSV, following this tutorial here.
My query isn't quite ready.
I'm pulling results where the columns we're going after are coming out as rows. Been reading around on how to to do this with a cursor + stored procedure, but was wondering if anyone has conquered this obstacle with a simpler solution?
Query
SELECT
b.name as 'field',
a.value
FROM
[PROD_21C_Sitecore_Web].[dbo].[VersionedFields] a
INNER JOIN [PROD_21C_Sitecore_Web].[dbo].[Items] b
on a.FieldId = b.ID
WHERE
ItemId = '8C1D5767-FB1A-47A6-913C-E78AAC24ABC9'
Results
field value
-------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UnderGradYear 1972
Position1 <p>Cancer Centers of North Carolina, Asheville Hematology Oncology Associates - Medical Oncologist</p>
FellowshipCity1 Winston Salem, NC
FellowshipYear1 1981
__Updated by sitecore\cvance
Position3 <p>St. Joseph's Hospital - Subsection Chief of Hematology/ Oncology</p>
Languages {A5AB043B-86D3-4033-BEDD-928BCA0A13C3}
UnderGradCity Chapel Hill, NC
Interests <p></p>
Locations {6127E1AE-E2BF-4517-8BCB-46590AEB06A8}|{A7D2EA25-1B3F-48AE-B347-FC1E06F48202}
__Revision 22068c83-3504-4d89-a9bf-2a9e83a49ca3
LastName Paschal
FirstName Barton R.
MedicalSchoolCity Atlanta, GA
Fellowship1 <p>Bowman Gray School of Medicine - Medical Oncology</p>
FullName Barton R. paschal
UnderGradSchool University of North Carolina
Residency <p>Louisiana State University Medical Center - Internal Medicine</p>
ResidencyYear 1979
ResidencyCity Shreveport, LA
Specialities {B5B0F45A-E2BE-4F3F-92A6-C39C0D3016C5}|{4FCCE0BC-F3D4-4E27-AA7F-D436E61B2A1B}
AwardsHonors <p>Fellow, American College of Physicians, Phi Beta Kappa</p>
Position1City Asheville, NC
Position2City Asheville, NC
Title MD
__Created 20120509T085416
Internship <p>LSU Medical Center</p>
Photo <image mediaid="{C0F47AFC-CBAE-4043-A52F-BF8E344DC6DA}" mediapath="/Images/Physicians/paschal-web" src="~/media/Images/Physicians/paschal-web.jpg" alt="paschal" height="" width="" hspace="" vspace="" />
Position2 <p>Memorial Mission Hospital - Medical Oncologist & Chair, Department of Internal Medicine</p>
BoardCertifications Board Certified - Internal Medicine, Medical Oncology
Position3City Asheville, NC
__Updated 20130514T080506:635041155069332802
MedicalSchool Emory University School of Medicine - MD
Position4 <p>Hospice of Hendersonville County - Medical Director</p>
Position4City Henderson, NC
MedicalSchoolYear 1976
InternshipCity Shreveport, LA
InternshipYear 1979
ProfessionalAssociations {05E5C7FA-99DC-47C7-AC5E-2DA51D87DD1F}|{E519CAD2-C25F-4ADD-BD91-A4DEF861517B}|{67D7F01D-1695-4963-A149-EDF18354BBCC}
Thank you for looking.
Sounds like you want to PIVOT the data
SELECT
ItemId,
UnderGradYear,
Position1,
FellowshipCity1,
FellowshipYear1
-- add 'columns'
FROM
data -- this 'table' is the result of current query
PIVOT
(
MAX(value)
FOR field IN ([UnderGradYear], [Position1],
[FellowshipCity1], [FellowshipYear1]) -- add 'columns'
) PivotTable
demo
you can then use SSIS to create a CSV of result of above and FTP that file.
Edit: Alternatively you could also use the pivot transformation in SSIS rather than in T-SQL